|
1 /* |
|
2 YUI 3.10.3 (build 2fb5187) |
|
3 Copyright 2013 Yahoo! Inc. All rights reserved. |
|
4 Licensed under the BSD License. |
|
5 http://yuilibrary.com/license/ |
|
6 */ |
|
7 |
|
8 YUI.add('timers', function (Y, NAME) { |
|
9 |
|
10 /** |
|
11 * Provides utilities for timed asynchronous callback execution. |
|
12 * Y.soon is a setImmediate/process.nextTick/setTimeout wrapper. |
|
13 * @module timers |
|
14 * @author Steven Olmsted |
|
15 */ |
|
16 |
|
17 var YGLOBAL = Y.config.global, |
|
18 |
|
19 /** |
|
20 * Y.soon accepts a callback function. The callback function will be called |
|
21 * once in a future turn of the JavaScript event loop. If the function |
|
22 * requires a specific execution context or arguments, wrap it with Y.bind. |
|
23 * Y.soon returns an object with a cancel method. If the cancel method is |
|
24 * called before the callback function, the callback function won't be |
|
25 * called. |
|
26 * @method soon |
|
27 * @for YUI |
|
28 * @param {Function} callbackFunction |
|
29 * @return {Object} An object with a cancel method. If the cancel method is |
|
30 * called before the callback function, the callback function won't be |
|
31 * called. |
|
32 */ |
|
33 soon = function (callbackFunction) { |
|
34 var canceled; |
|
35 |
|
36 soon._asynchronizer(function () { |
|
37 // Some asynchronizers may provide their own cancellation |
|
38 // methods such as clearImmediate or clearTimeout but some |
|
39 // asynchronizers do not. For simplicity, cancellation is |
|
40 // entirely handled here rather than wrapping the other methods. |
|
41 // All asynchronizers are expected to always call this anonymous |
|
42 // function. |
|
43 if (!canceled) { |
|
44 callbackFunction(); |
|
45 } |
|
46 }); |
|
47 |
|
48 return { |
|
49 cancel: function () { |
|
50 canceled = 1; |
|
51 } |
|
52 }; |
|
53 }; |
|
54 |
|
55 /** |
|
56 * The asynchronizer is the internal mechanism which will call a function |
|
57 * asynchronously. This property is exposed as a convenient way to define a |
|
58 * different asynchronizer implementation without having to rewrite the |
|
59 * entire Y.soon interface. |
|
60 * @method _asynchronizer |
|
61 * @for soon |
|
62 * @param {Function} callbackFunction The function to call asynchronously. |
|
63 * @protected |
|
64 */ |
|
65 |
|
66 /** |
|
67 * Since Y.soon is likely to have many differing asynchronizer |
|
68 * implementations, this property should be set to identify which |
|
69 * implementation is in use. |
|
70 * @property _impl |
|
71 * @protected |
|
72 * @type String |
|
73 */ |
|
74 |
|
75 // Check for a native or already polyfilled implementation of setImmediate. |
|
76 if ('setImmediate' in YGLOBAL) { |
|
77 soon._asynchronizer = function (callbackFunction) { |
|
78 setImmediate(callbackFunction); |
|
79 }; |
|
80 soon._impl = 'setImmediate'; |
|
81 } |
|
82 |
|
83 // Check for process and process.nextTick |
|
84 else if (('process' in YGLOBAL) && ('nextTick' in process)) { |
|
85 soon._asynchronizer = process.nextTick; |
|
86 soon._impl = 'nextTick'; |
|
87 } |
|
88 |
|
89 // The most widely supported asynchronizer is setTimeout so we use that as |
|
90 // the fallback. |
|
91 else { |
|
92 soon._asynchronizer = function (callbackFunction) { |
|
93 setTimeout(callbackFunction, 0); |
|
94 }; |
|
95 soon._impl = 'setTimeout'; |
|
96 } |
|
97 |
|
98 Y.soon = soon; |
|
99 |
|
100 |
|
101 }, '3.10.3', {"requires": ["yui-base"]}); |