Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

task.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. 'use strict';
  2. var globalThis = require('../internals/global-this');
  3. var apply = require('../internals/function-apply');
  4. var bind = require('../internals/function-bind-context');
  5. var isCallable = require('../internals/is-callable');
  6. var hasOwn = require('../internals/has-own-property');
  7. var fails = require('../internals/fails');
  8. var html = require('../internals/html');
  9. var arraySlice = require('../internals/array-slice');
  10. var createElement = require('../internals/document-create-element');
  11. var validateArgumentsLength = require('../internals/validate-arguments-length');
  12. var IS_IOS = require('../internals/environment-is-ios');
  13. var IS_NODE = require('../internals/environment-is-node');
  14. var set = globalThis.setImmediate;
  15. var clear = globalThis.clearImmediate;
  16. var process = globalThis.process;
  17. var Dispatch = globalThis.Dispatch;
  18. var Function = globalThis.Function;
  19. var MessageChannel = globalThis.MessageChannel;
  20. var String = globalThis.String;
  21. var counter = 0;
  22. var queue = {};
  23. var ONREADYSTATECHANGE = 'onreadystatechange';
  24. var $location, defer, channel, port;
  25. fails(function () {
  26. // Deno throws a ReferenceError on `location` access without `--location` flag
  27. $location = globalThis.location;
  28. });
  29. var run = function (id) {
  30. if (hasOwn(queue, id)) {
  31. var fn = queue[id];
  32. delete queue[id];
  33. fn();
  34. }
  35. };
  36. var runner = function (id) {
  37. return function () {
  38. run(id);
  39. };
  40. };
  41. var eventListener = function (event) {
  42. run(event.data);
  43. };
  44. var globalPostMessageDefer = function (id) {
  45. // old engines have not location.origin
  46. globalThis.postMessage(String(id), $location.protocol + '//' + $location.host);
  47. };
  48. // Node.js 0.9+ & IE10+ has setImmediate, otherwise:
  49. if (!set || !clear) {
  50. set = function setImmediate(handler) {
  51. validateArgumentsLength(arguments.length, 1);
  52. var fn = isCallable(handler) ? handler : Function(handler);
  53. var args = arraySlice(arguments, 1);
  54. queue[++counter] = function () {
  55. apply(fn, undefined, args);
  56. };
  57. defer(counter);
  58. return counter;
  59. };
  60. clear = function clearImmediate(id) {
  61. delete queue[id];
  62. };
  63. // Node.js 0.8-
  64. if (IS_NODE) {
  65. defer = function (id) {
  66. process.nextTick(runner(id));
  67. };
  68. // Sphere (JS game engine) Dispatch API
  69. } else if (Dispatch && Dispatch.now) {
  70. defer = function (id) {
  71. Dispatch.now(runner(id));
  72. };
  73. // Browsers with MessageChannel, includes WebWorkers
  74. // except iOS - https://github.com/zloirock/core-js/issues/624
  75. } else if (MessageChannel && !IS_IOS) {
  76. channel = new MessageChannel();
  77. port = channel.port2;
  78. channel.port1.onmessage = eventListener;
  79. defer = bind(port.postMessage, port);
  80. // Browsers with postMessage, skip WebWorkers
  81. // IE8 has postMessage, but it's sync & typeof its postMessage is 'object'
  82. } else if (
  83. globalThis.addEventListener &&
  84. isCallable(globalThis.postMessage) &&
  85. !globalThis.importScripts &&
  86. $location && $location.protocol !== 'file:' &&
  87. !fails(globalPostMessageDefer)
  88. ) {
  89. defer = globalPostMessageDefer;
  90. globalThis.addEventListener('message', eventListener, false);
  91. // IE8-
  92. } else if (ONREADYSTATECHANGE in createElement('script')) {
  93. defer = function (id) {
  94. html.appendChild(createElement('script'))[ONREADYSTATECHANGE] = function () {
  95. html.removeChild(this);
  96. run(id);
  97. };
  98. };
  99. // Rest old browsers
  100. } else {
  101. defer = function (id) {
  102. setTimeout(runner(id), 0);
  103. };
  104. }
  105. }
  106. module.exports = {
  107. set: set,
  108. clear: clear
  109. };