MCPcopy Create free account
hub / github.com/angular/angular / withEventReplay

Function withEventReplay

packages/core/src/hydration/event_replay.ts:78–213  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

76 *
77 */
78export function withEventReplay(): Provider[] {
79 const providers: Provider[] = [
80 {
81 provide: IS_EVENT_REPLAY_ENABLED,
82 useFactory: () => {
83 let isEnabled = true;
84 if (typeof ngServerMode === 'undefined' || !ngServerMode) {
85 // Note: globalThis[CONTRACT_PROPERTY] may be undefined in case Event Replay feature
86 // is enabled, but there are no events configured in this application, in which case
87 // we don't activate this feature, since there are no events to replay.
88 const appId = inject(APP_ID);
89 isEnabled = !!window._ejsas?.[appId];
90 }
91 if (isEnabled) {
92 performanceMarkFeature('NgEventReplay');
93 }
94 return isEnabled;
95 },
96 },
97 ];
98
99 if (typeof ngServerMode === 'undefined' || !ngServerMode) {
100 providers.push(
101 {
102 provide: ENVIRONMENT_INITIALIZER,
103 useValue: () => {
104 const appRef = inject(ApplicationRef);
105 const {injector} = appRef;
106 // We have to check for the appRef here due to the possibility of multiple apps
107 // being present on the same page. We only want to enable event replay for the
108 // apps that actually want it.
109 if (!appsWithEventReplay.has(appRef)) {
110 const jsActionMap = inject(JSACTION_BLOCK_ELEMENT_MAP);
111 if (shouldEnableEventReplay(injector)) {
112 enableStashEventListenerImpl();
113 const appId = injector.get(APP_ID);
114 const clearStashFn = setStashFn(
115 appId,
116 (rEl: RNode, eventName: string, listenerFn: VoidFunction) => {
117 // If a user binds to a ng-container and uses a directive that binds using a host listener,
118 // this element could be a comment node. So we need to ensure we have an actual element
119 // node with a `jsaction` attribute before stashing anything.
120 if (
121 (rEl as Node).nodeType !== Node.ELEMENT_NODE ||
122 !(rEl as Element).hasAttribute(Attribute.JSACTION)
123 ) {
124 return;
125 }
126 sharedStashFunction(rEl as RElement, eventName, listenerFn);
127 sharedMapFunction(rEl as RElement, jsActionMap);
128 },
129 );
130 // Clean up the reference to the function set by the environment initializer,
131 // as the function closure may capture injected elements and prevent them
132 // from being properly garbage collected.
133 appRef.onDestroy(clearStashFn);
134 }
135 }

Callers 1

withIncrementalHydrationFunction · 0.90

Calls 15

injectFunction · 0.90
performanceMarkFeatureFunction · 0.90
setStashFnFunction · 0.90
sharedStashFunctionFunction · 0.90
sharedMapFunctionFunction · 0.90
shouldEnableEventReplayFunction · 0.85
initEventReplayFunction · 0.85
hasMethod · 0.65
getMethod · 0.65

Tested by

no test coverage detected