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

Commit 89f990a

Browse files
JiaLiPassionmhevery
authored andcommitted
fix(patch): fix #744, add namespace to load patch name (#774)
* fix(patch): fix #744, add namespace to load patch name * in electron, we only patch timers.setTimeout * refactor browser/node module load structure
1 parent 582ff7b commit 89f990a

10 files changed

+104
-80
lines changed

Diff for: gulpfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ gulp.task('build/zone.js.d.ts', ['compile-esm'], function() {
7474

7575
// Zone for Node.js environment.
7676
gulp.task('build/zone-node.js', ['compile-esm'], function(cb) {
77-
return generateScript('./lib/node/node.ts', 'zone-node.js', false, cb);
77+
return generateScript('./lib/node/rollup-main.ts', 'zone-node.js', false, cb);
7878
});
7979

8080
// Zone for the browser.

Diff for: lib/browser/browser.ts

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

9-
import '../common/to-string';
10-
119
import {patchTimer} from '../common/timers';
1210
import {findEventTask, patchClass, patchEventTargetMethods, patchMethod, patchOnProperties, patchPrototype, zoneSymbol} from '../common/utils';
1311

Diff for: lib/browser/rollup-main.ts

+1-1
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-
109
import '../zone';
1110
import '../common/promise';
11+
import '../common/to-string';
1212
import './browser';

Diff for: lib/mix/rollup-mix.ts

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

9-
109
import '../zone';
10+
import '../common/promise';
11+
import '../common/to-string';
1112
import '../browser/browser';
1213
import '../node/node';

Diff for: lib/node/events.ts

+44-42
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,54 @@
88

99
import {makeZoneAwareAddListener, makeZoneAwareListeners, makeZoneAwareRemoveAllListeners, makeZoneAwareRemoveListener, patchMethod} from '../common/utils';
1010

11-
const callAndReturnFirstParam = (fn: (self: any, args: any[]) => any) => {
12-
return (self: any, args: any[]) => {
13-
fn(self, args);
14-
return self;
11+
Zone.__load_patch('EventEmitter', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
12+
const callAndReturnFirstParam = (fn: (self: any, args: any[]) => any) => {
13+
return (self: any, args: any[]) => {
14+
fn(self, args);
15+
return self;
16+
};
1517
};
16-
};
1718

18-
// For EventEmitter
19-
const EE_ADD_LISTENER = 'addListener';
20-
const EE_PREPEND_LISTENER = 'prependListener';
21-
const EE_REMOVE_LISTENER = 'removeListener';
22-
const EE_REMOVE_ALL_LISTENER = 'removeAllListeners';
23-
const EE_LISTENERS = 'listeners';
24-
const EE_ON = 'on';
19+
// For EventEmitter
20+
const EE_ADD_LISTENER = 'addListener';
21+
const EE_PREPEND_LISTENER = 'prependListener';
22+
const EE_REMOVE_LISTENER = 'removeListener';
23+
const EE_REMOVE_ALL_LISTENER = 'removeAllListeners';
24+
const EE_LISTENERS = 'listeners';
25+
const EE_ON = 'on';
2526

26-
const zoneAwareAddListener = callAndReturnFirstParam(
27-
makeZoneAwareAddListener(EE_ADD_LISTENER, EE_REMOVE_LISTENER, false, true, false));
28-
const zoneAwarePrependListener = callAndReturnFirstParam(
29-
makeZoneAwareAddListener(EE_PREPEND_LISTENER, EE_REMOVE_LISTENER, false, true, true));
30-
const zoneAwareRemoveListener =
31-
callAndReturnFirstParam(makeZoneAwareRemoveListener(EE_REMOVE_LISTENER, false));
32-
const zoneAwareRemoveAllListeners =
33-
callAndReturnFirstParam(makeZoneAwareRemoveAllListeners(EE_REMOVE_ALL_LISTENER));
34-
const zoneAwareListeners = makeZoneAwareListeners(EE_LISTENERS);
27+
const zoneAwareAddListener = callAndReturnFirstParam(
28+
makeZoneAwareAddListener(EE_ADD_LISTENER, EE_REMOVE_LISTENER, false, true, false));
29+
const zoneAwarePrependListener = callAndReturnFirstParam(
30+
makeZoneAwareAddListener(EE_PREPEND_LISTENER, EE_REMOVE_LISTENER, false, true, true));
31+
const zoneAwareRemoveListener =
32+
callAndReturnFirstParam(makeZoneAwareRemoveListener(EE_REMOVE_LISTENER, false));
33+
const zoneAwareRemoveAllListeners =
34+
callAndReturnFirstParam(makeZoneAwareRemoveAllListeners(EE_REMOVE_ALL_LISTENER));
35+
const zoneAwareListeners = makeZoneAwareListeners(EE_LISTENERS);
3536

36-
export function patchEventEmitterMethods(obj: any): boolean {
37-
if (obj && obj.addListener) {
38-
patchMethod(obj, EE_ADD_LISTENER, () => zoneAwareAddListener);
39-
patchMethod(obj, EE_PREPEND_LISTENER, () => zoneAwarePrependListener);
40-
patchMethod(obj, EE_REMOVE_LISTENER, () => zoneAwareRemoveListener);
41-
patchMethod(obj, EE_REMOVE_ALL_LISTENER, () => zoneAwareRemoveAllListeners);
42-
patchMethod(obj, EE_LISTENERS, () => zoneAwareListeners);
43-
obj[EE_ON] = obj[EE_ADD_LISTENER];
44-
return true;
45-
} else {
46-
return false;
37+
function patchEventEmitterMethods(obj: any): boolean {
38+
if (obj && obj.addListener) {
39+
patchMethod(obj, EE_ADD_LISTENER, () => zoneAwareAddListener);
40+
patchMethod(obj, EE_PREPEND_LISTENER, () => zoneAwarePrependListener);
41+
patchMethod(obj, EE_REMOVE_LISTENER, () => zoneAwareRemoveListener);
42+
patchMethod(obj, EE_REMOVE_ALL_LISTENER, () => zoneAwareRemoveAllListeners);
43+
patchMethod(obj, EE_LISTENERS, () => zoneAwareListeners);
44+
obj[EE_ON] = obj[EE_ADD_LISTENER];
45+
return true;
46+
} else {
47+
return false;
48+
}
4749
}
48-
}
4950

50-
// EventEmitter
51-
let events;
52-
try {
53-
events = require('events');
54-
} catch (err) {
55-
}
51+
// EventEmitter
52+
let events;
53+
try {
54+
events = require('events');
55+
} catch (err) {
56+
}
5657

57-
if (events && events.EventEmitter) {
58-
patchEventEmitterMethods(events.EventEmitter.prototype);
59-
}
58+
if (events && events.EventEmitter) {
59+
patchEventEmitterMethods(events.EventEmitter.prototype);
60+
}
61+
});

Diff for: lib/node/fs.ts

+28-26
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,34 @@
88

99
import {patchMacroTask} from '../common/utils';
1010

11-
let fs: any;
12-
try {
13-
fs = require('fs');
14-
} catch (err) {
15-
}
11+
Zone.__load_patch('fs', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
12+
let fs: any;
13+
try {
14+
fs = require('fs');
15+
} catch (err) {
16+
}
1617

17-
// watch, watchFile, unwatchFile has been patched
18-
// because EventEmitter has been patched
19-
const TO_PATCH_MACROTASK_METHODS = [
20-
'access', 'appendFile', 'chmod', 'chown', 'close', 'exists', 'fchmod',
21-
'fchown', 'fdatasync', 'fstat', 'fsync', 'ftruncate', 'futimes', 'lchmod',
22-
'lchown', 'link', 'lstat', 'mkdir', 'mkdtemp', 'open', 'read',
23-
'readdir', 'readFile', 'readlink', 'realpath', 'rename', 'rmdir', 'stat',
24-
'symlink', 'truncate', 'unlink', 'utimes', 'write', 'writeFile',
25-
];
18+
// watch, watchFile, unwatchFile has been patched
19+
// because EventEmitter has been patched
20+
const TO_PATCH_MACROTASK_METHODS = [
21+
'access', 'appendFile', 'chmod', 'chown', 'close', 'exists', 'fchmod',
22+
'fchown', 'fdatasync', 'fstat', 'fsync', 'ftruncate', 'futimes', 'lchmod',
23+
'lchown', 'link', 'lstat', 'mkdir', 'mkdtemp', 'open', 'read',
24+
'readdir', 'readFile', 'readlink', 'realpath', 'rename', 'rmdir', 'stat',
25+
'symlink', 'truncate', 'unlink', 'utimes', 'write', 'writeFile',
26+
];
2627

27-
if (fs) {
28-
TO_PATCH_MACROTASK_METHODS.filter(name => !!fs[name] && typeof fs[name] === 'function')
29-
.forEach(name => {
30-
patchMacroTask(fs, name, (self: any, args: any[]) => {
31-
return {
32-
name: 'fs.' + name,
33-
args: args,
34-
callbackIndex: args.length > 0 ? args.length - 1 : -1,
35-
target: self
36-
};
28+
if (fs) {
29+
TO_PATCH_MACROTASK_METHODS.filter(name => !!fs[name] && typeof fs[name] === 'function')
30+
.forEach(name => {
31+
patchMacroTask(fs, name, (self: any, args: any[]) => {
32+
return {
33+
name: 'fs.' + name,
34+
args: args,
35+
callbackIndex: args.length > 0 ? args.length - 1 : -1,
36+
target: self
37+
};
38+
});
3739
});
38-
});
39-
}
40+
}
41+
});

Diff for: lib/node/node.ts

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

9-
import '../zone';
10-
import '../common/promise';
11-
import '../common/to-string';
129
import './events';
1310
import './fs';
1411

1512
import {patchTimer} from '../common/timers';
16-
import {findEventTask, patchMacroTask, patchMicroTask} from '../common/utils';
13+
import {findEventTask, isMix, patchMacroTask, patchMicroTask} from '../common/utils';
1714

1815
const set = 'set';
1916
const clear = 'clear';
2017

21-
Zone.__load_patch('timers', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
18+
Zone.__load_patch('node_timers', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
2219
// Timers
2320
let globalUseTimeoutFromTimer = false;
2421
try {
2522
const timers = require('timers');
2623
let globalEqualTimersTimeout = global.setTimeout === timers.setTimeout;
27-
if (!globalEqualTimersTimeout) {
28-
// if global.setTimeout not equal timers.setTimeout, check
24+
if (!globalEqualTimersTimeout && !isMix) {
25+
// 1. if isMix, then we are in mix environment such as Electron
26+
// we should only patch timers.setTimeout because global.setTimeout
27+
// have been patched
28+
// 2. if global.setTimeout not equal timers.setTimeout, check
2929
// whether global.setTimeout use timers.setTimeout or not
3030
const originSetTimeout = timers.setTimeout;
3131
timers.setTimeout = function() {
@@ -43,6 +43,12 @@ Zone.__load_patch('timers', (global: any, Zone: ZoneType, api: _ZonePrivate) =>
4343
// timers module not exists, for example, when we using nativescript
4444
// timers is not available
4545
}
46+
if (isMix) {
47+
// if we are in mix environment, such as Electron,
48+
// the global.setTimeout has already been patched,
49+
// so we just patch timers.setTimeout
50+
return;
51+
}
4652
if (!globalUseTimeoutFromTimer) {
4753
// 1. global setTimeout equals timers setTimeout
4854
// 2. or global don't use timers setTimeout(maybe some other library patch setTimeout)

Diff for: lib/node/rollup-main.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
9+
import '../zone';
10+
import '../common/promise';
11+
import '../common/to-string';
12+
import './node';

Diff for: test/browser-zone-setup.ts

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

9+
import '../lib/common/to-string';
910
import '../lib/browser/browser';
1011
import '../lib/zone-spec/async-test';
1112
import '../lib/zone-spec/fake-async-test';

Diff for: test/node_entry_point.ts

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import './custom_error';
1212

1313
// Setup tests for Zone without microtask support
1414
import '../lib/zone';
15+
import '../lib/common/promise';
16+
import '../lib/common/to-string';
1517
import '../lib/node/node';
1618
import '../lib/zone-spec/async-test';
1719
import '../lib/zone-spec/fake-async-test';

0 commit comments

Comments
 (0)