proxy.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * @license
  3. * Copyright Google Inc. All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. class ProxyZoneSpec implements ZoneSpec {
  9. name: string = 'ProxyZone';
  10. private _delegateSpec: ZoneSpec|null = null;
  11. properties: {[k: string]: any} = {'ProxyZoneSpec': this};
  12. propertyKeys: string[]|null = null;
  13. lastTaskState: HasTaskState|null = null;
  14. isNeedToTriggerHasTask = false;
  15. private tasks: Task[] = [];
  16. static get(): ProxyZoneSpec {
  17. return Zone.current.get('ProxyZoneSpec');
  18. }
  19. static isLoaded(): boolean {
  20. return ProxyZoneSpec.get() instanceof ProxyZoneSpec;
  21. }
  22. static assertPresent(): ProxyZoneSpec {
  23. if (!ProxyZoneSpec.isLoaded()) {
  24. throw new Error(`Expected to be running in 'ProxyZone', but it was not found.`);
  25. }
  26. return ProxyZoneSpec.get();
  27. }
  28. constructor(private defaultSpecDelegate: ZoneSpec|null = null) {
  29. this.setDelegate(defaultSpecDelegate);
  30. }
  31. setDelegate(delegateSpec: ZoneSpec|null) {
  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 set a new delegateSpec, shoulde check whether need to
  41. // trigger hasTask or not
  42. if (isNewDelegate && 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. const delegateSpec = this.getDelegate();
  52. this.setDelegate(this.defaultSpecDelegate);
  53. }
  54. tryTriggerHasTask(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone) {
  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: 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: Task) => {
  78. const dataInfo = task.data &&
  79. Object.keys(task.data)
  80. .map((key: string) => {
  81. return key + ':' + (task.data as any)[key];
  82. })
  83. .join(',');
  84. return `type: ${task.type}, source: ${task.source}, args: {${dataInfo}}`;
  85. });
  86. const pendingTasksInfo = '--Pendng async tasks are: [' + taskInfo + ']';
  87. // clear tasks
  88. this.tasks = [];
  89. return pendingTasksInfo;
  90. }
  91. onFork(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, zoneSpec: ZoneSpec):
  92. Zone {
  93. if (this._delegateSpec && this._delegateSpec.onFork) {
  94. return this._delegateSpec.onFork(parentZoneDelegate, currentZone, targetZone, zoneSpec);
  95. } else {
  96. return parentZoneDelegate.fork(targetZone, zoneSpec);
  97. }
  98. }
  99. onIntercept(
  100. parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, delegate: Function,
  101. source: string): Function {
  102. if (this._delegateSpec && this._delegateSpec.onIntercept) {
  103. return this._delegateSpec.onIntercept(
  104. parentZoneDelegate, currentZone, targetZone, delegate, source);
  105. } else {
  106. return parentZoneDelegate.intercept(targetZone, delegate, source);
  107. }
  108. }
  109. onInvoke(
  110. parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, delegate: Function,
  111. applyThis: any, applyArgs?: any[], source?: string): any {
  112. this.tryTriggerHasTask(parentZoneDelegate, currentZone, targetZone);
  113. if (this._delegateSpec && this._delegateSpec.onInvoke) {
  114. return this._delegateSpec.onInvoke(
  115. parentZoneDelegate, currentZone, targetZone, delegate, applyThis, applyArgs, source);
  116. } else {
  117. return parentZoneDelegate.invoke(targetZone, delegate, applyThis, applyArgs, source);
  118. }
  119. }
  120. onHandleError(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, error: any):
  121. boolean {
  122. if (this._delegateSpec && this._delegateSpec.onHandleError) {
  123. return this._delegateSpec.onHandleError(parentZoneDelegate, currentZone, targetZone, error);
  124. } else {
  125. return parentZoneDelegate.handleError(targetZone, error);
  126. }
  127. }
  128. onScheduleTask(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task):
  129. Task {
  130. if (task.type !== 'eventTask') {
  131. this.tasks.push(task);
  132. }
  133. if (this._delegateSpec && this._delegateSpec.onScheduleTask) {
  134. return this._delegateSpec.onScheduleTask(parentZoneDelegate, currentZone, targetZone, task);
  135. } else {
  136. return parentZoneDelegate.scheduleTask(targetZone, task);
  137. }
  138. }
  139. onInvokeTask(
  140. parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task,
  141. applyThis: any, applyArgs: any): any {
  142. if (task.type !== 'eventTask') {
  143. this.removeFromTasks(task);
  144. }
  145. this.tryTriggerHasTask(parentZoneDelegate, currentZone, targetZone);
  146. if (this._delegateSpec && this._delegateSpec.onInvokeTask) {
  147. return this._delegateSpec.onInvokeTask(
  148. parentZoneDelegate, currentZone, targetZone, task, applyThis, applyArgs);
  149. } else {
  150. return parentZoneDelegate.invokeTask(targetZone, task, applyThis, applyArgs);
  151. }
  152. }
  153. onCancelTask(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task):
  154. any {
  155. if (task.type !== 'eventTask') {
  156. this.removeFromTasks(task);
  157. }
  158. this.tryTriggerHasTask(parentZoneDelegate, currentZone, targetZone);
  159. if (this._delegateSpec && this._delegateSpec.onCancelTask) {
  160. return this._delegateSpec.onCancelTask(parentZoneDelegate, currentZone, targetZone, task);
  161. } else {
  162. return parentZoneDelegate.cancelTask(targetZone, task);
  163. }
  164. }
  165. onHasTask(delegate: ZoneDelegate, current: Zone, target: Zone, hasTaskState: HasTaskState): void {
  166. this.lastTaskState = hasTaskState;
  167. if (this._delegateSpec && this._delegateSpec.onHasTask) {
  168. this._delegateSpec.onHasTask(delegate, current, target, hasTaskState);
  169. } else {
  170. delegate.hasTask(target, hasTaskState);
  171. }
  172. }
  173. }
  174. // Export the class so that new instances can be created with proper
  175. // constructor params.
  176. (Zone as any)['ProxyZoneSpec'] = ProxyZoneSpec;