forked from pmndrs/zustand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
127 lines (113 loc) · 3.65 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { useEffect, useLayoutEffect, useReducer, useRef } from 'react'
import createImpl, {
Destroy,
EqualityChecker,
GetState,
SetState,
State,
StateCreator,
StateSelector,
Subscribe,
StoreApi,
} from './vanilla'
export * from './vanilla'
// For server-side rendering: https://github.com/react-spring/zustand/pull/34
const useIsoLayoutEffect =
typeof window === 'undefined' ? useEffect : useLayoutEffect
export interface UseStore<T extends State> {
(): T
<U>(selector: StateSelector<T, U>, equalityFn?: EqualityChecker<U>): U
setState: SetState<T>
getState: GetState<T>
subscribe: Subscribe<T>
destroy: Destroy
}
export default function create<TState extends State>(
createState: StateCreator<TState> | StoreApi<TState>
): UseStore<TState> {
const api: StoreApi<TState> =
typeof createState === 'function' ? createImpl(createState) : createState
const useStore: any = <StateSlice>(
selector: StateSelector<TState, StateSlice> = api.getState as any,
equalityFn: EqualityChecker<StateSlice> = Object.is
) => {
const [, forceUpdate] = useReducer((c) => c + 1, 0) as [never, () => void]
const state = api.getState()
const stateRef = useRef(state)
const selectorRef = useRef(selector)
const equalityFnRef = useRef(equalityFn)
const erroredRef = useRef(false)
const currentSliceRef = useRef<StateSlice>()
if (currentSliceRef.current === undefined) {
currentSliceRef.current = selector(state)
}
let newStateSlice: StateSlice | undefined
let hasNewStateSlice = false
// The selector or equalityFn need to be called during the render phase if
// they change. We also want legitimate errors to be visible so we re-run
// them if they errored in the subscriber.
if (
stateRef.current !== state ||
selectorRef.current !== selector ||
equalityFnRef.current !== equalityFn ||
erroredRef.current
) {
// Using local variables to avoid mutations in the render phase.
newStateSlice = selector(state)
hasNewStateSlice = !equalityFn(
currentSliceRef.current as StateSlice,
newStateSlice
)
}
// Syncing changes in useEffect.
useIsoLayoutEffect(() => {
if (hasNewStateSlice) {
currentSliceRef.current = newStateSlice as StateSlice
}
stateRef.current = state
selectorRef.current = selector
equalityFnRef.current = equalityFn
erroredRef.current = false
})
const stateBeforeSubscriptionRef = useRef(state)
useIsoLayoutEffect(() => {
const listener = () => {
try {
const nextState = api.getState()
const nextStateSlice = selectorRef.current(nextState)
if (
!equalityFnRef.current(
currentSliceRef.current as StateSlice,
nextStateSlice
)
) {
stateRef.current = nextState
currentSliceRef.current = nextStateSlice
forceUpdate()
}
} catch (error) {
erroredRef.current = true
forceUpdate()
}
}
const unsubscribe = api.subscribe(listener)
if (api.getState() !== stateBeforeSubscriptionRef.current) {
listener() // state has changed before subscription
}
return unsubscribe
}, [])
return hasNewStateSlice
? (newStateSlice as StateSlice)
: currentSliceRef.current
}
Object.assign(useStore, api)
// For backward compatibility (No TS types for this)
useStore[Symbol.iterator] = function* () {
console.warn(
'[useStore, api] = create() is deprecated and will be removed in v4'
)
yield useStore
yield api
}
return useStore
}