File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed
Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ const LEGACY_ACTION_QRL_KEY = '__fictReactAction'
1111const ACTION_PROP_PATTERN = / ^ o n [ A - Z ] /
1212const RETRY_BASE_DELAY_MS = 100
1313const RETRY_MAX_DELAY_MS = 5_000
14+ const ACTION_HANDLER_CACHE_MAX_ENTRIES = 500
1415
1516const moduleCache = new Map < string , Promise < Record < string , unknown > > > ( )
1617const moduleRetryState = new Map < string , { failures : number ; nextRetryAt : number } > ( )
@@ -118,6 +119,12 @@ function toActionHandler(qrl: string): (...args: unknown[]) => void {
118119 } )
119120 }
120121
122+ if ( actionHandlerCache . size >= ACTION_HANDLER_CACHE_MAX_ENTRIES ) {
123+ const oldestKey = actionHandlerCache . keys ( ) . next ( ) . value as string | undefined
124+ if ( oldestKey ) {
125+ actionHandlerCache . delete ( oldestKey )
126+ }
127+ }
121128 actionHandlerCache . set ( qrl , handler )
122129 return handler
123130}
Original file line number Diff line number Diff line change @@ -90,6 +90,25 @@ describe('materializeReactProps', () => {
9090 expect ( typeof result . onAction ) . toBe ( 'function' )
9191 } )
9292
93+ it ( 'evicts oldest cached action handlers after cache limit is exceeded' , ( ) => {
94+ const firstQrl = '/mock/first-module.js#run'
95+ const firstMaterialized = materializeReactProps ( {
96+ onAction : reactActionFromQrl ( firstQrl ) ,
97+ } )
98+ const firstHandler = firstMaterialized . onAction
99+
100+ for ( let index = 0 ; index < 600 ; index += 1 ) {
101+ materializeReactProps ( {
102+ onAction : reactActionFromQrl ( `/mock/module-${ index } .js#run` ) ,
103+ } )
104+ }
105+
106+ const rematerialized = materializeReactProps ( {
107+ onAction : reactActionFromQrl ( firstQrl ) ,
108+ } )
109+ expect ( rematerialized . onAction ) . not . toBe ( firstHandler )
110+ } )
111+
93112 it ( 'ignores legacy-looking objects when additional keys are present' , ( ) => {
94113 const legacyLike = {
95114 __fictReactAction : '/mock/module.js#run' ,
You can’t perform that action at this time.
0 commit comments