45 lines
786 B
JavaScript
45 lines
786 B
JavaScript
const utils = require('./utils');
|
|
|
|
class Cond {
|
|
|
|
constructor() {
|
|
this.notifyCount = 0;
|
|
this.resolve = null;
|
|
this.timer = null;
|
|
}
|
|
|
|
async wait(timeout) {
|
|
return new Promise(function (resolve) {
|
|
if (this.notifyCount > 0) {
|
|
resolve();
|
|
this.reset();
|
|
return;
|
|
}
|
|
this.resolve = resolve;
|
|
this.timer = setTimeout(function () {
|
|
resolve();
|
|
this.reset();
|
|
}.bind(this), timeout);
|
|
}.bind(this));
|
|
}
|
|
|
|
notifyAll() {
|
|
if (this.timer != null && this.resolve) {
|
|
clearTimeout(this.timer());
|
|
this.resolve();
|
|
this.reset();
|
|
} else {
|
|
++this.notifyCount;
|
|
}
|
|
}
|
|
|
|
reset() {
|
|
this.notifyCount = 0;
|
|
this.resolve = null;
|
|
this.timer = null;
|
|
}
|
|
|
|
};
|
|
|
|
exports.Cond = Cond;
|