zone-patch-rxjs-fake-async.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. 'use strict';
  2. /**
  3. * @license Angular v<unknown>
  4. * (c) 2010-2025 Google LLC. https://angular.io/
  5. * License: MIT
  6. */
  7. class ProxyZoneSpec {
  8. defaultSpecDelegate;
  9. name = 'ProxyZone';
  10. _delegateSpec = null;
  11. properties = { 'ProxyZoneSpec': this };
  12. propertyKeys = null;
  13. lastTaskState = null;
  14. isNeedToTriggerHasTask = false;
  15. tasks = [];
  16. static get() {
  17. return Zone.current.get('ProxyZoneSpec');
  18. }
  19. static isLoaded() {
  20. return ProxyZoneSpec.get() instanceof ProxyZoneSpec;
  21. }
  22. static assertPresent() {
  23. const spec = ProxyZoneSpec.get();
  24. if (spec === undefined) {
  25. throw new Error(`Expected to be running in 'ProxyZone', but it was not found.`);
  26. }
  27. return spec;
  28. }
  29. constructor(defaultSpecDelegate = null) {
  30. this.defaultSpecDelegate = defaultSpecDelegate;
  31. this.setDelegate(defaultSpecDelegate);
  32. }
  33. setDelegate(delegateSpec) {
  34. const isNewDelegate = this._delegateSpec !== delegateSpec;
  35. this._delegateSpec = delegateSpec;
  36. this.propertyKeys && this.propertyKeys.forEach((key) => delete this.properties[key]);
  37. this.propertyKeys = null;
  38. if (delegateSpec && delegateSpec.properties) {
  39. this.propertyKeys = Object.keys(delegateSpec.properties);
  40. this.propertyKeys.forEach((k) => (this.properties[k] = delegateSpec.properties[k]));
  41. }
  42. // if a new delegateSpec was set, check if we need to trigger hasTask
  43. if (isNewDelegate &&
  44. this.lastTaskState &&
  45. (this.lastTaskState.macroTask || this.lastTaskState.microTask)) {
  46. this.isNeedToTriggerHasTask = true;
  47. }
  48. }
  49. getDelegate() {
  50. return this._delegateSpec;
  51. }
  52. resetDelegate() {
  53. this.getDelegate();
  54. this.setDelegate(this.defaultSpecDelegate);
  55. }
  56. tryTriggerHasTask(parentZoneDelegate, currentZone, targetZone) {
  57. if (this.isNeedToTriggerHasTask && this.lastTaskState) {
  58. // last delegateSpec has microTask or macroTask
  59. // should call onHasTask in current delegateSpec
  60. this.isNeedToTriggerHasTask = false;
  61. this.onHasTask(parentZoneDelegate, currentZone, targetZone, this.lastTaskState);
  62. }
  63. }
  64. removeFromTasks(task) {
  65. if (!this.tasks) {
  66. return;
  67. }
  68. for (let i = 0; i < this.tasks.length; i++) {
  69. if (this.tasks[i] === task) {
  70. this.tasks.splice(i, 1);
  71. return;
  72. }
  73. }
  74. }
  75. getAndClearPendingTasksInfo() {
  76. if (this.tasks.length === 0) {
  77. return '';
  78. }
  79. const taskInfo = this.tasks.map((task) => {
  80. const dataInfo = task.data &&
  81. Object.keys(task.data)
  82. .map((key) => {
  83. return key + ':' + task.data[key];
  84. })
  85. .join(',');
  86. return `type: ${task.type}, source: ${task.source}, args: {${dataInfo}}`;
  87. });
  88. const pendingTasksInfo = '--Pending async tasks are: [' + taskInfo + ']';
  89. // clear tasks
  90. this.tasks = [];
  91. return pendingTasksInfo;
  92. }
  93. onFork(parentZoneDelegate, currentZone, targetZone, zoneSpec) {
  94. if (this._delegateSpec && this._delegateSpec.onFork) {
  95. return this._delegateSpec.onFork(parentZoneDelegate, currentZone, targetZone, zoneSpec);
  96. }
  97. else {
  98. return parentZoneDelegate.fork(targetZone, zoneSpec);
  99. }
  100. }
  101. onIntercept(parentZoneDelegate, currentZone, targetZone, delegate, source) {
  102. if (this._delegateSpec && this._delegateSpec.onIntercept) {
  103. return this._delegateSpec.onIntercept(parentZoneDelegate, currentZone, targetZone, delegate, source);
  104. }
  105. else {
  106. return parentZoneDelegate.intercept(targetZone, delegate, source);
  107. }
  108. }
  109. onInvoke(parentZoneDelegate, currentZone, targetZone, delegate, applyThis, applyArgs, source) {
  110. this.tryTriggerHasTask(parentZoneDelegate, currentZone, targetZone);
  111. if (this._delegateSpec && this._delegateSpec.onInvoke) {
  112. return this._delegateSpec.onInvoke(parentZoneDelegate, currentZone, targetZone, delegate, applyThis, applyArgs, source);
  113. }
  114. else {
  115. return parentZoneDelegate.invoke(targetZone, delegate, applyThis, applyArgs, source);
  116. }
  117. }
  118. onHandleError(parentZoneDelegate, currentZone, targetZone, error) {
  119. if (this._delegateSpec && this._delegateSpec.onHandleError) {
  120. return this._delegateSpec.onHandleError(parentZoneDelegate, currentZone, targetZone, error);
  121. }
  122. else {
  123. return parentZoneDelegate.handleError(targetZone, error);
  124. }
  125. }
  126. onScheduleTask(parentZoneDelegate, currentZone, targetZone, task) {
  127. if (task.type !== 'eventTask') {
  128. this.tasks.push(task);
  129. }
  130. if (this._delegateSpec && this._delegateSpec.onScheduleTask) {
  131. return this._delegateSpec.onScheduleTask(parentZoneDelegate, currentZone, targetZone, task);
  132. }
  133. else {
  134. return parentZoneDelegate.scheduleTask(targetZone, task);
  135. }
  136. }
  137. onInvokeTask(parentZoneDelegate, currentZone, targetZone, task, applyThis, applyArgs) {
  138. if (task.type !== 'eventTask') {
  139. this.removeFromTasks(task);
  140. }
  141. this.tryTriggerHasTask(parentZoneDelegate, currentZone, targetZone);
  142. if (this._delegateSpec && this._delegateSpec.onInvokeTask) {
  143. return this._delegateSpec.onInvokeTask(parentZoneDelegate, currentZone, targetZone, task, applyThis, applyArgs);
  144. }
  145. else {
  146. return parentZoneDelegate.invokeTask(targetZone, task, applyThis, applyArgs);
  147. }
  148. }
  149. onCancelTask(parentZoneDelegate, currentZone, targetZone, task) {
  150. if (task.type !== 'eventTask') {
  151. this.removeFromTasks(task);
  152. }
  153. this.tryTriggerHasTask(parentZoneDelegate, currentZone, targetZone);
  154. if (this._delegateSpec && this._delegateSpec.onCancelTask) {
  155. return this._delegateSpec.onCancelTask(parentZoneDelegate, currentZone, targetZone, task);
  156. }
  157. else {
  158. return parentZoneDelegate.cancelTask(targetZone, task);
  159. }
  160. }
  161. onHasTask(delegate, current, target, hasTaskState) {
  162. this.lastTaskState = hasTaskState;
  163. if (this._delegateSpec && this._delegateSpec.onHasTask) {
  164. this._delegateSpec.onHasTask(delegate, current, target, hasTaskState);
  165. }
  166. else {
  167. delegate.hasTask(target, hasTaskState);
  168. }
  169. }
  170. }
  171. function patchProxyZoneSpec(Zone) {
  172. // Export the class so that new instances can be created with proper
  173. // constructor params.
  174. Zone['ProxyZoneSpec'] = ProxyZoneSpec;
  175. }
  176. patchProxyZoneSpec(Zone);