dev21w45-a

This commit is contained in:
Victor Westerlund 2021-11-11 15:36:48 +01:00
parent 22eda97800
commit 9de4811adf

View file

@ -17,32 +17,31 @@ class Monkey {
// Task scheduler // Task scheduler
next() { next() {
if(this.flags[0] === 0 || this.flags[2] === 0) return this.abort(); const start = performance.now();
const task = this.tasks[this.i]; const self = this;
let task = null;
// Run task after delay // Run task after delay
this.queue.thisTask = setTimeout(() => { function frame(time) {
// Dispatch task to main thread if(self.flags[0] === 0 || self.flags[2] === 0) return self.abort();
postMessage(["TASK",task]); postMessage(["TASK",task]);
this.i++; self.i++;
},task[0]); scheduleFrame(time);
// Loop until flag is 0 or infinite if 255
if(this.i === this.tasksLength) {
this.i = -1;
if(this.flags[1] < 255) this.flags[2]--;
} }
// Queue the next task // Queue the next task
this.queue.nextTask = setTimeout(() => this.next(),task[0]); function scheduleFrame(time) {
task = self.tasks[self.i];
//const elapsed = Math.round(performance.now() - start);
const wait = task[0] + start;
setTimeout(() => requestAnimationFrame(frame),wait);
}
scheduleFrame(start);
} }
abort() { abort() {
this.flags[2] = 0; // Playing: false this.flags[2] = 0; // Playing: false
clearTimeout(this.queue.thisTask);
clearTimeout(this.queue.nextTask);
this.queue.thisTask = null;
this.queue.nextTask = null;
} }
// Set or get a runtime flag // Set or get a runtime flag