Skip to content

Commit

Permalink
fix(state): removed caching, try out another solution for raf
Browse files Browse the repository at this point in the history
  • Loading branch information
BowlingX committed Dec 5, 2019
1 parent 879d10e commit d9de0c0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 34 deletions.
35 changes: 15 additions & 20 deletions src/lib/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,22 +201,20 @@ export const converter = (historyInstance: History) => (set, get) => {
// We skip one frame here in case we do route changes
// Because we rely on the state listener in store, it does not fire if it's not initialized
// TODO: Maybe find a better solution, but everything I tried yielded to double rendering of the component
requestAnimationFrame(() => {
Object.keys(namespaces).forEach(ns => {
set(
state => {
state.query = applyFlatConfigToState(
state.mappedConfig,
nextQueries,
ns,
state.values,
state.initialValues
)
},
HistoryEventType.REGISTER,
ns
)
})
Object.keys(namespaces).forEach(ns => {
set(
state => {
state.query = applyFlatConfigToState(
state.mappedConfig,
nextQueries,
ns,
state.values,
state.initialValues
)
},
HistoryEventType.REGISTER,
ns
)
})
})

Expand Down Expand Up @@ -277,7 +275,7 @@ export const converter = (historyInstance: History) => (set, get) => {
set(
state => {
state.subscribers = 1
state.unsubscribe = (cb?: () => void) => {
state.unsubscribe = () => {
set(thisState => {
// it's possible that the state namespace has been cleared by the provider
if (!thisState[ns]) {
Expand All @@ -287,9 +285,6 @@ export const converter = (historyInstance: History) => (set, get) => {
if (thisState[ns].subscribers === 0) {
// tslint:disable-next-line:no-delete
delete thisState[ns]
if (cb) {
cb()
}
}
}, HistoryEventType.REGISTER)
}
Expand Down
28 changes: 14 additions & 14 deletions src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ export const factoryParameters = <T = object>(
ns: string = DEFAULT_NAMESPACE
) => {
const flatConfig = flattenConfig(config)
// tslint:disable-next-line:no-let
let initialCache: { readonly query: object; readonly values: T } | null
const useQuery = () => {
const [useStore, api] = useContext(StoreContext) as [
UseStore<StoreState<T>>,
Expand Down Expand Up @@ -93,26 +91,31 @@ export const factoryParameters = <T = object>(
)

const initialRegisterState = useMemo(() => {
// in case this namespace has been initialized already, reuse the result.
if (initialCache) {
return initialCache
const { values, query, initialValues } =
api.getState().namespaces[ns] || {}
if (values) {
return {
initialValues,
query,
values
}
}
// thisValues will be mutated by applyFlatConfigToState, that's why we init it with a copy of
// the initial state.
const thisValues = { ...defaultInitialValues }
const query = applyFlatConfigToState(
const thisQuery = applyFlatConfigToState(
flatConfig,
initialQueries,
ns,
thisValues,
defaultInitialValues
)
initialCache = {
query,
return {
initialValues: defaultInitialValues,
query: thisQuery,
values: thisValues
}
return initialCache
}, [])
}, [api])

const [currentState, setCurrentState] = useState({
initialValues: defaultInitialValues,
Expand Down Expand Up @@ -147,10 +150,7 @@ export const factoryParameters = <T = object>(

return () => {
unsubscribe()
unregister(() => {
// When all subscribers are removed, we also clear the cache
initialCache = null
})
unregister()
}
}, [])

Expand Down

0 comments on commit d9de0c0

Please sign in to comment.