signal-promise
Advanced tools
Comparing version 1.0.1 to 1.0.2
24
index.js
@@ -7,6 +7,11 @@ module.exports = class Signal { | ||
this._bind = bind.bind(this) | ||
this._clear = clear.bind(this) | ||
this._timers = new Set() | ||
} | ||
wait (max) { | ||
if (!this._promise) this._promise = new Promise(this._bind) | ||
if (!this._promise) { | ||
this._promise = new Promise(this._bind) | ||
this._promise.then(this._clear).catch(this._clear) | ||
} | ||
if (max) return this._sleep(this._promise, max) | ||
@@ -17,11 +22,11 @@ return this._promise | ||
_sleep (p, max) { | ||
let id | ||
const clear = () => clearTimeout(id) | ||
const s = new Promise(resolve => { | ||
id = setTimeout(resolve, max, true) | ||
const done = () => { | ||
this._timers.delete(id) | ||
resolve(true) | ||
} | ||
const id = setTimeout(done, max) | ||
this._timers.add(id) | ||
}) | ||
p.then(clear).catch(clear) | ||
p.catch(clear) | ||
return Promise.race([s, p]) | ||
@@ -40,2 +45,7 @@ } | ||
function clear () { | ||
for (const id of this._timers) clearTimeout(id) | ||
this._timers.clear() | ||
} | ||
function bind (resolve, reject) { | ||
@@ -42,0 +52,0 @@ this._resolve = resolve |
{ | ||
"name": "signal-promise", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Simple wait/notify promise with optional max wait time", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3703
57