Skip to content

Commit d75adc5

Browse files
iterianidylhunn
authored andcommitted
refactor(core): Add additional cleanups to PR- Simplify event handler extraction logic. (#55752)
This should have been part of an earlier commit, but was not merged. PR Close #55752
1 parent ae0baa2 commit d75adc5

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

packages/core/src/hydration/event_replay.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {inject} from '../di/injector_compatibility';
2222
import {Provider} from '../di/interface/provider';
2323
import {setDisableEventReplayImpl} from '../render3/instructions/listener';
2424
import {TNode, TNodeType} from '../render3/interfaces/node';
25-
import {RNode} from '../render3/interfaces/renderer_dom';
25+
import {RElement, RNode} from '../render3/interfaces/renderer_dom';
2626
import {CLEANUP, LView, TView} from '../render3/interfaces/view';
2727
import {isPlatformBrowser} from '../render3/util/misc_utils';
2828
import {unwrapRNode} from '../render3/util/view_utils';
@@ -42,6 +42,11 @@ function getJsactionData(container: EarlyJsactionDataContainer) {
4242
}
4343

4444
const JSACTION_ATTRIBUTE = 'jsaction';
45+
46+
/**
47+
* Associates a DOM element with `jsaction` attribute to a map that contains info about all event
48+
* types (event names) and corresponding listeners.
49+
*/
4550
const jsactionMap: Map<Element, Map<string, Function[]>> = new Map();
4651

4752
/**
@@ -57,18 +62,19 @@ export function withEventReplay(): Provider[] {
5762
{
5863
provide: ENVIRONMENT_INITIALIZER,
5964
useValue: () => {
60-
setDisableEventReplayImpl((rEl, eventName, listenerFn) => {
65+
setDisableEventReplayImpl((rEl: RElement, eventName: string, listenerFn: VoidFunction) => {
6166
if (rEl.hasAttribute(JSACTION_ATTRIBUTE)) {
62-
const el = unwrapRNode(rEl) as Element;
67+
const el = rEl as unknown as Element;
6368
// We don't immediately remove the attribute here because
6469
// we need it for replay that happens after hydration.
6570
if (!jsactionMap.has(el)) {
6671
jsactionMap.set(el, new Map());
6772
}
68-
if (!jsactionMap.get(el)!.has(eventName)) {
69-
jsactionMap.get(el)!.set(eventName, []);
73+
const eventMap = jsactionMap.get(el)!;
74+
if (!eventMap.has(eventName)) {
75+
eventMap.set(eventName, []);
7076
}
71-
jsactionMap.get(el)!.get(eventName)!.push(listenerFn);
77+
eventMap.get(eventName)!.push(listenerFn);
7278
}
7379
});
7480
},
@@ -192,7 +198,7 @@ export function setJSActionAttribute(
192198
}
193199

194200
function handleEvent(event: EventInfoWrapper) {
195-
const nativeElement = unwrapRNode(event.getAction()!.element) as Element;
201+
const nativeElement = event.getAction()!.element as Element;
196202
const handlerFns = jsactionMap.get(nativeElement)?.get(event.getEventType());
197203
if (!handlerFns) {
198204
return;

0 commit comments

Comments
 (0)