-
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.
- Loading branch information
Showing
11 changed files
with
161 additions
and
48 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,15 @@ | ||
import React from 'react'; | ||
|
||
interface SyncReduxToRecoilProps { | ||
enabled?: boolean; | ||
} | ||
|
||
const SyncReduxToRecoil: React.FC<SyncReduxToRecoilProps> = () => { | ||
throw new Error('SyncReduxToRecoil: not implemented'); | ||
}; | ||
|
||
SyncReduxToRecoil.defaultProps = { | ||
enabled: true, | ||
}; | ||
|
||
export default SyncReduxToRecoil; |
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,26 @@ | ||
import { RecoilState, selector } from 'recoil'; | ||
|
||
import { getReduxSourceAtom, getValueAtPath } from './util'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const atomFromRedux = <ReturnType = any>(namespace: string): RecoilState<ReturnType> => { | ||
// The leading dot is just a convention to make things easier to understand: empty parts are ignored | ||
const namespaceParts = namespace.split('.').filter((word) => !!word); | ||
|
||
// Although named "atomFromRedux", each instance is actually just a selector. They all pull from a single atom | ||
const selectorFromRedux = selector<ReturnType>({ | ||
key: `redux-to-recoil:${namespaceParts.join('.')}`, | ||
get: ({ get }) => { | ||
const reduxSourceAtom = getReduxSourceAtom(); | ||
const reduxState = get(reduxSourceAtom); | ||
return getValueAtPath(reduxState, namespaceParts); | ||
}, | ||
set: ({ get, set }) => { | ||
console.log('TODO: Bidirectional support', namespace, { get, set }); | ||
}, | ||
}); | ||
|
||
return selectorFromRedux; | ||
}; | ||
|
||
export default atomFromRedux; |
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 +1,8 @@ | ||
export default {}; | ||
export { default as SyncReduxToRecoil } from './SyncReduxToRecoil'; | ||
export * from './SyncReduxToRecoil'; | ||
|
||
export { default as atomFromRedux } from './atomFromRedux'; | ||
export * from './atomFromRedux'; | ||
|
||
export { default as selectorFromReselect } from './selectorFromReselect'; | ||
export * from './selectorFromReselect'; |
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,5 @@ | ||
const selectorFromReselect = (): never => { | ||
throw new Error('selectorFromReselect: not implemented'); | ||
}; | ||
|
||
export default selectorFromReselect; |
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,2 @@ | ||
// Microbundle manages this: it's not a real variable | ||
declare const __DEV__: boolean; |
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,40 @@ | ||
import { RecoilState, atom } from 'recoil'; | ||
|
||
type ReduxState = unknown; | ||
let reduxSourceAtom: RecoilState<ReduxState>; | ||
|
||
const getReduxSourceAtom = (): typeof reduxSourceAtom => { | ||
if (__DEV__ && !reduxSourceAtom) { | ||
throw new Error('Cannot access redux state: SyncReduxToRecoil must be mounted'); | ||
} | ||
return reduxSourceAtom; | ||
}; | ||
|
||
const initializeReduxSourceAtom = (reduxState: ReduxState): void => { | ||
if (__DEV__ && !reduxSourceAtom) { | ||
throw new Error('Cannot set redux state: SyncReduxToRecoil is already mounted'); | ||
} | ||
|
||
reduxSourceAtom = atom({ | ||
key: '__redux-to-recoil', | ||
default: reduxState, | ||
}); | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const getValueAtPath = (state: ReduxState, pathParts: Array<string>): any => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let currentResult: any = state; | ||
let i = 0; | ||
while (currentResult && i < pathParts.length) { | ||
currentResult = currentResult[pathParts[i]]; | ||
i++; | ||
} | ||
|
||
if (i === pathParts.length) { | ||
return currentResult; | ||
} | ||
return undefined; | ||
}; | ||
|
||
export { getReduxSourceAtom, initializeReduxSourceAtom, getValueAtPath }; |
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,11 @@ | ||
/* eslint-env jest */ | ||
|
||
import atomFromRedux from '../src/atomFromRedux'; | ||
|
||
describe('TODO', () => { | ||
it('@TODO', () => { | ||
const sampleAtom = atomFromRedux('.sample'); | ||
|
||
console.log('@TODO', sampleAtom); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
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