proxy.js 6.8 KB

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