proxy.umd.js 8.3 KB

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