Created
February 14, 2017 19:14
-
-
Save mattpodwysocki/9839fa6d54673aa6a766d3f6a7a7e927 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
(function (global) { | |
'use strict'; | |
if (global.setTimeout) { return; } | |
let nextId = 1; | |
let tasksById = {}; | |
function setTimeout(fn: () => void, ms: number) { | |
tasksById[nextId] = fn; | |
registerSetTimeout(nextId, ms); | |
return nextId++; | |
} | |
function clearTimeout(id: number) { | |
delete tasksById[id]; | |
} | |
function runIfPresent(id: number) { | |
let task = tasksById[id]; | |
if (task) { | |
try { | |
task(); | |
} finally { | |
clearTimeout(id); | |
} | |
} | |
} | |
function registerSetTimeout(id: number, ms: number) { | |
global.runInBackground(() => { | |
global.pause(ms); | |
runIfPresent(id); | |
}); | |
} | |
global.setTimeout = setTimeout; | |
global.clearTimeout = clearTimeout; | |
}(control)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment