11import type { ImmerHook } from './useImmer' ;
22
3- import { freeze , produce } from 'immer' ;
3+ import { freeze } from 'immer' ;
44
5- import { useForceUpdate } from './useForceUpdate ' ;
5+ import { useImmer } from './useImmer ' ;
66import { useUnmount } from './useUnmount' ;
77
88export function createGlobalState < S > ( ) : ( ) => ImmerHook < S | undefined > ;
@@ -11,31 +11,25 @@ export function createGlobalState<S>(initialValue?: S): () => ImmerHook<S | unde
1111 const store = {
1212 state : freeze ( typeof initialValue === 'function' ? initialValue ( ) : initialValue , true ) ,
1313 setState ( updater : any ) {
14- const prev = store . state ;
15- if ( typeof updater === 'function' ) {
16- store . state = produce ( store . state , updater ) ;
17- } else {
18- store . state = freeze ( updater ) ;
19- }
20- if ( ! Object . is ( store . state , prev ) ) {
21- for ( const update of store . updates ) {
22- update ( ) ;
23- }
14+ for ( const update of store . updates ) {
15+ update ( updater ) ;
2416 }
2517 } ,
26- updates : new Set < ( ) => void > ( ) ,
18+ updates : new Set < ( ... args : any [ ] ) => any > ( ) ,
2719 } ;
2820
2921 return ( ) => {
30- const forceUpdate = useForceUpdate ( ) ;
31- if ( ! store . updates . has ( forceUpdate ) ) {
32- store . updates . add ( forceUpdate ) ;
22+ const [ state , setState ] = useImmer ( store . state ) ;
23+ store . state = state ;
24+
25+ if ( ! store . updates . has ( setState ) ) {
26+ store . updates . add ( setState ) ;
3327 }
3428
3529 useUnmount ( ( ) => {
36- store . updates . delete ( forceUpdate ) ;
30+ store . updates . delete ( setState ) ;
3731 } ) ;
3832
39- return [ store . state , store . setState ] ;
33+ return [ state , store . setState ] ;
4034 } ;
4135}
0 commit comments