Skip to content

Commit

Permalink
feat: removed (unnecessary) memoization
Browse files Browse the repository at this point in the history
  • Loading branch information
BowlingX committed Aug 23, 2023
1 parent 9c58f0b commit c7f76b0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 28 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@
"engines": {
"node": ">=14"
},
"dependencies": {
"memoize-one": "^6.0.0"
},
"dependencies": {},
"peerDependencies": {
"history": ">=4",
"immer": ">=9",
Expand Down Expand Up @@ -185,11 +183,13 @@
"dot-prop": "5.2.0"
},
"jest": {
"extensionsToTreatAsEsm": [".ts", ".tsx"],
"transform": {
".(ts|tsx)": [
"ts-jest",
{
"isolatedModules": true
"isolatedModules": true,
"useESM": true
}
]
},
Expand Down
12 changes: 3 additions & 9 deletions src/lib/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* tslint:disable:no-expression-statement readonly-keyword no-mixed-interface no-object-mutation readonly-array */
import { Patch, produceWithPatches } from 'immer'
import memoizeOneImport from 'memoize-one'
import { StoreApi } from 'zustand'
// tslint:disable-next-line:no-submodule-imports
import { shallow } from 'zustand/shallow'
Expand All @@ -15,8 +14,6 @@ import {
applyFlatConfigToState,
} from './utils.js'

const memoizeOne = memoizeOneImport.default || memoizeOneImport

export enum HistoryEventType {
PUSH,
REPLACE,
Expand Down Expand Up @@ -314,10 +311,8 @@ export const converter =
get: StoreApi<StoreState<T>>['getState'],
api: StoreApi<StoreState<T>>
): StoreState<T> => {
const memoizedGetInitialQueries = memoizeOne(parseSearchString)

const updateFromQuery = (search: string | URLSearchParams) => {
const nextQueries = memoizedGetInitialQueries(search)
const nextQueries = parseSearchString(search)
const namespaces = get().namespaces
Object.keys(namespaces).forEach((ns) => {
// It's possible that the ns got cleared while we are applying the new state.
Expand Down Expand Up @@ -395,8 +390,7 @@ export const converter =
)
},
/** the initial queries when the script got executed first (usually on page load). */
initialQueries: () =>
memoizedGetInitialQueries(historyInstance.initialSearch()),
initialQueries: () => parseSearchString(historyInstance.initialSearch()),
/** 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 Expand Up @@ -426,7 +420,7 @@ export const converter =
state.initialValues = initialValues
state.query = applyFlatConfigToState(
state.mappedConfig,
memoizedGetInitialQueries(historyInstance.initialSearch()),
parseSearchString(historyInstance.initialSearch()),
ns,
state.values,
initialValues
Expand Down
14 changes: 4 additions & 10 deletions src/lib/store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* tslint:disable:no-expression-statement readonly-array no-shadowed-variable */
import { Draft, enablePatches, produce } from 'immer'
import memoizeOneImport from 'memoize-one'
import {
createContext,
useCallback,
Expand All @@ -15,8 +14,6 @@ import { devtools, subscribeWithSelector } from 'zustand/middleware'
// tslint:disable-next-line:no-submodule-imports
import { createWithEqualityFn } from 'zustand/traditional'

const memoizeOne = memoizeOneImport.default || memoizeOneImport

// tslint:disable-next-line:no-submodule-imports
import { shallow } from 'zustand/shallow'
import {
Expand Down Expand Up @@ -150,9 +147,6 @@ export const factoryParameters = <T extends object>(
}
}

const memInitBlank = memoizeOne(initBlank)
const memCreateInitialValues = memoizeOne(createInitialValues)

const useQuery = () => {
const useStore = assertContextExists(
useContext(StoreContext) as UseBoundStore<
Expand Down Expand Up @@ -189,8 +183,8 @@ export const factoryParameters = <T extends object>(
shallow
)
const initialRegisterState = useMemo(() => {
const initialValues = memCreateInitialValues(defaultInitialValues)
return memInitBlank(initialQueries(), initialValues)
const initialValues = createInitialValues(defaultInitialValues)
return initBlank(initialQueries(), initialValues)
}, [useStore, defaultInitialValues])

const [currentState, setCurrentState] = useState({
Expand Down Expand Up @@ -284,7 +278,7 @@ export const factoryParameters = <T extends object>(
): string => {
const thisInitialValues =
typeof initialValues === 'undefined'
? memCreateInitialValues(defaultInitialValues)
? createInitialValues(defaultInitialValues)
: initialValues
return new URLSearchParams(
createQueryObject<T>(flatConfig, ns, values, thisInitialValues)
Expand All @@ -297,7 +291,7 @@ export const factoryParameters = <T extends object>(
): Partial<T> => {
const thisInitialValues =
typeof initialValues === 'undefined'
? memCreateInitialValues(defaultInitialValues)
? createInitialValues(defaultInitialValues)
: initialValues
return produce({}, (draft: Draft<T>) => {
const parsedQuery = new URLSearchParams(query)
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10921,11 +10921,6 @@ memfs@^3.1.2, memfs@^3.2.2:
dependencies:
fs-monkey "^1.0.3"

memoize-one@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/memoize-one/-/memoize-one-6.0.0.tgz#b2591b871ed82948aee4727dc6abceeeac8c1045"
integrity sha512-rkpe71W0N0c0Xz6QD0eJETuWAJGnJ9afsl1srmwPrI+yBCkge5EycXXbYRyvL29zZVUWQCY7InPRCv3GDXuZNw==

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 c7f76b0

Please sign in to comment.