1 | import { useReducer, useRef, useMemo, useContext } from 'react'
|
2 | import { useReduxContext as useDefaultReduxContext } from './useReduxContext'
|
3 | import Subscription from '../utils/Subscription'
|
4 | import { useIsomorphicLayoutEffect } from '../utils/useIsomorphicLayoutEffect'
|
5 | import { ReactReduxContext } from '../components/Context'
|
6 |
|
7 | const refEquality = (a, b) => a === b
|
8 |
|
9 | function useSelectorWithStoreAndSubscription(
|
10 | selector,
|
11 | equalityFn,
|
12 | store,
|
13 | contextSub
|
14 | ) {
|
15 | const [, forceRender] = useReducer(s => s + 1, 0)
|
16 |
|
17 | const subscription = useMemo(() => new Subscription(store, contextSub), [
|
18 | store,
|
19 | contextSub
|
20 | ])
|
21 |
|
22 | const latestSubscriptionCallbackError = useRef()
|
23 | const latestSelector = useRef()
|
24 | const latestSelectedState = useRef()
|
25 |
|
26 | let selectedState
|
27 |
|
28 | try {
|
29 | if (
|
30 | selector !== latestSelector.current ||
|
31 | latestSubscriptionCallbackError.current
|
32 | ) {
|
33 | selectedState = selector(store.getState())
|
34 | } else {
|
35 | selectedState = latestSelectedState.current
|
36 | }
|
37 | } catch (err) {
|
38 | if (latestSubscriptionCallbackError.current) {
|
39 | err.message += `\nThe error may be correlated with this previous error:\n${latestSubscriptionCallbackError.current.stack}\n\n`
|
40 | }
|
41 |
|
42 | throw err
|
43 | }
|
44 |
|
45 | useIsomorphicLayoutEffect(() => {
|
46 | latestSelector.current = selector
|
47 | latestSelectedState.current = selectedState
|
48 | latestSubscriptionCallbackError.current = undefined
|
49 | })
|
50 |
|
51 | useIsomorphicLayoutEffect(() => {
|
52 | function checkForUpdates() {
|
53 | try {
|
54 | const newSelectedState = latestSelector.current(store.getState())
|
55 |
|
56 | if (equalityFn(newSelectedState, latestSelectedState.current)) {
|
57 | return
|
58 | }
|
59 |
|
60 | latestSelectedState.current = newSelectedState
|
61 | } catch (err) {
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | latestSubscriptionCallbackError.current = err
|
67 | }
|
68 |
|
69 | forceRender({})
|
70 | }
|
71 |
|
72 | subscription.onStateChange = checkForUpdates
|
73 | subscription.trySubscribe()
|
74 |
|
75 | checkForUpdates()
|
76 |
|
77 | return () => subscription.tryUnsubscribe()
|
78 | }, [store, subscription])
|
79 |
|
80 | return selectedState
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | export function createSelectorHook(context = ReactReduxContext) {
|
90 | const useReduxContext =
|
91 | context === ReactReduxContext
|
92 | ? useDefaultReduxContext
|
93 | : () => useContext(context)
|
94 | return function useSelector(selector, equalityFn = refEquality) {
|
95 | if (process.env.NODE_ENV !== 'production' && !selector) {
|
96 | throw new Error(`You must pass a selector to useSelectors`)
|
97 | }
|
98 | const { store, subscription: contextSub } = useReduxContext()
|
99 |
|
100 | return useSelectorWithStoreAndSubscription(
|
101 | selector,
|
102 | equalityFn,
|
103 | store,
|
104 | contextSub
|
105 | )
|
106 | }
|
107 | }
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | export const useSelector = createSelectorHook()
|