@@ -13,6 +13,7 @@ import {
1313 useDeferredValue ,
1414 useId ,
1515 useImperativeHandle ,
16+ useLayoutEffect ,
1617 useReducer ,
1718 useState ,
1819 useSyncExternalStore ,
@@ -674,7 +675,7 @@ describe('Hooks', () => {
674675 let count = 0
675676 const unsubscribe = vi . fn ( )
676677 const subscribe = vi . fn ( ( ) => unsubscribe )
677- const getSnapshot = vi . fn ( ( ) => count ++ )
678+ const getSnapshot = vi . fn ( ( ) => count )
678679 const SubApp = ( ) => {
679680 const count = useSyncExternalStore ( subscribe , getSnapshot )
680681 return < div > { count } </ div >
@@ -695,16 +696,138 @@ describe('Hooks', () => {
695696 await new Promise ( ( r ) => setTimeout ( r ) )
696697 expect ( root . innerHTML ) . toBe ( '<button>toggle</button>' )
697698 expect ( unsubscribe ) . toBeCalled ( )
699+ count = 1
698700 root . querySelector ( 'button' ) ?. click ( )
699701 await new Promise ( ( r ) => setTimeout ( r ) )
700702 expect ( root . innerHTML ) . toBe ( '<div>1</div><button>toggle</button>' )
701703 } )
702704
705+ it ( 'updates the snapshot when subscribe changes' , async ( ) => {
706+ const unsubscribeA = vi . fn ( )
707+ const subscribeA = vi . fn ( ( ) => unsubscribeA )
708+ const unsubscribeB = vi . fn ( )
709+ const subscribeB = vi . fn ( ( ) => unsubscribeB )
710+ const stores = [
711+ { subscribe : subscribeA , getSnapshot : ( ) => 'a1' } ,
712+ { subscribe : subscribeB , getSnapshot : ( ) => 'b0' } ,
713+ ]
714+
715+ const App = ( ) => {
716+ const [ index , setIndex ] = useState ( 0 )
717+ const store = stores [ index ]
718+ const value = useSyncExternalStore ( store . subscribe , store . getSnapshot )
719+ return < button onClick = { ( ) => setIndex ( 1 ) } > { value } </ button >
720+ }
721+
722+ render ( < App /> , root )
723+ await new Promise ( ( r ) => setTimeout ( r ) )
724+ expect ( root . innerHTML ) . toBe ( '<button>a1</button>' )
725+ expect ( subscribeA ) . toBeCalledTimes ( 1 )
726+
727+ root . querySelector ( 'button' ) ?. click ( )
728+ await Promise . resolve ( )
729+ expect ( root . innerHTML ) . toBe ( '<button>b0</button>' )
730+ // the old store stays subscribed until the new subscription is set up at effect flush
731+ expect ( unsubscribeA ) . not . toBeCalled ( )
732+ await new Promise ( ( r ) => setTimeout ( r ) )
733+ expect ( unsubscribeA ) . toBeCalledTimes ( 1 )
734+ expect ( subscribeB ) . toBeCalledTimes ( 1 )
735+ expect ( unsubscribeB ) . not . toBeCalled ( )
736+ } )
737+
738+ it ( 'updates the snapshot when getSnapshot changes' , async ( ) => {
739+ const state : Record < string , string > = { a : 'a0' , b : 'b0' }
740+ const listeners = new Set < ( ) => void > ( )
741+ const subscribe = vi . fn ( ( listener : ( ) => void ) => {
742+ listeners . add ( listener )
743+ return ( ) => listeners . delete ( listener )
744+ } )
745+
746+ const App = ( ) => {
747+ const [ key , setKey ] = useState ( 'a' )
748+ const value = useSyncExternalStore ( subscribe , ( ) => state [ key ] )
749+ return < button onClick = { ( ) => setKey ( 'b' ) } > { value } </ button >
750+ }
751+
752+ render ( < App /> , root )
753+ await new Promise ( ( r ) => setTimeout ( r ) )
754+ expect ( root . innerHTML ) . toBe ( '<button>a0</button>' )
755+ expect ( subscribe ) . toBeCalledTimes ( 1 )
756+
757+ root . querySelector ( 'button' ) ?. click ( )
758+ await new Promise ( ( r ) => setTimeout ( r ) )
759+ expect ( root . innerHTML ) . toBe ( '<button>b0</button>' )
760+ expect ( subscribe ) . toBeCalledTimes ( 1 )
761+
762+ state . b = 'b1'
763+ listeners . forEach ( ( listener ) => listener ( ) )
764+ await new Promise ( ( r ) => setTimeout ( r ) )
765+ expect ( root . innerHTML ) . toBe ( '<button>b1</button>' )
766+ } )
767+
768+ it ( 'uses the latest getSnapshot when subscribing after a layout effect update' , async ( ) => {
769+ const state = { a : 'a0' , b : 'b0' }
770+ const subscribe = vi . fn ( ( _listener : ( ) => void ) => vi . fn ( ) )
771+
772+ const App = ( ) => {
773+ const [ key , setKey ] = useState < keyof typeof state > ( 'a' )
774+ const value = useSyncExternalStore ( subscribe , ( ) => state [ key ] )
775+
776+ useLayoutEffect ( ( ) => {
777+ setKey ( 'b' )
778+ queueMicrotask ( ( ) => {
779+ state . b = 'b1'
780+ } )
781+ } , [ ] )
782+
783+ return < div > { value } </ div >
784+ }
785+
786+ render ( < App /> , root )
787+ expect ( root . innerHTML ) . toBe ( '<div>a0</div>' )
788+
789+ await new Promise ( ( r ) => setTimeout ( r ) )
790+ expect ( root . innerHTML ) . toBe ( '<div>b1</div>' )
791+ } )
792+
793+ it ( 'does not loop when subscribe changes on every render' , async ( ) => {
794+ const unsubscribes : ReturnType < typeof vi . fn > [ ] = [ ]
795+ const subscribe = vi . fn ( ( _listener : ( ) => void ) => {
796+ const unsubscribe = vi . fn ( )
797+ unsubscribes . push ( unsubscribe )
798+ return unsubscribe
799+ } )
800+ const getSnapshot = ( ) => 'snapshot'
801+ let renderCount = 0
802+
803+ const App = ( ) => {
804+ const [ count , setCount ] = useState ( 0 )
805+ renderCount ++
806+ const value = useSyncExternalStore ( ( listener ) => subscribe ( listener ) , getSnapshot )
807+ return < button onClick = { ( ) => setCount ( ( count ) => count + 1 ) } > { `${ value } :${ count } ` } </ button >
808+ }
809+
810+ render ( < App /> , root )
811+ await new Promise ( ( r ) => setTimeout ( r ) )
812+ expect ( root . innerHTML ) . toBe ( '<button>snapshot:0</button>' )
813+ expect ( renderCount ) . toBe ( 1 )
814+ expect ( subscribe ) . toBeCalledTimes ( 1 )
815+
816+ root . querySelector ( 'button' ) ?. click ( )
817+ await Promise . resolve ( )
818+ await new Promise ( ( r ) => setTimeout ( r ) )
819+ expect ( root . innerHTML ) . toBe ( '<button>snapshot:1</button>' )
820+ expect ( renderCount ) . toBe ( 2 )
821+ expect ( subscribe ) . toBeCalledTimes ( 2 )
822+ expect ( unsubscribes [ 0 ] ) . toBeCalledTimes ( 1 )
823+ expect ( unsubscribes [ 1 ] ) . not . toBeCalled ( )
824+ } )
825+
703826 it ( 'with getServerSnapshot' , async ( ) => {
704827 let count = 0
705828 const unsubscribe = vi . fn ( )
706829 const subscribe = vi . fn ( ( ) => unsubscribe )
707- const getSnapshot = vi . fn ( ( ) => count ++ )
830+ const getSnapshot = vi . fn ( ( ) => count )
708831 const getServerSnapshot = vi . fn ( ( ) => 100 )
709832 const SubApp = ( ) => {
710833 const count = useSyncExternalStore ( subscribe , getSnapshot , getServerSnapshot )
@@ -727,6 +850,7 @@ describe('Hooks', () => {
727850 await new Promise ( ( r ) => setTimeout ( r ) )
728851 expect ( root . innerHTML ) . toBe ( '<button>toggle</button>' )
729852 expect ( unsubscribe ) . toBeCalled ( )
853+ count = 1
730854 root . querySelector ( 'button' ) ?. click ( )
731855 await new Promise ( ( r ) => setTimeout ( r ) )
732856 expect ( root . innerHTML ) . toBe ( '<div>1</div><button>toggle</button>' )
0 commit comments