1- import type { ImmerHook } from './useImmer' ;
1+ import type { ImmerHook , Updater } from './useImmer' ;
22
33import { freeze , produce } from 'immer' ;
44import { useState } from 'react' ;
55
66import { useUnmount } from './useUnmount' ;
77
8- export function createGlobalState < S > ( ) : ( ) => ImmerHook < S | undefined > ;
9- export function createGlobalState < S > ( initialValue : S ) : ( ) => ImmerHook < S > ;
10- export function createGlobalState < S > ( initialValue ?: S ) : ( ) => ImmerHook < S | undefined > {
8+ export function createGlobalState < S > ( ) : { ( ) : ImmerHook < S | undefined > ; set : Updater < S | undefined > } ;
9+ export function createGlobalState < S > ( initialValue : S ) : { ( ) : ImmerHook < S > ; set : Updater < S > } ;
10+ export function createGlobalState < S > ( initialValue ?: S ) : { ( ) : ImmerHook < S | undefined > ; set : Updater < S | undefined > } {
1111 const store = {
1212 state : freeze ( typeof initialValue === 'function' ? initialValue ( ) : initialValue , true ) ,
1313 setState ( updater : any ) {
@@ -24,17 +24,22 @@ export function createGlobalState<S>(initialValue?: S): () => ImmerHook<S | unde
2424 updates : new Set < ( ...args : any [ ] ) => any > ( ) ,
2525 } ;
2626
27- return ( ) => {
28- const [ state , setState ] = useState ( store . state ) ;
27+ return Object . assign < any , any > (
28+ ( ) => {
29+ const [ state , setState ] = useState ( store . state ) ;
2930
30- if ( ! store . updates . has ( setState ) ) {
31- store . updates . add ( setState ) ;
32- }
31+ if ( ! store . updates . has ( setState ) ) {
32+ store . updates . add ( setState ) ;
33+ }
3334
34- useUnmount ( ( ) => {
35- store . updates . delete ( setState ) ;
36- } ) ;
35+ useUnmount ( ( ) => {
36+ store . updates . delete ( setState ) ;
37+ } ) ;
3738
38- return [ state , store . setState ] ;
39- } ;
39+ return [ state , store . setState ] ;
40+ } ,
41+ {
42+ set : store . setState ,
43+ }
44+ ) ;
4045}
0 commit comments