Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

priorityQueue.js 2.6KB

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