proxy.js 8.0 KB

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