Created
March 18, 2020 17:08
-
-
Save iainireland/cefe20b704bc7ff7b8cbaad62818dcd0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Not-very-compliant polyfill for setTimeout, based on Promise. | |
// When this array becomes non-empty, call scheduleWork. | |
let timeouts = []; | |
// Fake clock used to order timeouts. | |
let now = 0; | |
// Least positive integer not yet used as a timer id. | |
let next_id = 1; | |
// Add an active timer record to timeouts. | |
function enqueue(id, t, cb) { | |
if (typeof cb !== 'function') { | |
let code = "" + cb; | |
cb = () => eval(code); | |
} | |
t += now; | |
timeouts.push({id, t, cb}); | |
timeouts.sort((a, b) => a.t - b.t); | |
if (timeouts.length === 1) { | |
scheduleWork(); | |
} | |
} | |
// Register a microtask to make sure timers are actually processed. There | |
// should be at most one of these microtasks pending at a time. | |
function scheduleWork() { | |
// Number of times to let the microtask queue spin before running a | |
// timeout. The idea is to let all Promises resolve between consecutive | |
// timer callbacks. Of course, this is a hack; whatever number is used | |
// here, an async function can simply await that many times plus one to | |
// observe that timeouts are being handled as microtasks, not events. | |
let delay = 10; | |
function handleTimeouts() { | |
if (--delay > 0) { | |
Promise.resolve().then(handleTimeouts); | |
return; | |
} | |
if (timeouts.length > 0) { | |
let {t, cb} = timeouts.shift(); | |
if (now < t) { | |
now = t; | |
} | |
if (timeouts.length > 0) { | |
scheduleWork(); // There's more to do after this. | |
} | |
cb(); | |
} | |
} | |
Promise.resolve().then(handleTimeouts); | |
} | |
this.fakeSetInterval = function (cb, t, ...args) { | |
let id = next_id++; | |
let func = () => { | |
enqueue(id, t, func); | |
cb(...args); | |
}; | |
enqueue(id, t, func); | |
return id; | |
}; | |
var intensity = 1; | |
var Color3 = function(r) { | |
this.r = r; | |
}; | |
Color3.prototype.scale = function(t) { | |
this.r = this.r * t; | |
}; | |
var groundColor = new Color3(0); | |
fakeSetInterval(function() { | |
groundColor.scale(intensity); | |
}, 0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment