tahamajs's picture
download
raw
1.74 kB
// @flow strict
import assert from 'assert';
export type TaskID = number; // can't mark opaque due to https://github.com/flowtype/flow-remove-types/pull/61
type Task = {
callback: (timeStamp: number) => void;
id: TaskID;
cancelled: boolean;
};
class TaskQueue {
_queue: Array<Task>;
_id: TaskID;
_cleared: boolean;
_currentlyRunning: Array<Task> | false;
constructor() {
this._queue = [];
this._id = 0;
this._cleared = false;
this._currentlyRunning = false;
}
add(callback: (timeStamp: number) => void): TaskID {
const id = ++this._id;
const queue = this._queue;
queue.push({callback, id, cancelled: false});
return id;
}
remove(id: TaskID) {
const running = this._currentlyRunning;
const queue = running ? this._queue.concat(running) : this._queue;
for (const task of queue) {
if (task.id === id) {
task.cancelled = true;
return;
}
}
}
run(timeStamp: number = 0) {
assert(!this._currentlyRunning);
const queue = this._currentlyRunning = this._queue;
// Tasks queued by callbacks in the current queue should be executed
// on the next run, not the current run.
this._queue = [];
for (const task of queue) {
if (task.cancelled) continue;
task.callback(timeStamp);
if (this._cleared) break;
}
this._cleared = false;
this._currentlyRunning = false;
}
clear() {
if (this._currentlyRunning) {
this._cleared = true;
}
this._queue = [];
}
}
export default TaskQueue;

Xet Storage Details

Size:
1.74 kB
·
Xet hash:
41bb74b400b52314f6ec001096e8677f5463fa816e1c92ad46606ab6e8528d40

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.