proxy.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. 'use strict';
  2. /**
  3. * @license Angular v<unknown>
  4. * (c) 2010-2022 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 && this.lastTaskState &&
  42. (this.lastTaskState.macroTask || this.lastTaskState.microTask)) {
  43. this.isNeedToTriggerHasTask = true;
  44. }
  45. }
  46. getDelegate() {
  47. return this._delegateSpec;
  48. }
  49. resetDelegate() {
  50. this.getDelegate();
  51. this.setDelegate(this.defaultSpecDelegate);
  52. }
  53. tryTriggerHasTask(parentZoneDelegate, currentZone, targetZone) {
  54. if (this.isNeedToTriggerHasTask && this.lastTaskState) {
  55. // last delegateSpec has microTask or macroTask
  56. // should call onHasTask in current delegateSpec
  57. this.isNeedToTriggerHasTask = false;
  58. this.onHasTask(parentZoneDelegate, currentZone, targetZone, this.lastTaskState);
  59. }
  60. }
  61. removeFromTasks(task) {
  62. if (!this.tasks) {
  63. return;
  64. }
  65. for (let i = 0; i < this.tasks.length; i++) {
  66. if (this.tasks[i] === task) {
  67. this.tasks.splice(i, 1);
  68. return;
  69. }
  70. }
  71. }
  72. getAndClearPendingTasksInfo() {
  73. if (this.tasks.length === 0) {
  74. return '';
  75. }
  76. const taskInfo = this.tasks.map((task) => {
  77. const dataInfo = task.data &&
  78. Object.keys(task.data)
  79. .map((key) => {
  80. return key + ':' + task.data[key];
  81. })
  82. .join(',');
  83. return `type: ${task.type}, source: ${task.source}, args: {${dataInfo}}`;
  84. });
  85. const pendingTasksInfo = '--Pending async tasks are: [' + taskInfo + ']';
  86. // clear tasks
  87. this.tasks = [];
  88. return pendingTasksInfo;
  89. }
  90. onFork(parentZoneDelegate, currentZone, targetZone, zoneSpec) {
  91. if (this._delegateSpec && this._delegateSpec.onFork) {
  92. return this._delegateSpec.onFork(parentZoneDelegate, currentZone, targetZone, zoneSpec);
  93. }
  94. else {
  95. return parentZoneDelegate.fork(targetZone, zoneSpec);
  96. }
  97. }
  98. onIntercept(parentZoneDelegate, currentZone, targetZone, delegate, source) {
  99. if (this._delegateSpec && this._delegateSpec.onIntercept) {
  100. return this._delegateSpec.onIntercept(parentZoneDelegate, currentZone, targetZone, delegate, source);
  101. }
  102. else {
  103. return parentZoneDelegate.intercept(targetZone, delegate, source);
  104. }
  105. }
  106. onInvoke(parentZoneDelegate, currentZone, targetZone, delegate, applyThis, applyArgs, source) {
  107. this.tryTriggerHasTask(parentZoneDelegate, currentZone, targetZone);
  108. if (this._delegateSpec && this._delegateSpec.onInvoke) {
  109. return this._delegateSpec.onInvoke(parentZoneDelegate, currentZone, targetZone, delegate, applyThis, applyArgs, source);
  110. }
  111. else {
  112. return parentZoneDelegate.invoke(targetZone, delegate, applyThis, applyArgs, source);
  113. }
  114. }
  115. onHandleError(parentZoneDelegate, currentZone, targetZone, error) {
  116. if (this._delegateSpec && this._delegateSpec.onHandleError) {
  117. return this._delegateSpec.onHandleError(parentZoneDelegate, currentZone, targetZone, error);
  118. }
  119. else {
  120. return parentZoneDelegate.handleError(targetZone, error);
  121. }
  122. }
  123. onScheduleTask(parentZoneDelegate, currentZone, targetZone, task) {
  124. if (task.type !== 'eventTask') {
  125. this.tasks.push(task);
  126. }
  127. if (this._delegateSpec && this._delegateSpec.onScheduleTask) {
  128. return this._delegateSpec.onScheduleTask(parentZoneDelegate, currentZone, targetZone, task);
  129. }
  130. else {
  131. return parentZoneDelegate.scheduleTask(targetZone, task);
  132. }
  133. }
  134. onInvokeTask(parentZoneDelegate, currentZone, targetZone, task, applyThis, applyArgs) {
  135. if (task.type !== 'eventTask') {
  136. this.removeFromTasks(task);
  137. }
  138. this.tryTriggerHasTask(parentZoneDelegate, currentZone, targetZone);
  139. if (this._delegateSpec && this._delegateSpec.onInvokeTask) {
  140. return this._delegateSpec.onInvokeTask(parentZoneDelegate, currentZone, targetZone, task, applyThis, applyArgs);
  141. }
  142. else {
  143. return parentZoneDelegate.invokeTask(targetZone, task, applyThis, applyArgs);
  144. }
  145. }
  146. onCancelTask(parentZoneDelegate, currentZone, targetZone, task) {
  147. if (task.type !== 'eventTask') {
  148. this.removeFromTasks(task);
  149. }
  150. this.tryTriggerHasTask(parentZoneDelegate, currentZone, targetZone);
  151. if (this._delegateSpec && this._delegateSpec.onCancelTask) {
  152. return this._delegateSpec.onCancelTask(parentZoneDelegate, currentZone, targetZone, task);
  153. }
  154. else {
  155. return parentZoneDelegate.cancelTask(targetZone, task);
  156. }
  157. }
  158. onHasTask(delegate, current, target, hasTaskState) {
  159. this.lastTaskState = hasTaskState;
  160. if (this._delegateSpec && this._delegateSpec.onHasTask) {
  161. this._delegateSpec.onHasTask(delegate, current, target, hasTaskState);
  162. }
  163. else {
  164. delegate.hasTask(target, hasTaskState);
  165. }
  166. }
  167. }
  168. // Export the class so that new instances can be created with proper
  169. // constructor params.
  170. Zone['ProxyZoneSpec'] = ProxyZoneSpec;