/**
 * Owns cli-v2 run-state polling mechanics for active terminal sessions.
 */
export class SessionRunStatePollerService {
    options;
    timer;
    inFlight = false;
    constructor(options) {
        this.options = options;
    }
    sync() {
        const address = this.options.getAddress();
        if (!this.options.isEnabled() || !address) {
            this.stop();
            return;
        }
        if (this.timer) {
            return;
        }
        this.timer = setInterval(() => {
            void this.tick();
        }, this.options.intervalMs);
    }
    dispose() {
        this.stop();
        this.inFlight = false;
    }
    stop() {
        if (!this.timer) {
            return;
        }
        clearInterval(this.timer);
        this.timer = undefined;
    }
    async tick() {
        const address = this.options.getAddress();
        if (!address || this.inFlight) {
            return;
        }
        this.inFlight = true;
        try {
            await this.options.poll(address);
        }
        catch (error) {
            this.options.onError(error);
        }
        finally {
            this.inFlight = false;
        }
    }
}
//# sourceMappingURL=session-run-state-poller-service.js.map