Advertisement
Guest User

Untitled

a guest
May 20th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. function CallbackQueue() {
  2. let arr = null;
  3.  
  4. function reset() {
  5. (arr = arr || []).length = 0;
  6. }
  7.  
  8. function flush(i) {
  9. let errThown;
  10. arr = arr ? arr.slice() : [];
  11. try {
  12. errThown = true;
  13. for (i !== undefined ? i : 0, len = arr.length; i < len; i++) {
  14. const item = arr[i];
  15. const fn = item[0];
  16. const ctx = item[1];
  17. const args = item[2];
  18. fn.apply(ctx, args);
  19. }
  20. errThown = false;
  21. } finally {
  22. if (errThown) {
  23. flush(i + 1);
  24. } else {
  25. reset();
  26. }
  27. }
  28. }
  29.  
  30. return {
  31. reset,
  32. enqueue(fn, context) {
  33. const args = [].slice.call(arguments, 2);
  34. arr = arr || [];
  35. arr.push([fn, context, args]);
  36. },
  37. flush() {
  38. flush(0);
  39. }
  40. };
  41. }
  42.  
  43. exports = module.exports = CallbackQueue;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement