1 | 'use strict';
|
---|
2 | /**
|
---|
3 | * @license Angular v12.0.0-next.0
|
---|
4 | * (c) 2010-2020 Google LLC. https://angular.io/
|
---|
5 | * License: MIT
|
---|
6 | */
|
---|
7 | /**
|
---|
8 | * @license
|
---|
9 | * Copyright Google LLC All Rights Reserved.
|
---|
10 | *
|
---|
11 | * Use of this source code is governed by an MIT-style license that can be
|
---|
12 | * found in the LICENSE file at https://angular.io/license
|
---|
13 | */
|
---|
14 | class ProxyZoneSpec {
|
---|
15 | constructor(defaultSpecDelegate = null) {
|
---|
16 | this.defaultSpecDelegate = defaultSpecDelegate;
|
---|
17 | this.name = 'ProxyZone';
|
---|
18 | this._delegateSpec = null;
|
---|
19 | this.properties = { 'ProxyZoneSpec': this };
|
---|
20 | this.propertyKeys = null;
|
---|
21 | this.lastTaskState = null;
|
---|
22 | this.isNeedToTriggerHasTask = false;
|
---|
23 | this.tasks = [];
|
---|
24 | this.setDelegate(defaultSpecDelegate);
|
---|
25 | }
|
---|
26 | static get() {
|
---|
27 | return Zone.current.get('ProxyZoneSpec');
|
---|
28 | }
|
---|
29 | static isLoaded() {
|
---|
30 | return ProxyZoneSpec.get() instanceof ProxyZoneSpec;
|
---|
31 | }
|
---|
32 | static assertPresent() {
|
---|
33 | if (!ProxyZoneSpec.isLoaded()) {
|
---|
34 | throw new Error(`Expected to be running in 'ProxyZone', but it was not found.`);
|
---|
35 | }
|
---|
36 | return ProxyZoneSpec.get();
|
---|
37 | }
|
---|
38 | setDelegate(delegateSpec) {
|
---|
39 | const isNewDelegate = this._delegateSpec !== delegateSpec;
|
---|
40 | this._delegateSpec = delegateSpec;
|
---|
41 | this.propertyKeys && this.propertyKeys.forEach((key) => delete this.properties[key]);
|
---|
42 | this.propertyKeys = null;
|
---|
43 | if (delegateSpec && delegateSpec.properties) {
|
---|
44 | this.propertyKeys = Object.keys(delegateSpec.properties);
|
---|
45 | this.propertyKeys.forEach((k) => 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 | getDelegate() {
|
---|
54 | return this._delegateSpec;
|
---|
55 | }
|
---|
56 | resetDelegate() {
|
---|
57 | const delegateSpec = this.getDelegate();
|
---|
58 | this.setDelegate(this.defaultSpecDelegate);
|
---|
59 | }
|
---|
60 | tryTriggerHasTask(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 | removeFromTasks(task) {
|
---|
69 | if (!this.tasks) {
|
---|
70 | return;
|
---|
71 | }
|
---|
72 | for (let 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 | getAndClearPendingTasksInfo() {
|
---|
80 | if (this.tasks.length === 0) {
|
---|
81 | return '';
|
---|
82 | }
|
---|
83 | const taskInfo = this.tasks.map((task) => {
|
---|
84 | const dataInfo = task.data &&
|
---|
85 | Object.keys(task.data)
|
---|
86 | .map((key) => {
|
---|
87 | return key + ':' + task.data[key];
|
---|
88 | })
|
---|
89 | .join(',');
|
---|
90 | return `type: ${task.type}, source: ${task.source}, args: {${dataInfo}}`;
|
---|
91 | });
|
---|
92 | const pendingTasksInfo = '--Pending async tasks are: [' + taskInfo + ']';
|
---|
93 | // clear tasks
|
---|
94 | this.tasks = [];
|
---|
95 | return pendingTasksInfo;
|
---|
96 | }
|
---|
97 | onFork(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 | onIntercept(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 | onInvoke(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 | onHandleError(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 | onScheduleTask(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 | onInvokeTask(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 | onCancelTask(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 | onHasTask(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 | }
|
---|
175 | // Export the class so that new instances can be created with proper
|
---|
176 | // constructor params.
|
---|
177 | Zone['ProxyZoneSpec'] = ProxyZoneSpec;
|
---|