Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Queue {
- constructor (id) {
- this.id = id;
- this.weight = 0;
- this.isClosed = false;
- this._p = Promise.resolve();
- }
- add (weight, task) {
- this.weight += weight;
- this._p = this._p
- .then(() => {
- if (this.isClosed) return;
- log(`Queue #${this.id}: task is started`);
- return task();
- })
- .catch(log)
- .then(() => {
- this.weight -= weight;
- if (this.isClosed) return;
- log(`Queue #${this.id}: task is finished`);
- });
- }
- close () {
- this.isClosed = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement