@@ -316,3 +316,107 @@ test("keyed deps changes force loading while retaining previous data until refet
316316 } ) ;
317317 container . remove ( ) ;
318318} ) ;
319+
320+ test ( "changing pollMs keeps cached data without a cold refetch" , async ( ) => {
321+ const { createRoot } = await import ( "react-dom/client" ) ;
322+ const container = document . createElement ( "div" ) ;
323+ document . body . append ( container ) ;
324+
325+ const KEY = `pollms-churn-${ Date . now ( ) } ` ;
326+ let fetches = 0 ;
327+ type Snap = { data : string | undefined ; loading : boolean } ;
328+ let snap : Snap = { data : undefined , loading : false } ;
329+ let setPollMs ! : ( value : number ) => void ;
330+
331+ function Page ( ) {
332+ const [ pollMs , setPollMsState ] = useState ( 40 ) ;
333+ setPollMs = setPollMsState ;
334+ const resource = useClientResource (
335+ KEY ,
336+ async ( ) => {
337+ fetches += 1 ;
338+ return "cached" ;
339+ } ,
340+ { pollMs } ,
341+ ) ;
342+ useEffect ( ( ) => {
343+ snap = { data : resource . data , loading : resource . loading } ;
344+ } , [ resource . data , resource . loading ] ) ;
345+ return < span /> ;
346+ }
347+
348+ let root ! : Root ;
349+ await act ( async ( ) => {
350+ root = createRoot ( container ) ;
351+ root . render ( < Page /> ) ;
352+ } ) ;
353+
354+ await waitFor ( ( ) => snap . data === "cached" && snap . loading === false ) ;
355+ expect ( fetches ) . toBe ( 1 ) ;
356+
357+ await act ( async ( ) => {
358+ setPollMs ( 80 ) ;
359+ } ) ;
360+
361+ // Subscribe identity churn must not wipe the store or force a cold fetch.
362+ await act ( async ( ) => {
363+ await new Promise < void > ( ( resolve ) => testWindow . setTimeout ( resolve , 20 ) ) ;
364+ } ) ;
365+ expect ( snap ) . toEqual ( { data : "cached" , loading : false } ) ;
366+ expect ( fetches ) . toBe ( 1 ) ;
367+
368+ await act ( async ( ) => {
369+ root . unmount ( ) ;
370+ } ) ;
371+ container . remove ( ) ;
372+ } ) ;
373+
374+ test ( "store is evicted after the last subscriber leaves" , async ( ) => {
375+ const { createRoot } = await import ( "react-dom/client" ) ;
376+ const container = document . createElement ( "div" ) ;
377+ document . body . append ( container ) ;
378+
379+ const KEY = `store-evict-${ Date . now ( ) } ` ;
380+ let fetches = 0 ;
381+ type Snap = { data : string | undefined } ;
382+ let snap : Snap = { data : undefined } ;
383+
384+ function Page ( ) {
385+ const resource = useClientResource ( KEY , async ( ) => {
386+ fetches += 1 ;
387+ return `v${ fetches } ` ;
388+ } ) ;
389+ useEffect ( ( ) => {
390+ snap = { data : resource . data } ;
391+ } , [ resource . data ] ) ;
392+ return < span /> ;
393+ }
394+
395+ let root ! : Root ;
396+ await act ( async ( ) => {
397+ root = createRoot ( container ) ;
398+ root . render ( < Page /> ) ;
399+ } ) ;
400+ await waitFor ( ( ) => snap . data === "v1" ) ;
401+ expect ( fetches ) . toBe ( 1 ) ;
402+
403+ await act ( async ( ) => {
404+ root . unmount ( ) ;
405+ } ) ;
406+ // Flush deferred eviction.
407+ await act ( async ( ) => {
408+ await new Promise < void > ( ( resolve ) => testWindow . setTimeout ( resolve , 0 ) ) ;
409+ } ) ;
410+
411+ await act ( async ( ) => {
412+ root = createRoot ( container ) ;
413+ root . render ( < Page /> ) ;
414+ } ) ;
415+ await waitFor ( ( ) => snap . data === "v2" ) ;
416+ expect ( fetches ) . toBe ( 2 ) ;
417+
418+ await act ( async ( ) => {
419+ root . unmount ( ) ;
420+ } ) ;
421+ container . remove ( ) ;
422+ } ) ;
0 commit comments