41 lines
872 B
JavaScript
41 lines
872 B
JavaScript
const utils = require('./utils');
|
|
|
|
class Cond {
|
|
|
|
constructor() {
|
|
this.waitList = [];
|
|
}
|
|
|
|
async wait(timeout) {
|
|
return new Promise((resolve) => {
|
|
const waitIdx = this.waitList.length;
|
|
this.waitList.push({
|
|
'resolve': resolve,
|
|
'timer': setTimeout(() => {
|
|
if (waitIdx < this.waitList.length &&
|
|
this.waitList[waitIdx]['resolve'] == resolve) {
|
|
this.waitList = this.waitList.splice(waitIdx, 1);
|
|
} else {
|
|
utils.throwError('cond waitIdx error');
|
|
}
|
|
resolve();
|
|
}, timeout)
|
|
});
|
|
});
|
|
}
|
|
|
|
notifyAll() {
|
|
if (this.waitList.length > 0) {
|
|
const waitListCopy = this.waitList;
|
|
this.waitList = [];
|
|
waitListCopy.forEach((v) => {
|
|
clearTimeout(v['timer']);
|
|
v['resolve']();
|
|
});
|
|
}
|
|
}
|
|
|
|
};
|
|
|
|
exports.Cond = Cond;
|