Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AsyncQueue.js 2.0KB

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