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

Commit 96420d6

Browse files
JiaLiPassionmhevery
authored andcommitted
fix(event): fix #1110, nodejs EventEmitter should support Symbol eventName (#1113)
1 parent eb72ff4 commit 96420d6

8 files changed

+57
-10
lines changed

Diff for: file-size-limit.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
"path": "dist/zone.min.js",
55
"checkTarget": true,
6-
"limit": 42500
6+
"limit": 43000
77
}
88
]
99
}

Diff for: lib/common/events.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ export interface PatchEventTargetOptions {
6969
diff?: (task: any, delegate: any) => boolean;
7070
// support passive or not
7171
supportPassive?: boolean;
72+
// get string from eventName (in nodejs, eventName maybe Symbol)
73+
eventNameToString?: (eventName: any) => string;
7274
}
7375

7476
export function patchEventTarget(
@@ -212,6 +214,8 @@ export function patchEventTarget(
212214
return false;
213215
}
214216

217+
const eventNameToString = patchOptions && patchOptions.eventNameToString;
218+
215219
// a shared global taskData to pass data for scheduleEventTask
216220
// so we do not need to create a new object just for pass some data
217221
const taskData: any = {};
@@ -384,8 +388,10 @@ export function patchEventTarget(
384388
let symbolEventName;
385389
if (!symbolEventNames) {
386390
// the code is duplicate, but I just want to get some better performance
387-
const falseEventName = eventName + FALSE_STR;
388-
const trueEventName = eventName + TRUE_STR;
391+
const falseEventName =
392+
(eventNameToString ? eventNameToString(eventName) : eventName) + FALSE_STR;
393+
const trueEventName =
394+
(eventNameToString ? eventNameToString(eventName) : eventName) + TRUE_STR;
389395
const symbol = ZONE_SYMBOL_PREFIX + falseEventName;
390396
const symbolCapture = ZONE_SYMBOL_PREFIX + trueEventName;
391397
zoneSymbolEventNames[eventName] = {};
@@ -418,7 +424,8 @@ export function patchEventTarget(
418424
source = targetSource[eventName];
419425
}
420426
if (!source) {
421-
source = constructorName + addSource + eventName;
427+
source = constructorName + addSource +
428+
(eventNameToString ? eventNameToString(eventName) : eventName);
422429
}
423430
// do not create a new object as task.data to pass those things
424431
// just use the global shared one
@@ -556,7 +563,8 @@ export function patchEventTarget(
556563
const eventName = arguments[0];
557564

558565
const listeners: any[] = [];
559-
const tasks = findEventTasks(target, eventName);
566+
const tasks =
567+
findEventTasks(target, eventNameToString ? eventNameToString(eventName) : eventName);
560568

561569
for (let i = 0; i < tasks.length; i++) {
562570
const task: any = tasks[i];

Diff for: lib/node/events.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ Zone.__load_patch('EventEmitter', (global: any) => {
2222
return task.callback === delegate || task.callback.listener === delegate;
2323
};
2424

25+
const eventNameToString = function(eventName: string|Symbol) {
26+
if (typeof eventName === 'string') {
27+
return eventName as string;
28+
}
29+
if (!eventName) {
30+
return '';
31+
}
32+
return eventName.toString().replace('(', '_').replace(')', '_');
33+
};
34+
2535
function patchEventEmitterMethods(obj: any) {
2636
const result = patchEventTarget(global, [obj], {
2737
useG: false,
@@ -32,7 +42,8 @@ Zone.__load_patch('EventEmitter', (global: any) => {
3242
listeners: EE_LISTENERS,
3343
chkDup: false,
3444
rt: true,
35-
diff: compareTaskCallbackVsDelegate
45+
diff: compareTaskCallbackVsDelegate,
46+
eventNameToString: eventNameToString
3647
});
3748
if (result && result[0]) {
3849
obj[EE_ON] = obj[EE_ADD_LISTENER];

Diff for: test/node/events.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,16 @@ describe('nodejs EventEmitter', () => {
189189
process.on('uncaughtException', function() {});
190190
});
191191
});
192+
it('should be able to addEventListener with symbol eventName', () => {
193+
zoneA.run(() => {
194+
const testSymbol = Symbol('test');
195+
const test1Symbol = Symbol('test1');
196+
emitter.on(testSymbol, expectZoneA);
197+
emitter.on(test1Symbol, shouldNotRun);
198+
emitter.removeListener(test1Symbol, shouldNotRun);
199+
expect(emitter.listeners(testSymbol).length).toBe(1);
200+
expect(emitter.listeners(test1Symbol).length).toBe(0);
201+
emitter.emit(testSymbol, 'test value');
202+
});
203+
});
192204
});

Diff for: tsconfig-esm-node.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"lib": [
1919
"es5",
2020
"dom",
21-
"es2015.promise"
21+
"es2015.promise",
22+
"es2015.symbol"
2223
]
2324
},
2425
"exclude": [

Diff for: tsconfig-esm.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
"build",
2727
"build-esm",
2828
"dist",
29-
"lib/closure"
29+
"lib/closure",
30+
"lib/node/**",
31+
"lib/mix/**",
32+
"test/node/**",
33+
"test/node_bluebird_entry_point.ts",
34+
"test/node_entry_point.ts",
35+
"test/node_error_entry_point.ts",
36+
"test/node_tests.ts"
3037
]
3138
}

Diff for: tsconfig-node.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"lib": [
1717
"es5",
1818
"dom",
19-
"es2015.promise"
19+
"es2015.promise",
20+
"es2015.symbol"
2021
]
2122
},
2223
"exclude": [

Diff for: tsconfig.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
"build",
2424
"build-esm",
2525
"dist",
26-
"lib/closure"
26+
"lib/closure",
27+
"lib/node/**",
28+
"lib/mix/**",
29+
"test/node/**",
30+
"test/node_bluebird_entry_point.ts",
31+
"test/node_entry_point.ts",
32+
"test/node_error_entry_point.ts",
33+
"test/node_tests.ts"
2734
]
2835
}

0 commit comments

Comments
 (0)