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

Commit b038214

Browse files
JiaLiPassionmhevery
authored andcommitted
spec: fix #1195, should create new taskData if using OPTIMIZED TASKDATA in event task (#1197)
1 parent 128649a commit b038214

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

lib/zone-spec/long-stack-trace.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ function renderLongStackTrace(frames: LongStackTrace[], stack?: string): string
109109
trace.length = this.longStackTraceLimit;
110110
}
111111
if (!task.data) task.data = {};
112+
if (task.type === 'eventTask') {
113+
// Fix issue https://github.com/angular/zone.js/issues/1195,
114+
// For event task of browser, by default, all task will share a
115+
// singleton instance of data object, we should create a new one here
116+
task.data = {...task.data};
117+
}
112118
(task.data as any)[creationTrace] = trace;
113119
}
114120
return parentZoneDelegate.scheduleTask(targetZone, task);

test/zone-spec/long-stack-trace-zone.spec.ts

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

9-
import {isBrowser, zoneSymbol} from '../../lib/common/utils';
10-
import {ifEnvSupports, isSupportSetErrorStack} from '../test-util';
9+
import {isBrowser, isIE, zoneSymbol} from '../../lib/common/utils';
10+
import {ifEnvSupports, isSafari, isSupportSetErrorStack} from '../test-util';
1111

1212
const defineProperty = (Object as any)[zoneSymbol('defineProperty')] || Object.defineProperty;
1313

@@ -82,6 +82,40 @@ describe(
8282
});
8383
}));
8484

85+
it('should not overwrite long stack traces data for different optimized eventTasks',
86+
ifEnvSupports(() => isBrowser, function() {
87+
lstz.run(function() {
88+
const button = document.createElement('button');
89+
const clickEvent = document.createEvent('Event');
90+
clickEvent.initEvent('click', true, true);
91+
document.body.appendChild(button);
92+
93+
const div = document.createElement('div');
94+
const enterEvent = document.createEvent('Event');
95+
enterEvent.initEvent('mouseenter', true, true);
96+
document.body.appendChild(div);
97+
98+
button.addEventListener('click', function() {
99+
throw new Error('clickError');
100+
});
101+
102+
div.addEventListener('mouseenter', function() {
103+
throw new Error('enterError');
104+
});
105+
106+
button.dispatchEvent(clickEvent);
107+
div.dispatchEvent(enterEvent);
108+
109+
expect(log.length).toBe(2);
110+
if (!isSafari() && !isIE()) {
111+
expect(log[0].stack === log[1].stack).toBe(false);
112+
}
113+
114+
document.body.removeChild(button);
115+
document.body.removeChild(div);
116+
});
117+
}));
118+
85119
it('should produce a long stack trace even if stack setter throws', (done) => {
86120
let wasStackAssigned = false;
87121
let error = new Error('Expected error');

0 commit comments

Comments
 (0)