[IMP] bdom: support multiple synthetic events on one node

This commit is contained in:
Bruno Boi
2021-11-08 12:12:44 +01:00
committed by Aaron Bohy
parent 8a84b5be56
commit 16f1e2c237
2 changed files with 80 additions and 6 deletions
+11 -5
View File
@@ -47,14 +47,18 @@ function createElementHandler(evName: string, capture: boolean = false): EventHa
// Synthetic handler: a form of event delegation that allows placing only one
// listener per event type.
let nextSyntheticEventId = 1;
function createSyntheticHandler(evName: string, capture: boolean = false): EventHandlerCreator {
let eventKey = `__event__synthetic_${evName}`;
if (capture) {
eventKey = `${eventKey}_capture`;
}
setupSyntheticEvent(evName, eventKey, capture);
const currentId = nextSyntheticEventId++;
function setup(this: HTMLElement, data: any) {
(this as any)[eventKey] = data;
const _data = (this as any)[eventKey] || {};
_data[currentId] = data;
(this as any)[eventKey] = _data;
}
return { setup, update: setup };
}
@@ -62,10 +66,12 @@ function createSyntheticHandler(evName: string, capture: boolean = false): Event
function nativeToSyntheticEvent(eventKey: string, event: Event) {
let dom = event.target;
while (dom !== null) {
const data = (dom as any)[eventKey];
if (data) {
const stopped = config.mainEventHandler(data, event, dom);
if (stopped) return;
const _data = (dom as any)[eventKey];
if (_data) {
for (const data of Object.values(_data)) {
const stopped = config.mainEventHandler(data, event, dom);
if (stopped) return;
}
}
dom = (dom as any).parentNode;
}