(options: IdleMonitorOptions)
| 77 | } |
| 78 | |
| 79 | export function createIdleMonitor(options: IdleMonitorOptions): IdleMonitor { |
| 80 | if (options.timeoutMs <= 0) { |
| 81 | return { |
| 82 | touch: () => { }, |
| 83 | stop: () => { }, |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | let timer: NodeJS.Timeout | null = null; |
| 88 | |
| 89 | const schedule = (): void => { |
| 90 | if (timer) clearTimeout(timer); |
| 91 | timer = setTimeout(() => { |
| 92 | timer = null; |
| 93 | if (options.isTransportAlive && options.isTransportAlive()) { |
| 94 | schedule(); |
| 95 | return; |
| 96 | } |
| 97 | options.onIdle(); |
| 98 | }, options.timeoutMs); |
| 99 | unrefHandle(timer); |
| 100 | }; |
| 101 | |
| 102 | schedule(); |
| 103 | |
| 104 | return { |
| 105 | touch: schedule, |
| 106 | stop: () => { |
| 107 | if (!timer) return; |
| 108 | clearTimeout(timer); |
| 109 | timer = null; |
| 110 | }, |
| 111 | }; |
| 112 | } |
| 113 | |
| 114 | export function startParentMonitor(options: ParentMonitorOptions): () => void { |
| 115 | if (!Number.isFinite(options.parentPid) || options.parentPid <= 1 || options.parentPid === process.pid) { |
no test coverage detected