diff options
author | Pierre Frisch | 2011-12-22 07:25:50 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-01-27 11:18:17 -0800 |
commit | b89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch) | |
tree | 0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /node_modules/montage/core/shim/timers.js | |
parent | 2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff) | |
download | ninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz |
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'node_modules/montage/core/shim/timers.js')
-rwxr-xr-x | node_modules/montage/core/shim/timers.js | 87 |
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 | |||
28 | var nextTick; | ||
29 | if (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 | |||
57 | if (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 | ||
83 | if (typeof bootstrap !== "undefined") { | ||
84 | bootstrap("core/shim/timers", function () {}); | ||
85 | } | ||
86 | |||
87 | })(typeof global === "undefined" ? window : global); | ||