index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. module.exports = (function() {
  2. var __MODS__ = {};
  3. var __DEFINE__ = function(modId, func, req) { var m = { exports: {}, _tempexports: {} }; __MODS__[modId] = { status: 0, func: func, req: req, m: m }; };
  4. var __REQUIRE__ = function(modId, source) { if(!__MODS__[modId]) return require(source); if(!__MODS__[modId].status) { var m = __MODS__[modId].m; m._exports = m._tempexports; var desp = Object.getOwnPropertyDescriptor(m, "exports"); if (desp && desp.configurable) Object.defineProperty(m, "exports", { set: function (val) { if(typeof val === "object" && val !== m._exports) { m._exports.__proto__ = val.__proto__; Object.keys(val).forEach(function (k) { m._exports[k] = val[k]; }); } m._tempexports = val }, get: function () { return m._tempexports; } }); __MODS__[modId].status = 1; __MODS__[modId].func(__MODS__[modId].req, m, m.exports); } return __MODS__[modId].m.exports; };
  5. var __REQUIRE_WILDCARD__ = function(obj) { if(obj && obj.__esModule) { return obj; } else { var newObj = {}; if(obj != null) { for(var k in obj) { if (Object.prototype.hasOwnProperty.call(obj, k)) newObj[k] = obj[k]; } } newObj.default = obj; return newObj; } };
  6. var __REQUIRE_DEFAULT__ = function(obj) { return obj && obj.__esModule ? obj.default : obj; };
  7. __DEFINE__(1662612701689, function(require, module, exports) {
  8. module.exports =
  9. {
  10. parallel : require('./parallel.js'),
  11. serial : require('./serial.js'),
  12. serialOrdered : require('./serialOrdered.js')
  13. };
  14. }, function(modId) {var map = {"./parallel.js":1662612701690,"./serial.js":1662612701697,"./serialOrdered.js":1662612701698}; return __REQUIRE__(map[modId], modId); })
  15. __DEFINE__(1662612701690, function(require, module, exports) {
  16. var iterate = require('./lib/iterate.js')
  17. , initState = require('./lib/state.js')
  18. , terminator = require('./lib/terminator.js')
  19. ;
  20. // Public API
  21. module.exports = parallel;
  22. /**
  23. * Runs iterator over provided array elements in parallel
  24. *
  25. * @param {array|object} list - array or object (named list) to iterate over
  26. * @param {function} iterator - iterator to run
  27. * @param {function} callback - invoked when all elements processed
  28. * @returns {function} - jobs terminator
  29. */
  30. function parallel(list, iterator, callback)
  31. {
  32. var state = initState(list);
  33. while (state.index < (state['keyedList'] || list).length)
  34. {
  35. iterate(list, iterator, state, function(error, result)
  36. {
  37. if (error)
  38. {
  39. callback(error, result);
  40. return;
  41. }
  42. // looks like it's the last one
  43. if (Object.keys(state.jobs).length === 0)
  44. {
  45. callback(null, state.results);
  46. return;
  47. }
  48. });
  49. state.index++;
  50. }
  51. return terminator.bind(state, callback);
  52. }
  53. }, function(modId) { var map = {"./lib/iterate.js":1662612701691,"./lib/state.js":1662612701695,"./lib/terminator.js":1662612701696}; return __REQUIRE__(map[modId], modId); })
  54. __DEFINE__(1662612701691, function(require, module, exports) {
  55. var async = require('./async.js')
  56. , abort = require('./abort.js')
  57. ;
  58. // API
  59. module.exports = iterate;
  60. /**
  61. * Iterates over each job object
  62. *
  63. * @param {array|object} list - array or object (named list) to iterate over
  64. * @param {function} iterator - iterator to run
  65. * @param {object} state - current job status
  66. * @param {function} callback - invoked when all elements processed
  67. */
  68. function iterate(list, iterator, state, callback)
  69. {
  70. // store current index
  71. var key = state['keyedList'] ? state['keyedList'][state.index] : state.index;
  72. state.jobs[key] = runJob(iterator, key, list[key], function(error, output)
  73. {
  74. // don't repeat yourself
  75. // skip secondary callbacks
  76. if (!(key in state.jobs))
  77. {
  78. return;
  79. }
  80. // clean up jobs
  81. delete state.jobs[key];
  82. if (error)
  83. {
  84. // don't process rest of the results
  85. // stop still active jobs
  86. // and reset the list
  87. abort(state);
  88. }
  89. else
  90. {
  91. state.results[key] = output;
  92. }
  93. // return salvaged results
  94. callback(error, state.results);
  95. });
  96. }
  97. /**
  98. * Runs iterator over provided job element
  99. *
  100. * @param {function} iterator - iterator to invoke
  101. * @param {string|number} key - key/index of the element in the list of jobs
  102. * @param {mixed} item - job description
  103. * @param {function} callback - invoked after iterator is done with the job
  104. * @returns {function|mixed} - job abort function or something else
  105. */
  106. function runJob(iterator, key, item, callback)
  107. {
  108. var aborter;
  109. // allow shortcut if iterator expects only two arguments
  110. if (iterator.length == 2)
  111. {
  112. aborter = iterator(item, async(callback));
  113. }
  114. // otherwise go with full three arguments
  115. else
  116. {
  117. aborter = iterator(item, key, async(callback));
  118. }
  119. return aborter;
  120. }
  121. }, function(modId) { var map = {"./async.js":1662612701692,"./abort.js":1662612701694}; return __REQUIRE__(map[modId], modId); })
  122. __DEFINE__(1662612701692, function(require, module, exports) {
  123. var defer = require('./defer.js');
  124. // API
  125. module.exports = async;
  126. /**
  127. * Runs provided callback asynchronously
  128. * even if callback itself is not
  129. *
  130. * @param {function} callback - callback to invoke
  131. * @returns {function} - augmented callback
  132. */
  133. function async(callback)
  134. {
  135. var isAsync = false;
  136. // check if async happened
  137. defer(function() { isAsync = true; });
  138. return function async_callback(err, result)
  139. {
  140. if (isAsync)
  141. {
  142. callback(err, result);
  143. }
  144. else
  145. {
  146. defer(function nextTick_callback()
  147. {
  148. callback(err, result);
  149. });
  150. }
  151. };
  152. }
  153. }, function(modId) { var map = {"./defer.js":1662612701693}; return __REQUIRE__(map[modId], modId); })
  154. __DEFINE__(1662612701693, function(require, module, exports) {
  155. module.exports = defer;
  156. /**
  157. * Runs provided function on next iteration of the event loop
  158. *
  159. * @param {function} fn - function to run
  160. */
  161. function defer(fn)
  162. {
  163. var nextTick = typeof setImmediate == 'function'
  164. ? setImmediate
  165. : (
  166. typeof process == 'object' && typeof process.nextTick == 'function'
  167. ? process.nextTick
  168. : null
  169. );
  170. if (nextTick)
  171. {
  172. nextTick(fn);
  173. }
  174. else
  175. {
  176. setTimeout(fn, 0);
  177. }
  178. }
  179. }, function(modId) { var map = {}; return __REQUIRE__(map[modId], modId); })
  180. __DEFINE__(1662612701694, function(require, module, exports) {
  181. // API
  182. module.exports = abort;
  183. /**
  184. * Aborts leftover active jobs
  185. *
  186. * @param {object} state - current state object
  187. */
  188. function abort(state)
  189. {
  190. Object.keys(state.jobs).forEach(clean.bind(state));
  191. // reset leftover jobs
  192. state.jobs = {};
  193. }
  194. /**
  195. * Cleans up leftover job by invoking abort function for the provided job id
  196. *
  197. * @this state
  198. * @param {string|number} key - job id to abort
  199. */
  200. function clean(key)
  201. {
  202. if (typeof this.jobs[key] == 'function')
  203. {
  204. this.jobs[key]();
  205. }
  206. }
  207. }, function(modId) { var map = {}; return __REQUIRE__(map[modId], modId); })
  208. __DEFINE__(1662612701695, function(require, module, exports) {
  209. // API
  210. module.exports = state;
  211. /**
  212. * Creates initial state object
  213. * for iteration over list
  214. *
  215. * @param {array|object} list - list to iterate over
  216. * @param {function|null} sortMethod - function to use for keys sort,
  217. * or `null` to keep them as is
  218. * @returns {object} - initial state object
  219. */
  220. function state(list, sortMethod)
  221. {
  222. var isNamedList = !Array.isArray(list)
  223. , initState =
  224. {
  225. index : 0,
  226. keyedList: isNamedList || sortMethod ? Object.keys(list) : null,
  227. jobs : {},
  228. results : isNamedList ? {} : [],
  229. size : isNamedList ? Object.keys(list).length : list.length
  230. }
  231. ;
  232. if (sortMethod)
  233. {
  234. // sort array keys based on it's values
  235. // sort object's keys just on own merit
  236. initState.keyedList.sort(isNamedList ? sortMethod : function(a, b)
  237. {
  238. return sortMethod(list[a], list[b]);
  239. });
  240. }
  241. return initState;
  242. }
  243. }, function(modId) { var map = {}; return __REQUIRE__(map[modId], modId); })
  244. __DEFINE__(1662612701696, function(require, module, exports) {
  245. var abort = require('./abort.js')
  246. , async = require('./async.js')
  247. ;
  248. // API
  249. module.exports = terminator;
  250. /**
  251. * Terminates jobs in the attached state context
  252. *
  253. * @this AsyncKitState#
  254. * @param {function} callback - final callback to invoke after termination
  255. */
  256. function terminator(callback)
  257. {
  258. if (!Object.keys(this.jobs).length)
  259. {
  260. return;
  261. }
  262. // fast forward iteration index
  263. this.index = this.size;
  264. // abort jobs
  265. abort(this);
  266. // send back results we have so far
  267. async(callback)(null, this.results);
  268. }
  269. }, function(modId) { var map = {"./abort.js":1662612701694,"./async.js":1662612701692}; return __REQUIRE__(map[modId], modId); })
  270. __DEFINE__(1662612701697, function(require, module, exports) {
  271. var serialOrdered = require('./serialOrdered.js');
  272. // Public API
  273. module.exports = serial;
  274. /**
  275. * Runs iterator over provided array elements in series
  276. *
  277. * @param {array|object} list - array or object (named list) to iterate over
  278. * @param {function} iterator - iterator to run
  279. * @param {function} callback - invoked when all elements processed
  280. * @returns {function} - jobs terminator
  281. */
  282. function serial(list, iterator, callback)
  283. {
  284. return serialOrdered(list, iterator, null, callback);
  285. }
  286. }, function(modId) { var map = {"./serialOrdered.js":1662612701698}; return __REQUIRE__(map[modId], modId); })
  287. __DEFINE__(1662612701698, function(require, module, exports) {
  288. var iterate = require('./lib/iterate.js')
  289. , initState = require('./lib/state.js')
  290. , terminator = require('./lib/terminator.js')
  291. ;
  292. // Public API
  293. module.exports = serialOrdered;
  294. // sorting helpers
  295. module.exports.ascending = ascending;
  296. module.exports.descending = descending;
  297. /**
  298. * Runs iterator over provided sorted array elements in series
  299. *
  300. * @param {array|object} list - array or object (named list) to iterate over
  301. * @param {function} iterator - iterator to run
  302. * @param {function} sortMethod - custom sort function
  303. * @param {function} callback - invoked when all elements processed
  304. * @returns {function} - jobs terminator
  305. */
  306. function serialOrdered(list, iterator, sortMethod, callback)
  307. {
  308. var state = initState(list, sortMethod);
  309. iterate(list, iterator, state, function iteratorHandler(error, result)
  310. {
  311. if (error)
  312. {
  313. callback(error, result);
  314. return;
  315. }
  316. state.index++;
  317. // are we there yet?
  318. if (state.index < (state['keyedList'] || list).length)
  319. {
  320. iterate(list, iterator, state, iteratorHandler);
  321. return;
  322. }
  323. // done here
  324. callback(null, state.results);
  325. });
  326. return terminator.bind(state, callback);
  327. }
  328. /*
  329. * -- Sort methods
  330. */
  331. /**
  332. * sort helper to sort array elements in ascending order
  333. *
  334. * @param {mixed} a - an item to compare
  335. * @param {mixed} b - an item to compare
  336. * @returns {number} - comparison result
  337. */
  338. function ascending(a, b)
  339. {
  340. return a < b ? -1 : a > b ? 1 : 0;
  341. }
  342. /**
  343. * sort helper to sort array elements in descending order
  344. *
  345. * @param {mixed} a - an item to compare
  346. * @param {mixed} b - an item to compare
  347. * @returns {number} - comparison result
  348. */
  349. function descending(a, b)
  350. {
  351. return -1 * ascending(a, b);
  352. }
  353. }, function(modId) { var map = {"./lib/iterate.js":1662612701691,"./lib/state.js":1662612701695,"./lib/terminator.js":1662612701696}; return __REQUIRE__(map[modId], modId); })
  354. return __REQUIRE__(1662612701689);
  355. })()
  356. //miniprogram-npm-outsideDeps=[]
  357. //# sourceMappingURL=index.js.map