Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 8293c37

Browse files
JiaLiPassionmhevery
authored andcommitted
fix(core): add helper method in util.ts to shorter zone.wrap/scehduleMacroTask
1 parent 957351e commit 8293c37

File tree

5 files changed

+25
-16
lines changed

5 files changed

+25
-16
lines changed

Diff for: lib/browser/browser.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import {findEventTasks} from '../common/events';
1414
import {patchTimer} from '../common/timers';
15-
import {bindArguments, patchClass, patchMacroTask, patchMethod, patchOnProperties, patchPrototype, ZONE_SYMBOL_ADD_EVENT_LISTENER, ZONE_SYMBOL_REMOVE_EVENT_LISTENER, zoneSymbol} from '../common/utils';
15+
import {bindArguments, patchClass, patchMacroTask, patchMethod, patchOnProperties, patchPrototype, scheduleMacroTaskWithCurrentZone, ZONE_SYMBOL_ADD_EVENT_LISTENER, ZONE_SYMBOL_REMOVE_EVENT_LISTENER, zoneSymbol} from '../common/utils';
1616

1717
import {propertyPatch} from './define-property';
1818
import {eventTargetPatch, patchEvent} from './event-target';
@@ -179,7 +179,6 @@ Zone.__load_patch('XHR', (global: any, Zone: ZoneType) => {
179179
const XMLHTTPREQUEST_SOURCE = 'XMLHttpRequest.send';
180180
const sendNative: Function =
181181
patchMethod(XMLHttpRequestPrototype, 'send', () => function(self: any, args: any[]) {
182-
const zone = Zone.current;
183182
if (self[XHR_SYNC]) {
184183
// if the XHR is sync there is no task to schedule, just execute the code.
185184
return sendNative.apply(self, args);
@@ -192,7 +191,7 @@ Zone.__load_patch('XHR', (global: any, Zone: ZoneType) => {
192191
args: args,
193192
aborted: false
194193
};
195-
return zone.scheduleMacroTask(
194+
return scheduleMacroTaskWithCurrentZone(
196195
XMLHTTPREQUEST_SOURCE, placeholderCallback, options, scheduleTask, clearTask);
197196
}
198197
});

Diff for: lib/browser/property-descriptor.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @suppress {globalThis}
1111
*/
1212

13-
import {isBrowser, isMix, isNode, ObjectDefineProperty, ObjectGetOwnPropertyDescriptor, ObjectGetPrototypeOf, patchClass, patchOnProperties, zoneSymbol} from '../common/utils';
13+
import {isBrowser, isMix, isNode, ObjectDefineProperty, ObjectGetOwnPropertyDescriptor, ObjectGetPrototypeOf, patchClass, patchOnProperties, wrapWithCurrentZone, zoneSymbol} from '../common/utils';
1414

1515
import * as webSocketPatch from './websocket';
1616

@@ -402,7 +402,7 @@ function patchViaCapturingAllTheEvents() {
402402
}
403403
while (elt) {
404404
if (elt[onproperty] && !elt[onproperty][unboundKey]) {
405-
bound = Zone.current.wrap(elt[onproperty], source);
405+
bound = wrapWithCurrentZone(elt[onproperty], source);
406406
bound[unboundKey] = elt[onproperty];
407407
elt[onproperty] = bound;
408408
}

Diff for: lib/browser/register-element.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {attachOriginToPatched, isBrowser, isMix, ObjectGetOwnPropertyDescriptor} from '../common/utils';
9+
import {attachOriginToPatched, isBrowser, isMix, ObjectGetOwnPropertyDescriptor, wrapWithCurrentZone} from '../common/utils';
1010

1111
import {_redefineProperty} from './define-property';
1212

@@ -27,13 +27,13 @@ export function registerElementPatch(_global: any) {
2727
if (prototype.hasOwnProperty(callback)) {
2828
const descriptor = ObjectGetOwnPropertyDescriptor(prototype, callback);
2929
if (descriptor && descriptor.value) {
30-
descriptor.value = Zone.current.wrap(descriptor.value, source);
30+
descriptor.value = wrapWithCurrentZone(descriptor.value, source);
3131
_redefineProperty(opts.prototype, callback, descriptor);
3232
} else {
33-
prototype[callback] = Zone.current.wrap(prototype[callback], source);
33+
prototype[callback] = wrapWithCurrentZone(prototype[callback], source);
3434
}
3535
} else if (prototype[callback]) {
36-
prototype[callback] = Zone.current.wrap(prototype[callback], source);
36+
prototype[callback] = wrapWithCurrentZone(prototype[callback], source);
3737
}
3838
});
3939
}

Diff for: lib/common/timers.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @suppress {missingRequire}
1111
*/
1212

13-
import {patchMethod, zoneSymbol} from './utils';
13+
import {patchMethod, scheduleMacroTaskWithCurrentZone, zoneSymbol} from './utils';
1414

1515
const taskSymbol = zoneSymbol('zoneTask');
1616

@@ -61,15 +61,14 @@ export function patchTimer(window: any, setName: string, cancelName: string, nam
6161
setNative =
6262
patchMethod(window, setName, (delegate: Function) => function(self: any, args: any[]) {
6363
if (typeof args[0] === 'function') {
64-
// Zone.current
65-
const zone = Zone.current;
6664
const options: TimerOptions = {
6765
handleId: null,
6866
isPeriodic: nameSuffix === 'Interval',
6967
delay: (nameSuffix === 'Timeout' || nameSuffix === 'Interval') ? args[1] || 0 : null,
7068
args: args
7169
};
72-
const task = zone.scheduleMacroTask(setName, args[0], options, scheduleTask, clearTask);
70+
const task =
71+
scheduleMacroTaskWithCurrentZone(setName, args[0], options, scheduleTask, clearTask);
7372
if (!task) {
7473
return task;
7574
}

Diff for: lib/common/utils.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ export const FALSE_STR = 'false';
3838
/** __zone_symbol__ string const */
3939
export const ZONE_SYMBOL_PREFIX = '__zone_symbol__';
4040

41+
export function wrapWithCurrentZone<T extends Function>(callback: T, source: string): T {
42+
return Zone.current.wrap(callback, source);
43+
}
44+
45+
export function scheduleMacroTaskWithCurrentZone(
46+
source: string, callback: Function, data: TaskData, customSchedule: (task: Task) => void,
47+
customCancel: (task: Task) => void): MacroTask {
48+
return Zone.current.scheduleMacroTask(source, callback, data, customSchedule, customCancel);
49+
}
50+
4151
// Hack since TypeScript isn't compiling this for a worker.
4252
declare const WorkerGlobalScope: any;
4353

@@ -52,7 +62,7 @@ const NULL_ON_PROP_VALUE: any[] = [null];
5262
export function bindArguments(args: any[], source: string): any[] {
5363
for (let i = args.length - 1; i >= 0; i--) {
5464
if (typeof args[i] === 'function') {
55-
args[i] = Zone.current.wrap(args[i], source + '_' + i);
65+
args[i] = wrapWithCurrentZone(args[i], source + '_' + i);
5666
}
5767
}
5868
return args;
@@ -300,7 +310,7 @@ export function patchClass(className: string) {
300310
ObjectDefineProperty(_global[className].prototype, prop, {
301311
set: function(fn) {
302312
if (typeof fn === 'function') {
303-
this[originalInstanceKey][prop] = Zone.current.wrap(fn, className + '.' + prop);
313+
this[originalInstanceKey][prop] = wrapWithCurrentZone(fn, className + '.' + prop);
304314
// keep callback in wrapped function so we can
305315
// use it in Function.prototype.toString to return
306316
// the native one.
@@ -379,7 +389,8 @@ export function patchMacroTask(
379389
setNative = patchMethod(obj, funcName, (delegate: Function) => function(self: any, args: any[]) {
380390
const meta = metaCreator(self, args);
381391
if (meta.cbIdx >= 0 && typeof args[meta.cbIdx] === 'function') {
382-
return Zone.current.scheduleMacroTask(meta.name, args[meta.cbIdx], meta, scheduleTask, null);
392+
return scheduleMacroTaskWithCurrentZone(
393+
meta.name, args[meta.cbIdx], meta, scheduleTask, null);
383394
} else {
384395
// cause an error by calling it directly.
385396
return delegate.apply(self, args);

0 commit comments

Comments
 (0)