UNPKG

concurrent-each

Version:

Async array operations to push tasks onto the macrotask queue to prevent UI lock up.

2 lines (1 loc) 2.36 kB
"use strict";var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};!function(e,t){if(!e.setImmediate){var n,a,s,o,i,c=1,l={},r=!1,d=e.document,f=Object.getPrototypeOf&&Object.getPrototypeOf(e);f=f&&f.setTimeout?f:e,"[object process]"==={}.toString.call(e.process)?n=function(e){process.nextTick((function(){u(e)}))}:!function(){if(e.postMessage&&!e.importScripts){var t=!0,n=e.onmessage;return e.onmessage=function(){t=!1},e.postMessage("","*"),e.onmessage=n,t}}()?e.MessageChannel?((s=new MessageChannel).port1.onmessage=function(e){u(e.data)},n=function(e){s.port2.postMessage(e)}):d&&"onreadystatechange"in d.createElement("script")?(a=d.documentElement,n=function(e){var t=d.createElement("script");t.onreadystatechange=function(){u(e),t.onreadystatechange=null,a.removeChild(t),t=null},a.appendChild(t)}):n=function(e){setTimeout(u,0,e)}:(o="setImmediate$"+Math.random()+"$",i=function(t){t.source===e&&"string"==typeof t.data&&0===t.data.indexOf(o)&&u(+t.data.slice(o.length))},e.addEventListener?e.addEventListener("message",i,!1):e.attachEvent("onmessage",i),n=function(t){e.postMessage(o+t,"*")}),f.setImmediate=function(e){"function"!=typeof e&&(e=new Function(""+e));for(var t=new Array(arguments.length-1),a=0;a<t.length;a++)t[a]=arguments[a+1];var s={callback:e,args:t};return l[c]=s,n(c),c++},f.clearImmediate=m}function m(e){delete l[e]}function u(e){if(r)setTimeout(u,0,e);else{var t=l[e];if(t){r=!0;try{!function(e){var t=e.callback,n=e.args;switch(n.length){case 0:t();break;case 1:t(n[0]);break;case 2:t(n[0],n[1]);break;case 3:t(n[0],n[1],n[2]);break;default:t.apply(void 0,n)}}(t)}finally{m(e),r=!1}}}}}("undefined"==typeof self?e:self);var t={map:(e,t,n)=>(n||(n=Math.ceil(e.length/1e3)),new Promise((a=>{const s=[];let o=0;const i=()=>{o<e.length-n&&setImmediate(i),s.push(...e.slice(o,o+n).map(t)),o+=n,o>=e.length&&a(s)};setImmediate(i)}))),forEach:(e,t,n)=>(n||(n=Math.ceil(e.length/1e3)),new Promise((a=>{let s=0;const o=()=>{s<e.length-n&&setImmediate(o),e.slice(s,s+n).forEach(t),s+=n,s>=e.length&&a()};setImmediate(o)}))),reduce:(e,t,n,a)=>(a||(a=Math.ceil(e.length/1e3)),new Promise((s=>{let o=0,i=n;const c=()=>{o<e.length-a&&setImmediate(c),i=e.slice(o,o+a).reduce(t,i),o+=a,o>=e.length&&s(i)};setImmediate(c)})))};module.exports=t;