forked from aspnet/JavaScriptServices
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathports.ts
More file actions
24 lines (22 loc) · 888 Bytes
/
ports.ts
File metadata and controls
24 lines (22 loc) · 888 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
import * as portastic from 'portastic';
const pollInterval = 500;
export function waitUntilPortState(port: number, iface: string, isListening: boolean, timeoutMs: number, callback: (err: any) => void) {
if (!(timeoutMs > 0)) {
throw new Error(`Timed out waiting for port ${ port } to become ${ isListening ? 'in use' : 'free' }`);
}
portastic.test(port, iface).then(
actuallyIsAvailable => {
const actuallyIsListening = !actuallyIsAvailable;
if (actuallyIsListening === isListening) {
// Desired state is reached
callback(null);
} else {
// Wait longer
setTimeout(() => {
waitUntilPortState(port, iface, isListening, timeoutMs - pollInterval, callback);
}, pollInterval);
}
},
callback
)
}