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