@4players/odin-common
Version:
A collection of commonly used type definitions and utility functions across ODIN web projects
39 lines (38 loc) • 1.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Strand = void 0;
class Strand {
constructor(...values) {
this._Tasks = [];
this._Running = false;
this._Values = values;
}
enqueue(task) {
return new Promise((resolve, reject) => {
const wrapped = async () => {
try {
const result = await task(...this._Values);
resolve(result);
}
catch (error) {
reject(error);
}
};
this._Tasks.push(wrapped);
if (!this._Running) {
this.execute();
}
});
}
async execute() {
this._Running = true;
while (true) {
const task = this._Tasks.shift();
if (task === undefined)
break;
await task();
}
this._Running = false;
}
}
exports.Strand = Strand;