Revert "fix(zone.js): should continue to executue listeners when throw error (#41562)" (#41707)

This reverts commit 5c48cd30b5.

Reason: that change introduces race conditions on CI.

PR Close #41707
This commit is contained in:
Andrew Kushnir 2021-04-19 11:45:20 -07:00
parent baa8c70416
commit d58747de8a
11 changed files with 64 additions and 154 deletions

View File

@ -66,7 +66,7 @@ Zone.__load_patch('EventTarget', (global: any, Zone: ZoneType, api: _ZonePrivate
// patch XMLHttpRequestEventTarget's addEventListener/removeEventListener
const XMLHttpRequestEventTarget = (global as any)['XMLHttpRequestEventTarget'];
if (XMLHttpRequestEventTarget && XMLHttpRequestEventTarget.prototype) {
api.patchEventTarget(global, api, [XMLHttpRequestEventTarget.prototype]);
api.patchEventTarget(global, [XMLHttpRequestEventTarget.prototype]);
}
});

View File

@ -112,7 +112,7 @@ export function eventTargetLegacyPatch(_global: any, api: _ZonePrivate) {
}
// vh is validateHandler to check event handler
// is valid or not(for security check)
api.patchEventTarget(_global, api, apiTypes, {
api.patchEventTarget(_global, apiTypes, {
vh: checkIEAndCrossContext,
transferEventName: (eventName: string) => {
const pointerEventName = pointerEventsMap[eventName];

View File

@ -29,7 +29,7 @@ export function eventTargetPatch(_global: any, api: _ZonePrivate) {
if (!EVENT_TARGET || !EVENT_TARGET.prototype) {
return;
}
api.patchEventTarget(_global, api, [EVENT_TARGET && EVENT_TARGET.prototype]);
api.patchEventTarget(_global, [EVENT_TARGET && EVENT_TARGET.prototype]);
return true;
}

View File

@ -20,7 +20,7 @@ Zone.__load_patch('shadydom', (global: any, Zone: ZoneType, api: _ZonePrivate) =
if (proto && proto.hasOwnProperty('addEventListener')) {
proto[Zone.__symbol__('addEventListener')] = null;
proto[Zone.__symbol__('removeEventListener')] = null;
api.patchEventTarget(global, api, [proto]);
api.patchEventTarget(global, [proto]);
}
});
});

View File

@ -22,5 +22,5 @@ Zone.__load_patch('RTCPeerConnection', (global: any, Zone: ZoneType, api: _ZoneP
RTCPeerConnection.prototype[addSymbol] = null;
RTCPeerConnection.prototype[removeSymbol] = null;
api.patchEventTarget(global, api, [RTCPeerConnection.prototype], {useG: false});
api.patchEventTarget(global, [RTCPeerConnection.prototype], {useG: false});
});

View File

@ -13,7 +13,7 @@ export function apply(api: _ZonePrivate, _global: any) {
// On Safari window.EventTarget doesn't exist so need to patch WS add/removeEventListener
// On older Chrome, no need since EventTarget was already patched
if (!(<any>_global).EventTarget) {
api.patchEventTarget(_global, api, [WS.prototype]);
api.patchEventTarget(_global, [WS.prototype]);
}
(<any>_global).WebSocket = function(x: any, y: any) {
const socket = arguments.length > 1 ? new WS(x, y) : new WS(x);

View File

@ -86,7 +86,7 @@ export interface PatchEventTargetOptions {
}
export function patchEventTarget(
_global: any, api: _ZonePrivate, apis: any[], patchOptions?: PatchEventTargetOptions) {
_global: any, apis: any[], patchOptions?: PatchEventTargetOptions) {
const ADD_EVENT_LISTENER = (patchOptions && patchOptions.add) || ADD_EVENT_LISTENER_STR;
const REMOVE_EVENT_LISTENER = (patchOptions && patchOptions.rm) || REMOVE_EVENT_LISTENER_STR;
@ -114,15 +114,7 @@ export function patchEventTarget(
task.originalDelegate = delegate;
}
// invoke static task.invoke
// need to try/catch error here, otherwise, the error in one event listener
// will break the executions of the other event listeners. Also error will
// not remove the event listener when `once` options is true.
let error;
try {
task.invoke(task, target, [event]);
} catch (err) {
error = err;
}
const options = task.options;
if (options && typeof options === 'object' && options.once) {
// if options.once is true, after invoke once remove listener here
@ -131,10 +123,10 @@ export function patchEventTarget(
const delegate = task.originalDelegate ? task.originalDelegate : task.callback;
target[REMOVE_EVENT_LISTENER].call(target, event.type, delegate, options);
}
return error;
};
function globalCallback(context: unknown, event: Event, isCapture: boolean) {
// global shared zoneAwareCallback to handle all event callback with capture = false
const globalZoneAwareCallback = function(this: unknown, event: Event) {
// https://github.com/angular/zone.js/issues/911, in IE, sometimes
// event will be undefined, so we need to use window.event
event = event || _global.event;
@ -143,8 +135,8 @@ export function patchEventTarget(
}
// event.target is needed for Samsung TV and SourceBuffer
// || global is needed https://github.com/angular/zone.js/issues/190
const target: any = context || event.target || _global;
const tasks = target[zoneSymbolEventNames[event.type][isCapture ? TRUE_STR : FALSE_STR]];
const target: any = this || event.target || _global;
const tasks = target[zoneSymbolEventNames[event.type][FALSE_STR]];
if (tasks) {
// invoke all tasks which attached to current target with given event.type and capture = false
// for performance concern, if task.length === 1, just invoke
@ -155,32 +147,46 @@ export function patchEventTarget(
// copy the tasks array before invoke, to avoid
// the callback will remove itself or other listener
const copyTasks = tasks.slice();
const errors = [];
for (let i = 0; i < copyTasks.length; i++) {
if (event && (event as any)[IMMEDIATE_PROPAGATION_SYMBOL] === true) {
break;
}
const err = invokeTask(copyTasks[i], target, event);
err && errors.push(err);
}
for (let i = 0; i < errors.length; i++) {
const err = errors[i];
api.nativeScheduleMicroTask(() => {
throw err;
});
invokeTask(copyTasks[i], target, event);
}
}
}
}
// global shared zoneAwareCallback to handle all event callback with capture = false
const globalZoneAwareCallback = function(this: unknown, event: Event) {
return globalCallback(this, event, false);
};
// global shared zoneAwareCallback to handle all event callback with capture = true
const globalZoneAwareCaptureCallback = function(this: unknown, event: Event) {
return globalCallback(this, event, true);
// https://github.com/angular/zone.js/issues/911, in IE, sometimes
// event will be undefined, so we need to use window.event
event = event || _global.event;
if (!event) {
return;
}
// event.target is needed for Samsung TV and SourceBuffer
// || global is needed https://github.com/angular/zone.js/issues/190
const target: any = this || event.target || _global;
const tasks = target[zoneSymbolEventNames[event.type][TRUE_STR]];
if (tasks) {
// invoke all tasks which attached to current target with given event.type and capture = false
// for performance concern, if task.length === 1, just invoke
if (tasks.length === 1) {
invokeTask(tasks[0], target, event);
} else {
// https://github.com/angular/zone.js/issues/836
// copy the tasks array before invoke, to avoid
// the callback will remove itself or other listener
const copyTasks = tasks.slice();
for (let i = 0; i < copyTasks.length; i++) {
if (event && (event as any)[IMMEDIATE_PROPAGATION_SYMBOL] === true) {
break;
}
invokeTask(copyTasks[i], target, event);
}
}
}
};
function patchEventTargetMethods(obj: any, patchOptions?: PatchEventTargetOptions) {

View File

@ -8,7 +8,7 @@
Zone.__load_patch('socketio', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
(Zone as any)[Zone.__symbol__('socketio')] = function patchSocketIO(io: any) {
// patch io.Socket.prototype event listener related method
api.patchEventTarget(global, api, [io.Socket.prototype], {
api.patchEventTarget(global, [io.Socket.prototype], {
useG: false,
chkDup: false,
rt: true,

View File

@ -8,7 +8,7 @@
import {patchEventTarget} from '../common/events';
Zone.__load_patch('EventEmitter', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
Zone.__load_patch('EventEmitter', (global: any) => {
// For EventEmitter
const EE_ADD_LISTENER = 'addListener';
const EE_PREPEND_LISTENER = 'prependListener';
@ -34,7 +34,7 @@ Zone.__load_patch('EventEmitter', (global: any, Zone: ZoneType, api: _ZonePrivat
};
function patchEventEmitterMethods(obj: any) {
const result = patchEventTarget(global, api, [obj], {
const result = patchEventTarget(global, [obj], {
useG: false,
add: EE_ADD_LISTENER,
rm: EE_REMOVE_LISTENER,

View File

@ -337,7 +337,7 @@ interface _ZonePrivate {
onUnhandledError: (error: Error) => void;
microtaskDrainDone: () => void;
showUncaughtError: () => boolean;
patchEventTarget: (global: any, api: _ZonePrivate, apis: any[], options?: any) => boolean[];
patchEventTarget: (global: any, apis: any[], options?: any) => boolean[];
patchOnProperties: (obj: any, properties: string[]|null, prototype?: any) => void;
patchThen: (ctro: Function) => void;
patchMethod:
@ -359,7 +359,6 @@ interface _ZonePrivate {
filterProperties: (target: any, onProperties: string[], ignoreProperties: any[]) => string[];
attachOriginToPatched: (target: any, origin: any) => void;
_redefineProperty: (target: any, callback: string, desc: any) => void;
nativeScheduleMicroTask: (func: Function) => void;
patchCallbacks:
(api: _ZonePrivate, target: any, targetName: string, method: string,
callbacks: string[]) => void;
@ -1344,7 +1343,11 @@ const Zone: ZoneType = (function(global: any) {
let _isDrainingMicrotaskQueue: boolean = false;
let nativeMicroTaskQueuePromise: any;
function nativeScheduleMicroTask(func: Function) {
function scheduleMicroTask(task?: MicroTask) {
// if we are not running in any task, and there has not been anything scheduled
// we must bootstrap the initial task creation by manually scheduling the drain
if (_numberOfNestedTaskFrames === 0 && _microTaskQueue.length === 0) {
// We are not running in Task, so we need to kickstart the microtask queue.
if (!nativeMicroTaskQueuePromise) {
if (global[symbolPromise]) {
nativeMicroTaskQueuePromise = global[symbolPromise].resolve(0);
@ -1357,19 +1360,11 @@ const Zone: ZoneType = (function(global: any) {
// issue 1078
nativeThen = nativeMicroTaskQueuePromise['then'];
}
nativeThen.call(nativeMicroTaskQueuePromise, func);
nativeThen.call(nativeMicroTaskQueuePromise, drainMicroTaskQueue);
} else {
global[symbolSetTimeout](func, 0);
global[symbolSetTimeout](drainMicroTaskQueue, 0);
}
}
function scheduleMicroTask(task?: MicroTask) {
// if we are not running in any task, and there has not been anything scheduled
// we must bootstrap the initial task creation by manually scheduling the drain
if (_numberOfNestedTaskFrames === 0 && _microTaskQueue.length === 0) {
// We are not running in Task, so we need to kickstart the microtask queue.
nativeScheduleMicroTask(drainMicroTaskQueue);
}
task && _microTaskQueue.push(task);
}
@ -1433,8 +1428,7 @@ const Zone: ZoneType = (function(global: any) {
filterProperties: () => [],
attachOriginToPatched: () => noop,
_redefineProperty: () => noop,
patchCallbacks: () => noop,
nativeScheduleMicroTask: nativeScheduleMicroTask
patchCallbacks: () => noop
};
let _currentZoneFrame: _ZoneFrame = {parent: null, zone: new Zone(null, null)};
let _currentTask: Task|null = null;

View File

@ -348,7 +348,7 @@ describe('Zone', function() {
(HTMLSpanElement.prototype as any)[zoneSymbol('addEventListener')] = null;
patchEventTarget(window, null as any, [HTMLSpanElement.prototype]);
patchEventTarget(window, [HTMLSpanElement.prototype]);
const span = document.createElement('span');
document.body.appendChild(span);
@ -1037,7 +1037,7 @@ describe('Zone', function() {
}));
it('should change options to boolean if not support passive', () => {
patchEventTarget(window, null as any, [TestEventListener.prototype]);
patchEventTarget(window, [TestEventListener.prototype]);
const testEventListener = new TestEventListener();
const listener = function() {};
@ -2490,96 +2490,6 @@ describe('Zone', function() {
expect(hookSpy).not.toHaveBeenCalled();
expect(logs).toEqual([]);
}));
it('should be able to continue to invoke remaining listeners even some listener throw error',
function(done: DoneFn) {
let logs: string[] = [];
const listener1 = function() {
logs.push('listener1');
};
const listener2 = function() {
throw new Error('test1');
};
const listener3 = function() {
throw new Error('test2');
};
const listener4 = {
handleEvent: function() {
logs.push('listener2');
}
};
button.addEventListener('click', listener1);
button.addEventListener('click', listener2);
button.addEventListener('click', listener3);
button.addEventListener('click', listener4);
const mouseEvent = document.createEvent('MouseEvent');
mouseEvent.initEvent('click', true, true);
const unhandledRejection = (e: PromiseRejectionEvent) => {
logs.push(e.reason.message);
};
window.addEventListener('unhandledrejection', unhandledRejection);
button.dispatchEvent(mouseEvent);
expect(logs).toEqual(['listener1', 'listener2']);
setTimeout(() => {
expect(logs).toEqual(['listener1', 'listener2', 'test1', 'test2']);
window.removeEventListener('unhandledrejection', unhandledRejection);
done()
});
});
it('should be able to continue to invoke remaining listeners even some listener throw error in the different zones',
function(done: DoneFn) {
let logs: string[] = [];
const zone1 = Zone.current.fork({
name: 'zone1',
onHandleError: (delegate, curr, target, error) => {
logs.push(error.message);
return false;
}
});
const listener1 = function() {
logs.push('listener1');
};
const listener2 = function() {
throw new Error('test1');
};
const listener3 = function() {
throw new Error('test2');
};
const listener4 = {
handleEvent: function() {
logs.push('listener2');
}
};
button.addEventListener('click', listener1);
zone1.run(() => {
button.addEventListener('click', listener2);
});
button.addEventListener('click', listener3);
button.addEventListener('click', listener4);
const mouseEvent = document.createEvent('MouseEvent');
mouseEvent.initEvent('click', true, true);
const unhandledRejection = (e: PromiseRejectionEvent) => {
logs.push(e.reason.message);
};
window.addEventListener('unhandledrejection', unhandledRejection);
button.dispatchEvent(mouseEvent);
expect(logs).toEqual(['listener1', 'test1', 'listener2']);
setTimeout(() => {
expect(logs).toEqual(['listener1', 'test1', 'listener2', 'test2']);
done()
});
});
});
// TODO: Re-enable via https://github.com/angular/angular/pull/41526