export function createChannelExternalMonitorLoop(options) {
    let timer;
    let running = false;
    const tick = async () => {
        if (running) {
            return;
        }
        running = true;
        try {
            await options.run();
        }
        catch (error) {
            console.error(`Failed to monitor ${options.label} external activity:`, error);
        }
        finally {
            running = false;
        }
    };
    return {
        start() {
            if (timer) {
                return;
            }
            if (options.initial ?? true) {
                setTimeout(() => void tick(), 0).unref?.();
            }
            timer = setInterval(() => void tick(), options.intervalMs);
            timer.unref?.();
        },
        stop() {
            if (timer) {
                clearInterval(timer);
                timer = undefined;
            }
        },
        tick,
    };
}
