зеркало из https://github.com/mozilla/pluotsorbet.git
Merge pull request #918 from yxl/setZeroTimeout
Reimplement setZeroTimeout with Promise.
This commit is contained in:
Коммит
d60f51d940
|
@ -18,6 +18,7 @@
|
|||
<!--<script type="text/javascript" src="build/shumway.js" defer></script>-->
|
||||
<!--<script type="text/javascript" src="libs/terminal.js" defer></script>-->
|
||||
<script type="text/javascript" src="libs/console.js" defer></script>
|
||||
<script type="text/javascript" src="libs/promise-6.0.0.js" defer></script>
|
||||
<script type="text/javascript" src="legacy.js" defer></script>
|
||||
<script type="text/javascript" src="blackBox.js" defer></script>
|
||||
<script type="text/javascript" src="timer.js" defer></script>
|
||||
|
@ -39,7 +40,6 @@
|
|||
<script type="text/javascript" src="libs/jsbn/jsbn.js" defer></script>
|
||||
<script type="text/javascript" src="libs/jsbn/jsbn2.js" defer></script>
|
||||
<script type="text/javascript" src="libs/contacts.js" defer></script>
|
||||
<script type="text/javascript" src="libs/promise-6.0.0.js" defer></script>
|
||||
<script type="text/javascript" src="libs/pipe.js" defer></script>
|
||||
<script type="text/javascript" src="libs/contact2vcard.js" defer></script>
|
||||
<script type="text/javascript" src="libs/emoji.js" defer></script>
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="initial-scale=1.0">
|
||||
<script type="text/javascript" src="../../libs/fs.js" defer></script>
|
||||
<script type="text/javascript" src="../../libs/promise-6.0.0.js" defer></script>
|
||||
<script type="text/javascript" src="../../timer.js" defer></script>
|
||||
<script type="text/javascript" src="../qunit/core.js" defer></script>
|
||||
<script type="text/javascript" src="../qunit/equiv.js" defer></script>
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<script type="text/javascript" src="../../libs/promise-6.0.0.js" defer></script>
|
||||
<script type="text/javascript" src="../../timer.js" defer></script>
|
||||
<script type="text/javascript" src="../../libs/load.js" defer></script>
|
||||
<script type="text/javascript" src="fs-v1.js" defer></script>
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<script type="text/javascript" src="../../libs/promise-6.0.0.js" defer></script>
|
||||
<script type="text/javascript" src="../../timer.js" defer></script>
|
||||
<script type="text/javascript" src="../../libs/load.js" defer></script>
|
||||
<script type="text/javascript" src="fs-v2.js" defer></script>
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<script type="text/javascript" src="../../libs/promise-6.0.0.js" defer></script>
|
||||
<script type="text/javascript" src="../../timer.js" defer></script>
|
||||
<script type="text/javascript" src="../../libs/load.js" defer></script>
|
||||
<script type="text/javascript" src="fs-v2.js" defer></script>
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="initial-scale=1.0">
|
||||
<script type="text/javascript" src="../../libs/fs.js" defer></script>
|
||||
<script type="text/javascript" src="../../libs/promise-6.0.0.js" defer></script>
|
||||
<script type="text/javascript" src="../../timer.js" defer></script>
|
||||
<script type="text/javascript" src="test-fs-harness.js" defer></script>
|
||||
<script type="text/javascript" src="test-fs-init.js" defer></script>
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<script type="text/javascript" src="../../libs/promise-6.0.0.js" defer></script>
|
||||
<script type="text/javascript" src="../../timer.js" defer></script>
|
||||
<script type="text/javascript" src="../../libs/fs.js" defer></script>
|
||||
<script type="text/javascript" src="test-fs-harness.js" defer></script>
|
||||
|
|
18
timer.js
18
timer.js
|
@ -6,29 +6,15 @@
|
|||
// Only add setZeroTimeout to the window object, and hide everything
|
||||
// else in a closure.
|
||||
(function() {
|
||||
var timeouts = [];
|
||||
var messageName = "zero-timeout-message";
|
||||
var resolved = Promise.resolve();
|
||||
|
||||
// Like setTimeout, but only takes a function argument. There's
|
||||
// no time argument (always zero) and no arguments (you have to
|
||||
// use a closure).
|
||||
function setZeroTimeout(fn) {
|
||||
timeouts.push(fn);
|
||||
window.postMessage(messageName, "*");
|
||||
resolved.then(fn);
|
||||
}
|
||||
|
||||
function handleMessage(event) {
|
||||
if (event.source == window && event.data == messageName) {
|
||||
event.stopPropagation();
|
||||
if (timeouts.length > 0) {
|
||||
var fn = timeouts.shift();
|
||||
fn();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener("message", handleMessage, true);
|
||||
|
||||
// Add the one thing we want added to the window object.
|
||||
window.setZeroTimeout = setZeroTimeout;
|
||||
})();
|
||||
|
|
Загрузка…
Ссылка в новой задаче