Revert "feat: support passive event options by defining global variables in zone.js config file (#34503)"

This reverts commit d7d359e3ee.
This commit is contained in:
Miško Hevery 2020-02-21 22:16:34 +00:00
parent e17bde99f8
commit e084835fb1
5 changed files with 9 additions and 97 deletions

View File

@ -297,24 +297,7 @@ Following is all the code discussed in this page.
</code-tabs> </code-tabs>
### Passive events
Angular also support passive event listeners. For example, we can use the following steps to make scroll event passive.
1. create a file `src/zone-flags.ts`.
2. add the following line into this file.
```
(window as any)['__zone_symbol__PASSIVE_EVENTS'] = ['scroll'];
```
3. in `src/polyfills.ts`, before import zone.js, import the new created `zone-flags`.
```
import './zone-flags';
import 'zone.js/dist/zone'; // Included with Angular CLI.
```
after those steps, if user add event listeners for the `passive` event, the listeners will be `passive`.
## Summary ## Summary

View File

@ -4,7 +4,7 @@
"uncompressed": { "uncompressed": {
"runtime-es2015": 1485, "runtime-es2015": 1485,
"main-es2015": 141569, "main-es2015": 141569,
"polyfills-es2015": 37300 "polyfills-es2015": 36657
} }
} }
}, },
@ -13,7 +13,7 @@
"uncompressed": { "uncompressed": {
"runtime-es2015": 1485, "runtime-es2015": 1485,
"main-es2015": 16514, "main-es2015": 16514,
"polyfills-es2015": 37118 "polyfills-es2015": 36657
} }
} }
}, },
@ -22,7 +22,7 @@
"uncompressed": { "uncompressed": {
"runtime-es2015": 1485, "runtime-es2015": 1485,
"main-es2015": 147647, "main-es2015": 147647,
"polyfills-es2015": 37300 "polyfills-es2015": 36657
} }
} }
}, },
@ -31,7 +31,7 @@
"uncompressed": { "uncompressed": {
"runtime-es2015": 1485, "runtime-es2015": 1485,
"main-es2015": 136777, "main-es2015": 136777,
"polyfills-es2015": 37980 "polyfills-es2015": 37334
} }
} }
}, },
@ -40,7 +40,7 @@
"uncompressed": { "uncompressed": {
"runtime-es2015": 2289, "runtime-es2015": 2289,
"main-es2015": 247198, "main-es2015": 247198,
"polyfills-es2015": 36958, "polyfills-es2015": 36657,
"5-es2015": 751 "5-es2015": 751
} }
} }
@ -50,7 +50,7 @@
"uncompressed": { "uncompressed": {
"runtime-es2015": 2289, "runtime-es2015": 2289,
"main-es2015": 226144, "main-es2015": 226144,
"polyfills-es2015": 37300, "polyfills-es2015": 36657,
"5-es2015": 779 "5-es2015": 779
} }
} }

View File

@ -257,30 +257,6 @@ export function patchEventTarget(
} }
} }
/**
*
* this util function will build an option object with passive option
* to handle all possible input from the user
*/
function buildPassiveOptions(options: any, passive: boolean) {
if (!passiveSupported || !passive) {
return options;
}
if (!options) {
return {passive: true};
}
if (typeof options === 'boolean') {
return {capture: options, passive: true};
}
if (typeof options === 'object') {
if (typeof options.passive === 'boolean' && !options.passive) {
return options;
}
return {...options, passive: true};
}
return options;
}
const customScheduleGlobal = function(task: Task) { const customScheduleGlobal = function(task: Task) {
// if there is already a task for the eventName + capture, // if there is already a task for the eventName + capture,
// just return, because we use the shared globalZoneAwareCallback here. // just return, because we use the shared globalZoneAwareCallback here.
@ -362,7 +338,6 @@ export function patchEventTarget(
(patchOptions && patchOptions.diff) ? patchOptions.diff : compareTaskCallbackVsDelegate; (patchOptions && patchOptions.diff) ? patchOptions.diff : compareTaskCallbackVsDelegate;
const blackListedEvents: string[] = (Zone as any)[zoneSymbol('BLACK_LISTED_EVENTS')]; const blackListedEvents: string[] = (Zone as any)[zoneSymbol('BLACK_LISTED_EVENTS')];
const passiveEvents: string[] = _global[zoneSymbol('PASSIVE_EVENTS')];
const makeAddListener = function( const makeAddListener = function(
nativeListener: any, addSource: string, customScheduleFn: any, customCancelFn: any, nativeListener: any, addSource: string, customScheduleFn: any, customCancelFn: any,
@ -398,22 +373,15 @@ export function patchEventTarget(
} }
const options = arguments[2]; const options = arguments[2];
const passive =
passiveSupported && !!passiveEvents && passiveEvents.indexOf(eventName) !== -1;
if (blackListedEvents) { if (blackListedEvents) {
// check black list // check black list
for (let i = 0; i < blackListedEvents.length; i++) { for (let i = 0; i < blackListedEvents.length; i++) {
if (eventName === blackListedEvents[i]) { if (eventName === blackListedEvents[i]) {
if (passive) {
const passiveOptions = buildPassiveOptions(options, passive);
return nativeListener.call(target, eventName, delegate, passiveOptions);
} else {
return nativeListener.apply(this, arguments); return nativeListener.apply(this, arguments);
} }
} }
} }
}
let capture; let capture;
let once = false; let once = false;
@ -463,7 +431,7 @@ export function patchEventTarget(
} }
// do not create a new object as task.data to pass those things // do not create a new object as task.data to pass those things
// just use the global shared one // just use the global shared one
taskData.options = buildPassiveOptions(options, passive); taskData.options = options;
if (once) { if (once) {
// if addEventListener with once options, we don't pass it to // if addEventListener with once options, we don't pass it to
// native addEventListener, instead we keep the once setting // native addEventListener, instead we keep the once setting

View File

@ -222,7 +222,6 @@ describe('Zone', function() {
}); });
zone.run(() => { document.dispatchEvent(scrollEvent); }); zone.run(() => { document.dispatchEvent(scrollEvent); });
(document as any).removeAllListeners('scroll');
}); });
it('should be able to clear on handler added before load zone.js', function() { it('should be able to clear on handler added before load zone.js', function() {
@ -800,7 +799,6 @@ describe('Zone', function() {
button.dispatchEvent(clickEvent); button.dispatchEvent(clickEvent);
expect(logs).toEqual([]); expect(logs).toEqual([]);
(document as any).removeAllListeners('click');
}); });
})); }));
@ -1037,42 +1035,6 @@ describe('Zone', function() {
button.removeEventListener('click', listener); button.removeEventListener('click', listener);
})); }));
describe('passiveEvents', () => {
let logs: string[] = [];
const listener = (e: Event) => {
logs.push(e.defaultPrevented ? 'defaultPrevented' : 'default will run');
e.preventDefault();
logs.push(e.defaultPrevented ? 'defaultPrevented' : 'default will run');
};
const testPassive = function(eventName: string, expectedPassiveLog: string, options: any) {
(button as any).addEventListener(eventName, listener, options);
const evt = document.createEvent('Event');
evt.initEvent(eventName, true, true);
button.dispatchEvent(evt);
expect(logs).toEqual(['default will run', expectedPassiveLog]);
(button as any).removeAllListeners(eventName);
};
beforeEach(() => { logs = []; });
it('should be passive with global variable defined',
() => { testPassive('touchstart', 'default will run', {passive: true}); });
it('should not be passive without global variable defined',
() => { testPassive('touchend', 'defaultPrevented', undefined); });
it('should be passive with global variable defined even without passive options',
() => { testPassive('touchstart', 'default will run', undefined); });
it('should be passive with global variable defined even without passive options and with capture',
() => { testPassive('touchstart', 'default will run', {capture: true}); });
it('should be passive with global variable defined with capture option',
() => { testPassive('touchstart', 'default will run', true); });
it('should not be passive with global variable defined with passive false option',
() => { testPassive('touchstart', 'defaultPrevented', {passive: false}); });
it('should be passive with global variable defined and also blacklisted', () => {
(document as any).removeAllListeners('scroll');
testPassive('scroll', 'default will run', undefined);
});
it('should not be passive without global variable defined and also blacklisted',
() => { testPassive('wheel', 'defaultPrevented', undefined); });
});
it('should support Event.stopImmediatePropagation', it('should support Event.stopImmediatePropagation',
ifEnvSupports(supportEventListenerOptions, function() { ifEnvSupports(supportEventListenerOptions, function() {
const hookSpy = jasmine.createSpy('hook'); const hookSpy = jasmine.createSpy('hook');

View File

@ -78,6 +78,5 @@
global['__Zone_ignore_on_properties'] = global['__Zone_ignore_on_properties'] =
[{target: TestTarget.prototype, ignoreProperties: ['prop1']}]; [{target: TestTarget.prototype, ignoreProperties: ['prop1']}];
global[zoneSymbolPrefix + 'FakeAsyncTestMacroTask'] = [{source: 'TestClass.myTimeout'}]; global[zoneSymbolPrefix + 'FakeAsyncTestMacroTask'] = [{source: 'TestClass.myTimeout'}];
global[zoneSymbolPrefix + 'UNPATCHED_EVENTS'] = ['scroll', 'wheel']; global[zoneSymbolPrefix + 'UNPATCHED_EVENTS'] = ['scroll'];
global[zoneSymbolPrefix + 'PASSIVE_EVENTS'] = ['touchstart', 'scroll'];
})(typeof window === 'object' && window || typeof self === 'object' && self || global); })(typeof window === 'object' && window || typeof self === 'object' && self || global);