diff --git a/src/lib/middleware.ts b/src/lib/middleware.ts index 6e8c576..6a23ceb 100644 --- a/src/lib/middleware.ts +++ b/src/lib/middleware.ts @@ -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 + ) }) }) @@ -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]) { @@ -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) } diff --git a/src/lib/store.ts b/src/lib/store.ts index d020609..ff4ea73 100644 --- a/src/lib/store.ts +++ b/src/lib/store.ts @@ -58,8 +58,6 @@ export const factoryParameters = ( 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>, @@ -93,26 +91,31 @@ export const factoryParameters = ( ) 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, @@ -147,10 +150,7 @@ export const factoryParameters = ( return () => { unsubscribe() - unregister(() => { - // When all subscribers are removed, we also clear the cache - initialCache = null - }) + unregister() } }, [])