UI for Zipcoin Blue

priorityQueue.js 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = function (worker, concurrency) {
  6. // Start with a normal queue
  7. var q = (0, _queue2.default)(worker, concurrency);
  8. // Override push to accept second parameter representing priority
  9. q.push = function (data, priority, callback) {
  10. if (callback == null) callback = _noop2.default;
  11. if (typeof callback !== 'function') {
  12. throw new Error('task callback must be a function');
  13. }
  14. q.started = true;
  15. if (!(0, _isArray2.default)(data)) {
  16. data = [data];
  17. }
  18. if (data.length === 0) {
  19. // call drain immediately if there are no tasks
  20. return (0, _setImmediate2.default)(function () {
  21. q.drain();
  22. });
  23. }
  24. priority = priority || 0;
  25. var nextNode = q._tasks.head;
  26. while (nextNode && priority >= nextNode.priority) {
  27. nextNode = nextNode.next;
  28. }
  29. for (var i = 0, l = data.length; i < l; i++) {
  30. var item = {
  31. data: data[i],
  32. priority: priority,
  33. callback: callback
  34. };
  35. if (nextNode) {
  36. q._tasks.insertBefore(nextNode, item);
  37. } else {
  38. q._tasks.push(item);
  39. }
  40. }
  41. (0, _setImmediate2.default)(q.process);
  42. };
  43. // Remove unshift function
  44. delete q.unshift;
  45. return q;
  46. };
  47. var _isArray = require('lodash/isArray');
  48. var _isArray2 = _interopRequireDefault(_isArray);
  49. var _noop = require('lodash/noop');
  50. var _noop2 = _interopRequireDefault(_noop);
  51. var _setImmediate = require('./setImmediate');
  52. var _setImmediate2 = _interopRequireDefault(_setImmediate);
  53. var _queue = require('./queue');
  54. var _queue2 = _interopRequireDefault(_queue);
  55. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  56. module.exports = exports['default'];
  57. /**
  58. * The same as [async.queue]{@link module:ControlFlow.queue} only tasks are assigned a priority and
  59. * completed in ascending priority order.
  60. *
  61. * @name priorityQueue
  62. * @static
  63. * @memberOf module:ControlFlow
  64. * @method
  65. * @see [async.queue]{@link module:ControlFlow.queue}
  66. * @category Control Flow
  67. * @param {AsyncFunction} worker - An async function for processing a queued task.
  68. * If you want to handle errors from an individual task, pass a callback to
  69. * `q.push()`.
  70. * Invoked with (task, callback).
  71. * @param {number} concurrency - An `integer` for determining how many `worker`
  72. * functions should be run in parallel. If omitted, the concurrency defaults to
  73. * `1`. If the concurrency is `0`, an error is thrown.
  74. * @returns {module:ControlFlow.QueueObject} A priorityQueue object to manage the tasks. There are two
  75. * differences between `queue` and `priorityQueue` objects:
  76. * * `push(task, priority, [callback])` - `priority` should be a number. If an
  77. * array of `tasks` is given, all tasks will be assigned the same priority.
  78. * * The `unshift` method was removed.
  79. */