-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(extensions): added branch function to reset
- Loading branch information
1 parent
9d8e71b
commit 3501915
Showing
5 changed files
with
100 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const SENDER = 'extension:reset'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,38 @@ | ||
import snapshotExtension from '@harlem/extension-snapshot'; | ||
import { | ||
SENDER, | ||
} from './constants'; | ||
|
||
import { | ||
clone, | ||
overwrite, | ||
} from '@harlem/utilities'; | ||
|
||
import type { | ||
InternalStore, | ||
BaseState, | ||
} from '@harlem/core'; | ||
|
||
export default function resetExtension<TState extends BaseState>() { | ||
const createSnapshotExtension = snapshotExtension<TState>({ | ||
mutationName: '$reset', | ||
}); | ||
import type { | ||
BranchCallback, | ||
} from './types'; | ||
|
||
export * from './types'; | ||
|
||
export default function resetExtension<TState extends BaseState>() { | ||
return (store: InternalStore<TState>) => { | ||
const { | ||
snapshot, | ||
} = createSnapshotExtension(store); | ||
const snapshot = clone(store.state) as TState; | ||
|
||
function reset<TBranchState extends BaseState>(branchCallback: BranchCallback<TState, TBranchState> = state => state as TBranchState) { | ||
store.write('$reset', SENDER, state => { | ||
const source = branchCallback(snapshot); | ||
const target = branchCallback(state); | ||
|
||
const snap = snapshot(); | ||
overwrite(target, source, /^\$/); | ||
}); | ||
} | ||
|
||
return { | ||
reset: () => snap.apply(), | ||
reset, | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { | ||
BaseState, | ||
ReadState, | ||
WriteState, | ||
} from '@harlem/core'; | ||
|
||
export type BranchCallback<TState extends BaseState, TData extends BaseState> = (state: ReadState<TState> | WriteState<TState>) => TData; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters