aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/core/shim/timers.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/montage/core/shim/timers.js')
-rwxr-xr-xnode_modules/montage/core/shim/timers.js87
1 files changed, 87 insertions, 0 deletions
diff --git a/node_modules/montage/core/shim/timers.js b/node_modules/montage/core/shim/timers.js
new file mode 100755
index 00000000..859a7aec
--- /dev/null
+++ b/node_modules/montage/core/shim/timers.js
@@ -0,0 +1,87 @@
1/* <copyright>
2 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4 (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5 </copyright> */
6
7 /**
8 Defines [setImmediate()]{@link setImmediate} and [clearImmediate()]{@link clearImmediate} shim functions.
9 @see setImmediate
10 @see clearImmediate
11 @module montage/core/shim/timers
12 */
13
14/**
15 @function
16 @name setImmediate
17 @global
18*/
19
20/**
21 @function
22 @name clearImmediate
23 @global
24*/
25
26(function (global) {
27
28var nextTick;
29if (typeof process !== "undefined") {
30 nextTick = process.nextTick;
31} else if (typeof MessageChannel !== "undefined") {
32 // http://www.nonblocking.io/2011/06/windownexttick.html
33 var channel = new MessageChannel();
34 // linked list of tasks (single, with head node)
35 var head = {}, tail = head;
36
37 channel.port1.onmessage = function () {
38 var next = head.next;
39 var task = next.task;
40 head = next;
41 task();
42 };
43
44 nextTick = function (task) {
45 tail = tail.next = {task: task};
46 channel.port2.postMessage(void 0);
47 }
48} else if (typeof setTimeout !== "undefined") {
49
50 nextTick = function (callback) {
51 setTimeout(callback, 0);
52 };
53} else {
54 throw new Error("Can't shim setImmediate.");
55}
56
57if (typeof setImmediate === "undefined") {
58 var nextHandle = 0;
59 var handles = {};
60
61
62 global.setImmediate = function setImmediate(callback) {
63 var handle = nextHandle++;
64 var args = arguments.length > 1 ?
65 Array.prototype.slice.call(arguments, 1) :
66 void 0;
67 handles[handle] = true;
68 nextTick(function () {
69 if (handles[handle]) {
70 callback.apply(void 0, args);
71 delete handles[handle];
72 }
73 });
74 return handle;
75 };
76
77 global.clearImmediate = function clearImmediate(handle) {
78 delete handles[handle];
79 };
80}
81
82// Make this work as a <script> for bootstrapping montage.js
83if (typeof bootstrap !== "undefined") {
84 bootstrap("core/shim/timers", function () {});
85}
86
87})(typeof global === "undefined" ? window : global);