-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds support for event proxying and event options #856
Changes from 10 commits
0ca2cc0
8019f20
e68e698
5cb05d3
7250629
d570feb
0c95262
682c440
6a65c0d
f46484b
f2f2d67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1236,6 +1236,8 @@ function wrapFunctionProperties(id: string, properties: any) { | |
return props; | ||
} | ||
|
||
type EventMapValue = { proxy: EventListener; callback: Function; options: any } | undefined; | ||
|
||
export function renderer(renderer: () => RenderResult): Renderer { | ||
let _mountOptions: MountOptions & { domNode: HTMLElement } = { | ||
sync: false, | ||
|
@@ -1248,7 +1250,7 @@ export function renderer(renderer: () => RenderResult): Renderer { | |
let _processQueue: (ProcessItem | DetachApplication | AttachApplication)[] = []; | ||
let _deferredProcessQueue: (ProcessItem | DetachApplication | AttachApplication)[] = []; | ||
let _applicationQueue: ApplicationInstruction[] = []; | ||
let _eventMap = new WeakMap<Function, EventListener>(); | ||
let _eventMap = new WeakMap<Node, { [index: string]: EventMapValue }>(); | ||
let _idToWrapperMap = new Map<string, DNodeWrapper>(); | ||
let _wrapperSiblingMap = new WeakMap<DNodeWrapper, DNodeWrapper>(); | ||
let _idToChildrenWrappers = new Map<string, DNodeWrapper[]>(); | ||
|
@@ -1282,12 +1284,10 @@ export function renderer(renderer: () => RenderResult): Renderer { | |
domNode: Node, | ||
eventName: string, | ||
currentValue: (event: Event) => void, | ||
previousValue?: Function | ||
eventOptions?: { passive: string[] } | ||
) { | ||
if (previousValue) { | ||
const previousEvent = _eventMap.get(previousValue); | ||
previousEvent && domNode.removeEventListener(eventName, previousEvent); | ||
} | ||
const proxyEvents = _eventMap.get(domNode) || {}; | ||
let proxyEvent = proxyEvents[eventName]; | ||
|
||
let callback = currentValue; | ||
|
||
|
@@ -1298,8 +1298,33 @@ export function renderer(renderer: () => RenderResult): Renderer { | |
}; | ||
} | ||
|
||
domNode.addEventListener(eventName, callback); | ||
_eventMap.set(currentValue, callback); | ||
const { passive: currentPassive = [] } = eventOptions || {}; | ||
|
||
const isPassive = currentPassive.indexOf(`on${eventName}`) !== -1; | ||
|
||
const options = { passive: isPassive }; | ||
|
||
if (proxyEvent && proxyEvent.options.passive !== isPassive) { | ||
domNode.removeEventListener(eventName, proxyEvent.proxy); | ||
proxyEvent = undefined; | ||
} | ||
|
||
if (proxyEvent) { | ||
proxyEvents[eventName] = { ...proxyEvent, callback }; | ||
_eventMap.set(domNode, proxyEvents); | ||
} else { | ||
const proxy = (...args: any[]) => { | ||
const proxyEvents = _eventMap.get(domNode) || {}; | ||
const proxyEvent = proxyEvents[eventName]; | ||
proxyEvent && proxyEvent.callback(...args); | ||
}; | ||
proxyEvents[eventName] = { callback, proxy, options }; | ||
has('dom-passive-event') | ||
? domNode.addEventListener(eventName, proxy, options) | ||
: domNode.addEventListener(eventName, proxy); | ||
|
||
_eventMap.set(domNode, proxyEvents); | ||
} | ||
} | ||
|
||
function removeOrphanedEvents( | ||
|
@@ -1312,9 +1337,13 @@ export function renderer(renderer: () => RenderResult): Renderer { | |
const isEvent = propName.substr(0, 2) === 'on' || onlyEvents; | ||
const eventName = onlyEvents ? propName : propName.substr(2); | ||
if (isEvent && !properties[propName]) { | ||
const eventCallback = _eventMap.get(previousProperties[propName]); | ||
if (eventCallback) { | ||
domNode.removeEventListener(eventName, eventCallback); | ||
const proxyEvents = _eventMap.get(domNode) || {}; | ||
let proxyEvent: { proxy: EventListener; callback: Function; options: any } | undefined = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah it can actually just be inferred, i just forgot to remove it. |
||
proxyEvents[eventName]; | ||
if (proxyEvent) { | ||
domNode.removeEventListener(eventName, proxyEvent.proxy); | ||
delete proxyEvents[eventName]; | ||
_eventMap.set(domNode, proxyEvents); | ||
} | ||
} | ||
}); | ||
|
@@ -1609,18 +1638,26 @@ export function renderer(renderer: () => RenderResult): Renderer { | |
(domNode as any)['select-value'] = propValue; | ||
} | ||
setValue(domNode, propValue, previousValue); | ||
} else if (propName !== 'key' && propValue !== previousValue) { | ||
} else if (propName !== 'key') { | ||
const type = typeof propValue; | ||
if (type === 'function' && propName.lastIndexOf('on', 0) === 0 && includesEventsAndAttributes) { | ||
updateEvent(domNode, propName.substr(2), propValue, previousValue); | ||
} else if (type === 'string' && propName !== 'innerHTML' && includesEventsAndAttributes) { | ||
updateAttribute(domNode, propName, propValue, nextWrapper.namespace); | ||
} else if (propName === 'scrollLeft' || propName === 'scrollTop') { | ||
if ((domNode as any)[propName] !== propValue) { | ||
if ( | ||
type === 'function' && | ||
propName.lastIndexOf('on', 0) === 0 && | ||
includesEventsAndAttributes && | ||
(propValue !== previousValue || properties.oneventoptions) | ||
) { | ||
updateEvent(domNode, propName.substr(2), propValue, properties.oneventoptions); | ||
} else if (propName === 'oneventoptions') { | ||
} else if (propValue !== previousValue) { | ||
if (type === 'string' && propName !== 'innerHTML' && includesEventsAndAttributes) { | ||
updateAttribute(domNode, propName, propValue, nextWrapper.namespace); | ||
} else if (propName === 'scrollLeft' || propName === 'scrollTop') { | ||
if ((domNode as any)[propName] !== propValue) { | ||
(domNode as any)[propName] = propValue; | ||
} | ||
} else { | ||
(domNode as any)[propName] = propValue; | ||
} | ||
} else { | ||
(domNode as any)[propName] = propValue; | ||
} | ||
} | ||
} | ||
|
@@ -1677,7 +1714,7 @@ export function renderer(renderer: () => RenderResult): Renderer { | |
} | ||
previousProperties.events = previousProperties.events || {}; | ||
Object.keys(events).forEach((event) => { | ||
updateEvent(next.domNode as HTMLElement, event, events[event], previousProperties.events[event]); | ||
updateEvent(next.domNode as HTMLElement, event, events[event]); | ||
}); | ||
} else { | ||
setProperties(next.domNode as HTMLElement, previousProperties.properties, next); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we type options?
passive: boolean
or similar?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to actually use
EventListenerOptions
but I think the type didn't have passive. I'll type it though.