schedulers-fix.js 1.5 KB

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. var global = require('../internals/global');
  3. var apply = require('../internals/function-apply');
  4. var isCallable = require('../internals/is-callable');
  5. var ENGINE_IS_BUN = require('../internals/engine-is-bun');
  6. var USER_AGENT = require('../internals/engine-user-agent');
  7. var arraySlice = require('../internals/array-slice');
  8. var validateArgumentsLength = require('../internals/validate-arguments-length');
  9. var Function = global.Function;
  10. // dirty IE9- and Bun 0.3.0- checks
  11. var WRAP = /MSIE .\./.test(USER_AGENT) || ENGINE_IS_BUN && (function () {
  12. var version = global.Bun.version.split('.');
  13. return version.length < 3 || version[0] === '0' && (version[1] < 3 || version[1] === '3' && version[2] === '0');
  14. })();
  15. // IE9- / Bun 0.3.0- setTimeout / setInterval / setImmediate additional parameters fix
  16. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timers
  17. // https://github.com/oven-sh/bun/issues/1633
  18. module.exports = function (scheduler, hasTimeArg) {
  19. var firstParamIndex = hasTimeArg ? 2 : 1;
  20. return WRAP ? function (handler, timeout /* , ...arguments */) {
  21. var boundArgs = validateArgumentsLength(arguments.length, 1) > firstParamIndex;
  22. var fn = isCallable(handler) ? handler : Function(handler);
  23. var params = boundArgs ? arraySlice(arguments, firstParamIndex) : [];
  24. var callback = boundArgs ? function () {
  25. apply(fn, this, params);
  26. } : fn;
  27. return hasTimeArg ? scheduler(callback, timeout) : scheduler(callback);
  28. } : scheduler;
  29. };