|
1 | 1 | import { EffectModule, ActionOfEffectModule } from '@sigi/core'
|
2 | 2 | import { ConstructorOf, IStore } from '@sigi/types'
|
3 | 3 | import { useEffect, useState, useRef, useMemo, useCallback } from 'react'
|
4 |
| -import { distinctUntilChanged, map, skip } from 'rxjs/operators' |
| 4 | +import { identity } from 'rxjs' |
| 5 | +import { skip, tap } from 'rxjs/operators' |
5 | 6 |
|
6 | 7 | import { useInstance } from './injectable-context'
|
7 | 8 | import { shallowEqual } from './shallow-equal'
|
@@ -32,38 +33,41 @@ function _useModuleState<S, U = S>(
|
32 | 33 | }
|
33 | 34 | // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
34 | 35 | dependencies = dependencies || []
|
35 |
| - const isFirstRendering = useRef(true) |
36 |
| - const [appState, setState] = useState(() => { |
37 |
| - const initialState = store.state |
38 |
| - return selector ? selector(initialState) : initialState |
39 |
| - }) |
40 |
| - |
41 |
| - const subscribe = useCallback(() => { |
42 |
| - return store.state$ |
43 |
| - .pipe( |
44 |
| - map((s) => (selector ? selector(s) : s)), |
45 |
| - distinctUntilChanged((s1, s2) => equalFn(s1, s2)), |
46 |
| - // skip initial state emission |
47 |
| - skip(isFirstRendering.current ? 1 : 0), |
48 |
| - ) |
49 |
| - .subscribe(setState) |
50 |
| - // eslint-disable-next-line react-hooks/exhaustive-deps |
51 |
| - }, [store, ...dependencies]) |
| 36 | + const stateRef = useRef<S | U>() |
| 37 | + const depsRef = useRef<any[]>() |
| 38 | + const selectorRef = useRef<(s: S) => S | U>() |
| 39 | + |
| 40 | + if (!equalFn(depsRef.current, dependencies)) { |
| 41 | + selectorRef.current = selector ?? identity |
| 42 | + stateRef.current = selectorRef.current(store.state) |
| 43 | + } |
| 44 | + depsRef.current = dependencies |
52 | 45 |
|
53 |
| - const subscription = useMemo(() => { |
54 |
| - return subscribe() |
| 46 | + const [_, _flipSig] = useState(false) |
| 47 | + |
| 48 | + const tryUpdateState = useCallback((state: S) => { |
| 49 | + const newState = selectorRef.current!(state) |
| 50 | + if (!equalFn(stateRef.current, newState)) { |
| 51 | + stateRef.current = newState |
| 52 | + _flipSig((v) => !v) |
| 53 | + } |
55 | 54 | // eslint-disable-next-line react-hooks/exhaustive-deps
|
56 |
| - }, [subscribe]) |
| 55 | + }, []) |
| 56 | + |
| 57 | + const subscribe = useCallback( |
| 58 | + () => store.state$.pipe(skip(1), tap(tryUpdateState)).subscribe(), |
| 59 | + [store, tryUpdateState], |
| 60 | + ) |
| 61 | + const subscription = useMemo(() => subscribe(), [subscribe]) |
57 | 62 |
|
58 | 63 | useEffect(() => {
|
59 |
| - isFirstRendering.current = false |
60 | 64 | const maybeReSubscribeInConcurrencyMode = subscription.closed ? subscribe() : subscription
|
61 | 65 | return () => {
|
62 | 66 | maybeReSubscribeInConcurrencyMode.unsubscribe()
|
63 | 67 | }
|
64 | 68 | }, [subscription, subscribe])
|
65 | 69 |
|
66 |
| - return appState |
| 70 | + return stateRef.current! |
67 | 71 | }
|
68 | 72 |
|
69 | 73 | export function useModuleState<M extends EffectModule<any>>(
|
|
0 commit comments