feat: support passive event options by defining global variables in zone.js config file (#34503)
PR Close #34503
This commit is contained in:

committed by
Miško Hevery

parent
8cb1f65e7d
commit
d7d359e3ee
@ -257,6 +257,30 @@ 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) {
|
||||
// if there is already a task for the eventName + capture,
|
||||
// just return, because we use the shared globalZoneAwareCallback here.
|
||||
@ -338,6 +362,7 @@ export function patchEventTarget(
|
||||
(patchOptions && patchOptions.diff) ? patchOptions.diff : compareTaskCallbackVsDelegate;
|
||||
|
||||
const blackListedEvents: string[] = (Zone as any)[zoneSymbol('BLACK_LISTED_EVENTS')];
|
||||
const passiveEvents: string[] = _global[zoneSymbol('PASSIVE_EVENTS')];
|
||||
|
||||
const makeAddListener = function(
|
||||
nativeListener: any, addSource: string, customScheduleFn: any, customCancelFn: any,
|
||||
@ -373,12 +398,19 @@ export function patchEventTarget(
|
||||
}
|
||||
|
||||
const options = arguments[2];
|
||||
const passive =
|
||||
passiveSupported && !!passiveEvents && passiveEvents.indexOf(eventName) !== -1;
|
||||
|
||||
if (blackListedEvents) {
|
||||
// check black list
|
||||
for (let i = 0; i < blackListedEvents.length; i++) {
|
||||
if (eventName === blackListedEvents[i]) {
|
||||
return nativeListener.apply(this, arguments);
|
||||
if (passive) {
|
||||
const passiveOptions = buildPassiveOptions(options, passive);
|
||||
return nativeListener.call(target, eventName, delegate, passiveOptions);
|
||||
} else {
|
||||
return nativeListener.apply(this, arguments);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -431,7 +463,7 @@ export function patchEventTarget(
|
||||
}
|
||||
// do not create a new object as task.data to pass those things
|
||||
// just use the global shared one
|
||||
taskData.options = options;
|
||||
taskData.options = buildPassiveOptions(options, passive);
|
||||
if (once) {
|
||||
// if addEventListener with once options, we don't pass it to
|
||||
// native addEventListener, instead we keep the once setting
|
||||
|
Reference in New Issue
Block a user