@@ -10,12 +10,8 @@ import GlobalStateManager from './global-state-manager';
1010import setGlobal from './set-global' ;
1111import REACT_HOOKS_ERROR from './utils/react-hooks-error' ;
1212
13-
14-
1513type VoidFunction = ( ) => void ;
1614
17-
18-
1915// useGlobal()
2016export default function _useGlobal < G extends { } = State > (
2117 overrideGlobalStateManager : GlobalStateManager < G > | null ,
@@ -24,7 +20,7 @@ export default function _useGlobal<G extends {} = State>(
2420// useGlobal('property')
2521export default function _useGlobal <
2622 G extends { } = State ,
27- Property extends keyof G = keyof G ,
23+ Property extends keyof G = keyof G
2824> (
2925 overrideGlobalStateManager : GlobalStateManager < G > | null ,
3026 property : Property ,
@@ -33,12 +29,11 @@ export default function _useGlobal<
3329// Implementation
3430export default function _useGlobal <
3531 G extends { } = State ,
36- Property extends keyof G = keyof G ,
32+ Property extends keyof G = keyof G
3733> (
3834 overrideGlobalStateManager : GlobalStateManager < G > | null ,
3935 property ?: Property ,
4036) : UseGlobal < G , Property > {
41-
4237 // Require hooks.
4338 if ( ! useContext ) {
4439 throw REACT_HOOKS_ERROR ;
@@ -56,47 +51,39 @@ export default function _useGlobal<
5651
5752 // Return the entire global state.
5853 if ( typeof property === 'undefined' ) {
59-
60-
6154 // If this component ever updates or unmounts, remove the force update
6255 // listener.
63- useEffect ( ( ) : VoidFunction => removeForceUpdateListener ) ;
56+ useEffect ( ( ) : VoidFunction => removeForceUpdateListener , [ ] ) ;
6457
6558 const globalStateSetter = useCallback (
6659 (
6760 newGlobalState : NewGlobalState < G > ,
6861 callback : Callback < G > | null = null ,
69- ) : Promise < G > =>
70- setGlobal ( globalStateManager , newGlobalState , callback ) ,
62+ ) : Promise < G > => setGlobal ( globalStateManager , newGlobalState , callback ) ,
7163 [ ] ,
7264 ) ;
7365
74- return [
75- globalStateManager . spyState ( forceUpdate ) ,
76- globalStateSetter ,
77- ] ;
66+ return [ globalStateManager . spyState ( forceUpdate ) , globalStateSetter ] ;
7867 }
7968
80- useEffect ( ( ) : VoidFunction => {
81-
82- // We add the listener as an effect, so that there are not race conditions
83- // between subscribing and unsubscribing.
84- // Subscribing outside of useEffect via `spyState()[property]` will
85- // cause the re-render subscription to occur before the unmount
86- // unsubscription occurs. As a result, the unmount unsubscription
87- // removes the re-rendered subscription.
88- globalStateManager . addPropertyListener ( property , forceUpdate ) ;
89-
90- // If this component ever updates or unmounts, remove the force update
91- // listener.
92- return removeForceUpdateListener ;
93- } ) ;
69+ useEffect (
70+ ( ) : VoidFunction => {
71+ // We add the listener as an effect, so that there are not race conditions
72+ // between subscribing and unsubscribing.
73+ // Subscribing outside of useEffect via `spyState()[property]` will
74+ // cause the re-render subscription to occur before the unmount
75+ // unsubscription occurs. As a result, the unmount unsubscription
76+ // removes the re-rendered subscription.
77+ globalStateManager . addPropertyListener ( property , forceUpdate ) ;
78+
79+ // If this component ever updates or unmounts, remove the force update
80+ // listener.
81+ return removeForceUpdateListener ;
82+ } ,
83+ ) ;
9484
9585 const globalPropertySetter = useCallback (
96- (
97- value : G [ Property ] ,
98- callback : Callback < G > | null = null ,
99- ) : Promise < G > => {
86+ ( value : G [ Property ] , callback : Callback < G > | null = null ) : Promise < G > => {
10087 const newGlobalState : Partial < G > = Object . create ( null ) ;
10188 newGlobalState [ property ] = value ;
10289 return setGlobal ( globalStateManager , newGlobalState , callback ) ;
@@ -105,8 +92,5 @@ export default function _useGlobal<
10592 ) ;
10693
10794 // Return both getter and setter.
108- return [
109- globalStateManager . state [ property ] ,
110- globalPropertySetter ,
111- ] ;
112- } ;
95+ return [ globalStateManager . state [ property ] , globalPropertySetter ] ;
96+ }
0 commit comments