@@ -26,7 +26,11 @@ import {CLEANUP, LView, TView} from '../render3/interfaces/view';
2626import { isPlatformBrowser } from '../render3/util/misc_utils' ;
2727import { unwrapRNode } from '../render3/util/view_utils' ;
2828
29- import { IS_EVENT_REPLAY_ENABLED , IS_GLOBAL_EVENT_DELEGATION_ENABLED } from './tokens' ;
29+ import {
30+ EVENT_REPLAY_ENABLED_DEFAULT ,
31+ IS_EVENT_REPLAY_ENABLED ,
32+ IS_GLOBAL_EVENT_DELEGATION_ENABLED ,
33+ } from './tokens' ;
3034import {
3135 GlobalEventDelegation ,
3236 sharedStashFunction ,
@@ -50,6 +54,16 @@ function isGlobalEventDelegationEnabled(injector: Injector) {
5054 return injector . get ( IS_GLOBAL_EVENT_DELEGATION_ENABLED , false ) ;
5155}
5256
57+ /**
58+ * Determines whether Event Replay feature should be activated on the client.
59+ */
60+ function shouldEnableEventReplay ( injector : Injector ) {
61+ return (
62+ injector . get ( IS_EVENT_REPLAY_ENABLED , EVENT_REPLAY_ENABLED_DEFAULT ) &&
63+ ! isGlobalEventDelegationEnabled ( injector )
64+ ) ;
65+ }
66+
5367/**
5468 * Returns a set of providers required to setup support for event replay.
5569 * Requires hydration to be enabled separately.
@@ -58,19 +72,28 @@ export function withEventReplay(): Provider[] {
5872 return [
5973 {
6074 provide : IS_EVENT_REPLAY_ENABLED ,
61- useValue : true ,
75+ useFactory : ( ) => {
76+ let isEnabled = true ;
77+ if ( isPlatformBrowser ( ) ) {
78+ // Note: globalThis[CONTRACT_PROPERTY] may be undefined in case Event Replay feature
79+ // is enabled, but there are no events configured in this application, in which case
80+ // we don't activate this feature, since there are no events to replay.
81+ const appId = inject ( APP_ID ) ;
82+ isEnabled = ! ! globalThis [ CONTRACT_PROPERTY ] ?. [ appId ] ;
83+ }
84+ return isEnabled ;
85+ } ,
6286 } ,
6387 {
6488 provide : ENVIRONMENT_INITIALIZER ,
6589 useValue : ( ) => {
6690 const injector = inject ( Injector ) ;
67- if ( isGlobalEventDelegationEnabled ( injector ) ) {
68- return ;
91+ if ( isPlatformBrowser ( injector ) && shouldEnableEventReplay ( injector ) ) {
92+ setStashFn ( ( rEl : RElement , eventName : string , listenerFn : VoidFunction ) => {
93+ sharedStashFunction ( rEl , eventName , listenerFn ) ;
94+ jsactionSet . add ( rEl as unknown as Element ) ;
95+ } ) ;
6996 }
70- setStashFn ( ( rEl : RElement , eventName : string , listenerFn : VoidFunction ) => {
71- sharedStashFunction ( rEl , eventName , listenerFn ) ;
72- jsactionSet . add ( rEl as unknown as Element ) ;
73- } ) ;
7497 } ,
7598 multi : true ,
7699 } ,
@@ -81,13 +104,14 @@ export function withEventReplay(): Provider[] {
81104 const injector = inject ( Injector ) ;
82105 const appRef = inject ( ApplicationRef ) ;
83106 return ( ) => {
107+ if ( ! shouldEnableEventReplay ( injector ) ) {
108+ return ;
109+ }
110+
84111 // Kick off event replay logic once hydration for the initial part
85112 // of the application is completed. This timing is similar to the unclaimed
86113 // dehydrated views cleanup timing.
87114 whenStable ( appRef ) . then ( ( ) => {
88- if ( isGlobalEventDelegationEnabled ( injector ) ) {
89- return ;
90- }
91115 const globalEventDelegation = injector . get ( GlobalEventDelegation ) ;
92116 initEventReplay ( globalEventDelegation , injector ) ;
93117 jsactionSet . forEach ( removeListeners ) ;
@@ -112,8 +136,6 @@ function getJsactionData(container: EarlyJsactionDataContainer) {
112136const initEventReplay = ( eventDelegation : GlobalEventDelegation , injector : Injector ) => {
113137 const appId = injector . get ( APP_ID ) ;
114138 // This is set in packages/platform-server/src/utils.ts
115- // Note: globalThis[CONTRACT_PROPERTY] may be undefined in case Event Replay feature
116- // is enabled, but there are no events configured in an application.
117139 const container = globalThis [ CONTRACT_PROPERTY ] ?. [ appId ] ;
118140 const earlyJsactionData = getJsactionData ( container ) ! ;
119141 const eventContract = ( eventDelegation . eventContract = new EventContract (
0 commit comments