API Docs for: 3.10.3
Show:

File: timers/js/timers.js

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