Skip to content

Instantly share code, notes, and snippets.

@lyleunderwood
Created October 11, 2019 14:52
Show Gist options
  • Save lyleunderwood/a2cb4473796053d9d67c7195599bd734 to your computer and use it in GitHub Desktop.
Save lyleunderwood/a2cb4473796053d9d67c7195599bd734 to your computer and use it in GitHub Desktop.
type Operation = {
behavior: Function,
timeAdded: integer,
msDelay: integer,
};
class OperationQueue {
operations: Operation[] = [];
operationsForTime(time) {
return this.operations.filter(operation => {
return (operation.timeAdded + (operation.msDelay || 0)) <= time;
});
}
push(operation) {
this.operations.push(operation);
}
}
JavascriptEngine.operationQueue = new OperationQueue();
// JavascriptEngine.operationQueue.push can be called by
// things like setTimeout and events firing
while (true) {
// start of tick
operationQueue.operationsForTime(new Date().getTime()).forEach(operation => {
operation.behavior();
});
// end of tick
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment