2022-01-24 17:38:41 +08:00

45 lines
783 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));
}
notify() {
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;