-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Sync Recoil changes back to Redux (#7)
* Initial support for writing state * Bidirectional read-write * Update docs for write support
- Loading branch information
Showing
15 changed files
with
142 additions
and
38 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
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
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
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
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,4 @@ | ||
export { default as internalStateAtom } from './internalStateAtom'; | ||
export * from './internalStateAtom'; | ||
|
||
export * from './types'; |
File renamed without changes.
File renamed without changes.
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,4 +1,5 @@ | ||
// These renames are for readability, and to avoid having to repeatedly disable no-explicit-any when used. | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type DefaultReturnType = any; | ||
export type ReduxState = unknown; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type ReduxState = any; |
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,47 @@ | ||
import { set as setPath } from 'immutable-path'; | ||
import { Reducer } from 'redux'; | ||
|
||
import { ReduxState } from './internals/types'; | ||
|
||
const SYNC_CHANGES_FROM_RECOIL = 'SYNC_CHANGES_FROM_RECOIL'; | ||
|
||
export type ChangeEntry = [string, ReduxState]; | ||
|
||
export interface SyncFromRecoilAction { | ||
type: typeof SYNC_CHANGES_FROM_RECOIL; | ||
payload: Array<ChangeEntry>; | ||
} | ||
|
||
const syncChangesFromRecoilAction = (changes: Array<ChangeEntry>): SyncFromRecoilAction => ({ | ||
type: SYNC_CHANGES_FROM_RECOIL, | ||
payload: changes, | ||
}); | ||
|
||
const applyChangesToState = (state: ReduxState, changes: Array<ChangeEntry>): ReduxState => { | ||
console.log('applyChangesToState()', state, changes); | ||
|
||
let newState = state; | ||
for (let i = 0; i < changes.length; i++) { | ||
const [path, value] = changes[i]; | ||
newState = setPath(newState, path, value); | ||
} | ||
|
||
return newState; | ||
}; | ||
|
||
const syncChangesFromRecoil = (rootReducer: Reducer): Reducer => { | ||
console.log('syncChangesFromRecoil reducer is registered!'); | ||
|
||
return (state, action) => { | ||
console.log('syncChangesFromRecoil reducer is running!', action, state); | ||
|
||
if (action.type === SYNC_CHANGES_FROM_RECOIL) { | ||
return applyChangesToState(state, action.payload); | ||
} else { | ||
return rootReducer(state, action); | ||
} | ||
}; | ||
}; | ||
|
||
export default syncChangesFromRecoil; | ||
export { applyChangesToState, syncChangesFromRecoilAction }; |
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