Skip to content

Commit

Permalink
feat(memoization): added propper memoization for init now
Browse files Browse the repository at this point in the history
  • Loading branch information
BowlingX committed Dec 5, 2019
1 parent caf6a0a commit b95cd4d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"node": ">=8.9"
},
"dependencies": {
"query-string": "^6.9.0"
"query-string": "^6.9.0",
"memoize-one": "^5.1.1"
},
"peerDependencies": {
"history": "^4",
Expand Down
38 changes: 23 additions & 15 deletions src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { History } from 'history'
import LocationState = History.LocationState

import memoizeOne from 'memoize-one'
import { stringify } from 'query-string'
import { createContext, useContext, useEffect, useMemo, useState } from 'react'
import { create, StoreApi, UseStore } from 'zustand'
Expand Down Expand Up @@ -58,6 +59,27 @@ export const factoryParameters = <T = object>(
ns: string = DEFAULT_NAMESPACE
) => {
const flatConfig = flattenConfig(config)

const initBlank = (initialQueries: object) => {
// thisValues will be mutated by applyFlatConfigToState, that's why we init it with a copy of
// the initial state.
const thisValues = { ...defaultInitialValues }
const thisQuery = applyFlatConfigToState(
flatConfig,
initialQueries,
ns,
thisValues,
defaultInitialValues
)
return {
initialValues: defaultInitialValues,
query: thisQuery,
values: thisValues
}
}

const memInitBlank = memoizeOne(initBlank)

const useQuery = () => {
const [useStore, api] = useContext(StoreContext) as [
UseStore<StoreState<T>>,
Expand Down Expand Up @@ -100,21 +122,7 @@ export const factoryParameters = <T = object>(
values
}
}
// thisValues will be mutated by applyFlatConfigToState, that's why we init it with a copy of
// the initial state.
const thisValues = { ...defaultInitialValues }
const thisQuery = applyFlatConfigToState(
flatConfig,
initialQueries,
ns,
thisValues,
defaultInitialValues
)
return {
initialValues: defaultInitialValues,
query: thisQuery,
values: thisValues
}
return memInitBlank(initialQueries)
}, [api])

const [currentState, setCurrentState] = useState({
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9024,6 +9024,11 @@ mem@^4.0.0:
mimic-fn "^2.0.0"
p-is-promise "^2.0.0"

memoize-one@^5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/memoize-one/-/memoize-one-5.1.1.tgz#047b6e3199b508eaec03504de71229b8eb1d75c0"
integrity sha512-HKeeBpWvqiVJD57ZUAsJNm71eHTykffzcLZVYWiVfQeI1rJtuEaS7hQiEpWfVVk18donPwJEcFKIkCmPJNOhHA==

memoizerific@^1.11.3:
version "1.11.3"
resolved "https://registry.yarnpkg.com/memoizerific/-/memoizerific-1.11.3.tgz#7c87a4646444c32d75438570905f2dbd1b1a805a"
Expand Down

0 comments on commit b95cd4d

Please sign in to comment.