Wait for port to be freed before continuing

This commit is contained in:
SteveSandersonMS
2016-12-15 20:25:20 +00:00
parent 200d80627c
commit 953f370336
3 changed files with 49 additions and 5 deletions

View File

@@ -0,0 +1,23 @@
import * as portastic from 'portastic';
const pollInterval = 500;
export function waitUntilPortState(port: number, isOpen: boolean, timeoutMs: number, callback: (err: any) => void) {
if (!(timeoutMs > 0)) {
throw new Error(`Timed out after ${ timeoutMs }ms waiting for port ${ port } to become ${ isOpen ? 'free' : 'in use' }`);
}
portastic.test(port).then(
actualIsOpenState => {
if (actualIsOpenState === isOpen) {
// Desired state is reached
callback(null);
} else {
// Wait longer
setTimeout(() => {
waitUntilPortState(port, isOpen, timeoutMs - pollInterval, callback);
}, pollInterval);
}
},
callback
)
}