Revert "fix(core): should use native addEventListener in ngZone (#20672)"

This reverts commit 65a2cb8307.
This commit is contained in:
Miško Hevery 2017-11-29 14:56:29 -06:00
parent f3c5481181
commit ba850b36de
2 changed files with 28 additions and 43 deletions

View File

@ -154,50 +154,37 @@ export class DomEventsPlugin extends EventManagerPlugin {
let callback: EventListener = handler as EventListener; let callback: EventListener = handler as EventListener;
// if zonejs is loaded and current zone is not ngZone // if zonejs is loaded and current zone is not ngZone
// we keep Zone.current on target for later restoration. // we keep Zone.current on target for later restoration.
if (zoneJsLoaded) { if (zoneJsLoaded && (!NgZone.isInAngularZone() || isBlackListedEvent(eventName))) {
if (!NgZone.isInAngularZone() || isBlackListedEvent(eventName)) { let symbolName = symbolNames[eventName];
let symbolName = symbolNames[eventName]; if (!symbolName) {
if (!symbolName) { symbolName = symbolNames[eventName] = __symbol__(ANGULAR + eventName + FALSE);
symbolName = symbolNames[eventName] = __symbol__(ANGULAR + eventName + FALSE); }
} let taskDatas: TaskData[] = (element as any)[symbolName];
let taskDatas: TaskData[] = (element as any)[symbolName]; const globalListenerRegistered = taskDatas && taskDatas.length > 0;
const globalListenerRegistered = taskDatas && taskDatas.length > 0; if (!taskDatas) {
if (!taskDatas) { taskDatas = (element as any)[symbolName] = [];
taskDatas = (element as any)[symbolName] = []; }
}
const zone = isBlackListedEvent(eventName) ? Zone.root : Zone.current; const zone = isBlackListedEvent(eventName) ? Zone.root : Zone.current;
if (taskDatas.length === 0) { if (taskDatas.length === 0) {
taskDatas.push({zone: zone, handler: callback}); taskDatas.push({zone: zone, handler: callback});
} else {
let callbackRegistered = false;
for (let i = 0; i < taskDatas.length; i++) {
if (taskDatas[i].handler === callback) {
callbackRegistered = true;
break;
}
}
if (!callbackRegistered) {
taskDatas.push({zone: zone, handler: callback});
}
}
if (!globalListenerRegistered) {
element[ADD_EVENT_LISTENER](eventName, globalListener, false);
}
} else { } else {
// if zone.js loaded and we are in angular zone, we don't need to let callbackRegistered = false;
// use zone.js patched addEventListener for (let i = 0; i < taskDatas.length; i++) {
const wrappedCallback = function() { if (taskDatas[i].handler === callback) {
return self.ngZone.run(callback, this, arguments as any); callbackRegistered = true;
}; break;
zoneJsLoaded.apply(element, [eventName, wrappedCallback, false]); }
// we just use the underlying removeEventListener }
return () => element[REMOVE_EVENT_LISTENER].apply( if (!callbackRegistered) {
element, [eventName, wrappedCallback, false]); taskDatas.push({zone: zone, handler: callback});
}
}
if (!globalListenerRegistered) {
element[ADD_EVENT_LISTENER](eventName, globalListener, false);
} }
} else { } else {
// use zone.js patched addEventListener or native addEventListener if zone.js not loaded
element[NATIVE_ADD_LISTENER](eventName, callback, false); element[NATIVE_ADD_LISTENER](eventName, callback, false);
} }
return () => this.removeEventListener(element, eventName, callback); return () => this.removeEventListener(element, eventName, callback);
@ -209,8 +196,6 @@ export class DomEventsPlugin extends EventManagerPlugin {
if (!underlyingRemove) { if (!underlyingRemove) {
return target[NATIVE_REMOVE_LISTENER].apply(target, [eventName, callback, false]); return target[NATIVE_REMOVE_LISTENER].apply(target, [eventName, callback, false]);
} }
// if zone.js loaded and wrappedCallback not exists, the callback was added in different zone
let symbolName = symbolNames[eventName]; let symbolName = symbolNames[eventName];
let taskDatas: TaskData[] = symbolName && target[symbolName]; let taskDatas: TaskData[] = symbolName && target[symbolName];
if (!taskDatas) { if (!taskDatas) {

View File

@ -283,7 +283,7 @@ export function main() {
}); });
getDOM().dispatchEvent(element, dispatchedEvent); getDOM().dispatchEvent(element, dispatchedEvent);
expect(receivedEvents).toEqual([dispatchedEvent, dispatchedEvent]); expect(receivedEvents).toEqual([dispatchedEvent, dispatchedEvent]);
expect(receivedZones).toEqual([Zone.root.name, 'angular']); expect(receivedZones).toEqual([Zone.root.name, 'fakeAngularZone']);
receivedEvents = []; receivedEvents = [];
remover1 && remover1(); remover1 && remover1();