Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

AsyncQueue.js 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import async from 'async';
  2. /**
  3. * A queue for async task execution.
  4. */
  5. export default class AsyncQueue {
  6. /**
  7. * Creates new instance.
  8. */
  9. constructor() {
  10. this._queue = async.queue(this._processQueueTasks.bind(this), 1);
  11. this._stopped = false;
  12. }
  13. /**
  14. * Internal task processing implementation which makes things work.
  15. */
  16. _processQueueTasks(task, finishedCallback) {
  17. task(finishedCallback);
  18. }
  19. /**
  20. * The 'workFunction' function will be given a callback it MUST call with either:
  21. * 1) No arguments if it was successful or
  22. * 2) An error argument if there was an error
  23. * If the task wants to process the success or failure of the task, it
  24. * should pass the {@code callback} to the push function, e.g.:
  25. * queue.push(task, (err) => {
  26. * if (err) {
  27. * // error handling
  28. * } else {
  29. * // success handling
  30. * }
  31. * });
  32. *
  33. * @param {function} workFunction - The task to be execute. See the description above.
  34. * @param {function} [callback] - Optional callback to be called after the task has been executed.
  35. */
  36. push(workFunction, callback) {
  37. if (this._stopped) {
  38. callback && callback(new Error('The queue has been stopped'));
  39. return;
  40. }
  41. this._queue.push(workFunction, callback);
  42. }
  43. /**
  44. * Shutdowns the queue. All already queued tasks will execute, but no future tasks can be added. If a task is added
  45. * after the queue has been shutdown then the callback will be called with an error.
  46. */
  47. shutdown() {
  48. this._stopped = true;
  49. }
  50. }