task-tracking.umd.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict';
  2. /**
  3. * @license Angular v<unknown>
  4. * (c) 2010-2022 Google LLC. https://angular.io/
  5. * License: MIT
  6. */
  7. (function (factory) {
  8. typeof define === 'function' && define.amd ? define(factory) :
  9. factory();
  10. })((function () {
  11. 'use strict';
  12. /**
  13. * A `TaskTrackingZoneSpec` allows one to track all outstanding Tasks.
  14. *
  15. * This is useful in tests. For example to see which tasks are preventing a test from completing
  16. * or an automated way of releasing all of the event listeners at the end of the test.
  17. */
  18. var TaskTrackingZoneSpec = /** @class */ (function () {
  19. function TaskTrackingZoneSpec() {
  20. this.name = 'TaskTrackingZone';
  21. this.microTasks = [];
  22. this.macroTasks = [];
  23. this.eventTasks = [];
  24. this.properties = { 'TaskTrackingZone': this };
  25. }
  26. TaskTrackingZoneSpec.get = function () {
  27. return Zone.current.get('TaskTrackingZone');
  28. };
  29. TaskTrackingZoneSpec.prototype.getTasksFor = function (type) {
  30. switch (type) {
  31. case 'microTask':
  32. return this.microTasks;
  33. case 'macroTask':
  34. return this.macroTasks;
  35. case 'eventTask':
  36. return this.eventTasks;
  37. }
  38. throw new Error('Unknown task format: ' + type);
  39. };
  40. TaskTrackingZoneSpec.prototype.onScheduleTask = function (parentZoneDelegate, currentZone, targetZone, task) {
  41. task['creationLocation'] = new Error("Task '".concat(task.type, "' from '").concat(task.source, "'."));
  42. var tasks = this.getTasksFor(task.type);
  43. tasks.push(task);
  44. return parentZoneDelegate.scheduleTask(targetZone, task);
  45. };
  46. TaskTrackingZoneSpec.prototype.onCancelTask = function (parentZoneDelegate, currentZone, targetZone, task) {
  47. var tasks = this.getTasksFor(task.type);
  48. for (var i = 0; i < tasks.length; i++) {
  49. if (tasks[i] == task) {
  50. tasks.splice(i, 1);
  51. break;
  52. }
  53. }
  54. return parentZoneDelegate.cancelTask(targetZone, task);
  55. };
  56. TaskTrackingZoneSpec.prototype.onInvokeTask = function (parentZoneDelegate, currentZone, targetZone, task, applyThis, applyArgs) {
  57. var _a;
  58. if (task.type === 'eventTask' || ((_a = task.data) === null || _a === void 0 ? void 0 : _a.isPeriodic))
  59. return parentZoneDelegate.invokeTask(targetZone, task, applyThis, applyArgs);
  60. var tasks = this.getTasksFor(task.type);
  61. for (var i = 0; i < tasks.length; i++) {
  62. if (tasks[i] == task) {
  63. tasks.splice(i, 1);
  64. break;
  65. }
  66. }
  67. return parentZoneDelegate.invokeTask(targetZone, task, applyThis, applyArgs);
  68. };
  69. TaskTrackingZoneSpec.prototype.clearEvents = function () {
  70. while (this.eventTasks.length) {
  71. Zone.current.cancelTask(this.eventTasks[0]);
  72. }
  73. };
  74. return TaskTrackingZoneSpec;
  75. }());
  76. // Export the class so that new instances can be created with proper
  77. // constructor params.
  78. Zone['TaskTrackingZoneSpec'] = TaskTrackingZoneSpec;
  79. }));