Skip to content

Commit

Permalink
feat(reset): added resetPush and resetReplace to reset the curren…
Browse files Browse the repository at this point in the history
…t value to the current initialState
  • Loading branch information
BowlingX committed Dec 4, 2019
1 parent 04e0899 commit 393b10d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ build/docs
coverage
.nyc_output
*.log
.github
22 changes: 20 additions & 2 deletions src/lib/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,16 @@ export const historyManagement = (historyInstance: History) => apply => (

const namespaceProducer = (fn, ns?: string) => state => {
if (!ns) {
return fn(state.namespaces)
const result = fn(state.namespaces)
// if no namespaces is given, we support return values
if (result) {
state.namespaces = result
}
return
}
if (state.namespaces[ns]) {
return fn(state.namespaces[ns])
fn(state.namespaces[ns])
return
}
const next = {}
fn(next)
Expand Down Expand Up @@ -272,6 +278,18 @@ export const converter = (historyInstance: History) => (set, get) => {
},
replaceState: (ns: string, fn) =>
set(state => fn(state.values), HistoryEventType.REPLACE, ns),
resetPush: (ns: string) =>
set(
state => void (state.values = state.initialValues),
HistoryEventType.PUSH,
ns
),
resetReplace: (ns: string) =>
set(
state => void (state.values = state.initialValues),
HistoryEventType.REPLACE,
ns
),
unregister: () => {
set(() => {
// return a new object for namespaces
Expand Down
18 changes: 14 additions & 4 deletions src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,22 @@ export const factoryParameters = <T = object>(

const callback = useCallback(
// tslint:disable-next-line:no-shadowed-variable
({ register, pushState, replaceState }) => ({
({ register, pushState, replaceState, resetPush, resetReplace }) => ({
pushState,
register,
replaceState
replaceState,
resetPush,
resetReplace
}),
[useStore]
)
const { register, pushState, replaceState } = useStore(callback)
const {
register,
pushState,
replaceState,
resetPush,
resetReplace
} = useStore(callback)

useMemo(() => {
register(config, flatConfig, ns, initialValues)
Expand Down Expand Up @@ -124,9 +132,11 @@ export const factoryParameters = <T = object>(
initialValues: innerValues.initialValues,
pushState: (state: (state: T) => void) => pushState(ns, state),
replaceState: (state: (state: T) => void) => replaceState(ns, state),
resetPush: () => resetPush(ns),
resetReplace: () => resetReplace(ns),
values: innerValues.values
}),
[innerValues, pushState, replaceState]
[innerValues, pushState, replaceState, resetPush, resetReplace]
)
}

Expand Down

0 comments on commit 393b10d

Please sign in to comment.