Skip to content

Commit

Permalink
put back subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Jan 21, 2020
1 parent 7f92f16 commit 58f69ad
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/hooks/useSelector.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useReducer, useContext } from 'react'
import { useReducer, useMemo, useContext } from 'react'
import { useReduxContext as useDefaultReduxContext } from './useReduxContext'
import Subscription from '../utils/Subscription'
import { useIsomorphicLayoutEffect } from '../utils/useIsomorphicLayoutEffect'
import { ReactReduxContext } from '../components/Context'

Expand All @@ -9,9 +10,9 @@ function useSelectorWithStoreAndSubscription(
selector,
equalityFn,
store,
subscription
contextSub
) {
const [selectedState, dispatch] = useReducer(
const [selectedState, checkForUpdates] = useReducer(
prevSelectedState => {
const nextState = store.getState()
const nextSelectedState = selector(nextState)
Expand All @@ -20,15 +21,20 @@ function useSelectorWithStoreAndSubscription(
}
return nextSelectedState
},
null,
() => selector(store.getState())
store.getState(),
selector
)

const subscription = useMemo(() => new Subscription(store, contextSub), [
store,
contextSub
])

useIsomorphicLayoutEffect(() => {
subscription.onStateChange = dispatch
subscription.onStateChange = checkForUpdates
subscription.trySubscribe()

dispatch()
checkForUpdates()

return () => subscription.tryUnsubscribe()
}, [subscription])
Expand Down

0 comments on commit 58f69ad

Please sign in to comment.