forked from clerk/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaitForIdleProcess.ts
More file actions
29 lines (25 loc) · 845 Bytes
/
Copy pathwaitForIdleProcess.ts
File metadata and controls
29 lines (25 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import type { ChildProcess } from 'node:child_process';
const createWaitForIdle = (opts?: { delayInMs: number }) => {
const { delayInMs = 5000 } = opts || {};
let id;
let idler;
const waitForIdle = new Promise(resolve => {
idler = () => {
clearTimeout(id);
id = setTimeout(resolve, delayInMs);
};
idler();
});
return { idler, waitForIdle };
};
/**
* Accepts a ChildProcess, usually started using `run` or `execa.spawn` and
* returns a promise that resolves when the spawned process's stdout or stderr
* has been idle for delayInMs milliseconds.
*/
export const waitForIdleProcess = (cp: ChildProcess, opts?: Parameters<typeof createWaitForIdle>[0]) => {
const { idler, waitForIdle } = createWaitForIdle(opts);
cp.stdout?.on('data', idler);
cp.stderr?.on('data', idler);
return waitForIdle;
};