@@ -21,8 +21,13 @@ export default function useStateTogetherWithPerUserValues<
2121 T extends NotUndefined
2222> (
2323 rtKey : string ,
24- initial_value : T
24+ initialValue : T
2525) : [ T , Dispatch < SetStateAction < T > > , { [ id : string ] : T } ] {
26+ // Memorize the first initial value, ignore changes to the
27+ // initialValue prop
28+ // https://react.dev/reference/react/useState
29+ const [ actualInitialValue ] = useState ( initialValue )
30+
2631 const context = useContext ( CroquetContext )
2732 let model : null | ReactTogetherModel = null
2833 let session : null | Session = null
@@ -35,11 +40,13 @@ export default function useStateTogetherWithPerUserValues<
3540 }
3641 const viewId = view ?. viewId || ''
3742 const [ allValuesState , setAllValuesState ] = useState < {
38- value : Map < string , T >
43+ value : { [ id : string ] : T }
3944 hash : string
4045 } > ( ( ) => {
41- const value = ( model ?. stateTogether . get ( rtKey ) ||
42- new Map ( [ [ viewId , initial_value ] ] ) ) as Map < string , T >
46+ const value = mapToObject (
47+ ( model ?. stateTogether . get ( rtKey ) ||
48+ new Map ( [ [ viewId , actualInitialValue ] ] ) ) as Map < string , T >
49+ )
4350 const hash = hash_fn ( value )
4451 return { value, hash }
4552 } )
@@ -50,12 +57,16 @@ export default function useStateTogetherWithPerUserValues<
5057 if ( ! session || ! view || ! model || ! viewId ) return
5158
5259 const handler = ( ) => {
53- const newValues = model . stateTogether . get ( rtKey ) as Map < string , T >
60+ const newValues = mapToObject (
61+ model . stateTogether . get ( rtKey ) as Map < string , T >
62+ )
5463 const newHash = hash_fn ( newValues )
5564
56- setAllValuesState ( ( prev ) =>
57- prev . hash === newHash ? prev : { value : newValues , hash : newHash }
58- )
65+ setAllValuesState ( ( prev ) => {
66+ return prev . hash === newHash
67+ ? prev
68+ : { value : newValues , hash : newHash }
69+ } )
5970 }
6071
6172 view . subscribe (
@@ -74,7 +85,7 @@ export default function useStateTogetherWithPerUserValues<
7485 view . publish ( model . id , 'setStateTogether' , {
7586 id : rtKey ,
7687 viewId,
77- newValue : initial_value
88+ newValue : actualInitialValue
7889 } )
7990
8091 return ( ) => {
@@ -87,9 +98,9 @@ export default function useStateTogetherWithPerUserValues<
8798
8899 view . unsubscribe ( rtKey , 'updated' , handler )
89100 }
90- } , [ session , view , viewId , model , rtKey , initial_value ] )
101+ } , [ session , view , viewId , model , rtKey , actualInitialValue ] )
91102
92- const localValue = allValues . get ( viewId ) || initial_value
103+ const localValue = allValues [ viewId ] || actualInitialValue
93104
94105 const setter = useCallback (
95106 ( newValueOrFn : SetStateAction < T > ) : void => {
@@ -112,12 +123,12 @@ export default function useStateTogetherWithPerUserValues<
112123 // the same state interface
113124 setAllValuesState ( ( prev ) => {
114125 const { value, hash } = prev
115- const newValue = getNewValue ( value . get ( '' ) ! , newValueOrFn )
126+ const newValue = getNewValue ( value [ viewId ] ! , newValueOrFn )
116127 const newHash = hash_fn ( newValue )
117128 if ( hash === newHash ) {
118129 return prev
119130 } else {
120- value . set ( '' , newValue )
131+ value [ viewId ] = newValue
121132 return { value, hash : newHash }
122133 }
123134 } )
@@ -126,5 +137,5 @@ export default function useStateTogetherWithPerUserValues<
126137 [ view , viewId , model , rtKey ]
127138 )
128139
129- return [ localValue , setter , mapToObject ( allValues ) ]
140+ return [ localValue , setter , allValues ]
130141}
0 commit comments