Skip to content

Commit

Permalink
fix(build, memoization): fixed ts docs build, added memoization-one f…
Browse files Browse the repository at this point in the history
…or initial state
  • Loading branch information
BowlingX committed Dec 5, 2019
1 parent d9de0c0 commit 88933f0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 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
12 changes: 9 additions & 3 deletions src/lib/store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* tslint:disable:no-expression-statement readonly-array no-shadowed-variable */
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'
Expand Down Expand Up @@ -58,6 +59,11 @@ export const factoryParameters = <T = object>(
ns: string = DEFAULT_NAMESPACE
) => {
const flatConfig = flattenConfig(config)
const memoizedApplyFlatConfigToState = memoizeOne(
applyFlatConfigToState,
([, nextInitialQueries, nextNs], [, previousInitialQueries, previousNs]) =>
nextNs === previousNs && nextInitialQueries === previousInitialQueries
)
const useQuery = () => {
const [useStore, api] = useContext(StoreContext) as [
UseStore<StoreState<T>>,
Expand Down Expand Up @@ -91,8 +97,8 @@ export const factoryParameters = <T = object>(
)

const initialRegisterState = useMemo(() => {
const { values, query, initialValues } =
api.getState().namespaces[ns] || {}
const namespaceData = api.getState().namespaces[ns] || {}
const { values, query, initialValues } = namespaceData
if (values) {
return {
initialValues,
Expand All @@ -103,7 +109,7 @@ export const factoryParameters = <T = 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(
const thisQuery = memoizedApplyFlatConfigToState(
flatConfig,
initialQueries,
ns,
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 88933f0

Please sign in to comment.