Skip to content

Commit

Permalink
fix(routing): Change initial queries to be a function to prevent a st…
Browse files Browse the repository at this point in the history
…ale state when registering on navigating
  • Loading branch information
BowlingX committed Dec 24, 2019
1 parent 12b339f commit b402891
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 10 additions & 7 deletions src/lib/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/* tslint:disable:no-expression-statement readonly-keyword no-mixed-interface no-object-mutation readonly-array */
import { History } from 'history'
import { Patch, produceWithPatches } from 'immer'
import memoizeOne from 'memoize-one'
import { parse, stringify } from 'query-string'
import { GetState, State, StoreApi } from 'zustand'
import { Config, MappedConfig } from './store'
import {
applyDiffWithCreateQueriesFromPatch,
applyFlatConfigToState,
createOrApplyPath,
get as getByPath
applyFlatConfigToState
} from './utils'

export enum HistoryEventType {
Expand Down Expand Up @@ -69,7 +68,7 @@ export interface StoreState<ValueState = object> {
unregister: () => void
resetPush: (ns: string) => void
resetReplace: (ns: string) => void
initialQueries: object
initialQueries: () => object
}

type NamespaceProducerFunction<T> = (state: NamespaceValues<T>) => void
Expand Down Expand Up @@ -266,14 +265,17 @@ export const immerWithPatches = <T>(config: ImmerStateCreator<T>) => (
api
)

const parseSearchString = (search: string) => parse(search)

export const converter = <T extends GenericObject>(
historyInstance: History
) => (
set: NamespaceProducer<T> & GenericConverter<T>,
get: GetState<StoreState<T>>,
api: StoreApi<StoreState<T>>
): StoreState<T> => {
const initialQueries = parse(historyInstance.location.search)
const memoizedGetInitialQueries = memoizeOne(parseSearchString)

const unregisterListener = historyInstance.listen((location, action) => {
// don't handle our own actions
if (
Expand All @@ -283,7 +285,7 @@ export const converter = <T extends GenericObject>(
) {
return
}
const nextQueries = parse(location.search)
const nextQueries = memoizedGetInitialQueries(location.search)
const namespaces = get().namespaces
Object.keys(namespaces).forEach(ns => {
set(
Expand Down Expand Up @@ -333,7 +335,8 @@ export const converter = <T extends GenericObject>(
)
},
/** the initial queries when the script got executed first (usually on page load). */
initialQueries,
initialQueries: () =>
memoizedGetInitialQueries(historyInstance.location.search),
/** here we store all data and configurations for the different namespaces */
namespaces: {},
/** pushes a new state for a given namespace, (will use history.pushState) */
Expand Down
2 changes: 1 addition & 1 deletion src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export const factoryParameters = <T = {}>(
values
}
}
return memInitBlank(initialQueries)
return memInitBlank(initialQueries())
}, [api])

const [currentState, setCurrentState] = useState({
Expand Down

0 comments on commit b402891

Please sign in to comment.