@4players/odin-common
Version:
A collection of commonly used type definitions and utility functions across ODIN web projects
35 lines (34 loc) • 890 B
JavaScript
export 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;
}
}