-
Notifications
You must be signed in to change notification settings - Fork 3
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
6 changed files
with
76 additions
and
51 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
---|---|---|
@@ -1,64 +1,84 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import * as v3 from '@reatom/core'; | ||
import { createAtom } from '~utils/atoms'; | ||
import { store as globalStore } from '~core/store/store'; | ||
import type { | ||
Action, | ||
ActionCreatorBinded, | ||
Atom, | ||
AtomSelfBinded, | ||
Fn, | ||
Rec, | ||
Store, | ||
} from '@reatom/core-v2'; | ||
import type { Atom, AtomSelfBinded, Fn, Rec, Store, TrackReducer } from '@reatom/core-v2'; | ||
|
||
type Tov2Actions<V3Actions extends Record<string, v3.Action>> = { | ||
[Properties in keyof V3Actions]: (args?: Parameters<V3Actions[Properties]>) => Action; | ||
// Define our own AnyAction type | ||
type AnyAction = { | ||
payload: any; | ||
type: string; | ||
v3action: v3.Action<any>; | ||
targets?: Atom[]; | ||
}; | ||
|
||
// Update the ActionCreatorBinded type | ||
type ActionCreatorBinded<P> = { | ||
(payload: P): AnyAction; | ||
type: string; | ||
dispatch: (payload: P) => void; | ||
v3action: v3.Action<any>; | ||
}; | ||
|
||
// [Properties in keyof Type]: Type[Properties] | ||
export function v3toV2< | ||
State, | ||
Deps extends Rec<Fn | Atom>, | ||
V3A extends Record<string, v3.Action>, | ||
>( | ||
v3atom: v3.Atom<State>, | ||
v3Actions?: V3A, | ||
store: Store = globalStore, | ||
): AtomSelfBinded<State, Tov2Actions<V3A>> { | ||
// @ts-expect-error - actions will be assigned later | ||
const v2Atom = createAtom<State, Deps>({}, () => {}, { store, v3atom }); | ||
V3A extends Record<string, v3.Action<any>>, | ||
T extends v3.Atom<State> | v3.AtomMut<State>, | ||
>(v3atom: T, v3Actions?: V3A, store: Store = globalStore) { | ||
const _v2Atom = createAtom<State, Deps>( | ||
{} as Deps, | ||
(() => {}) as unknown as TrackReducer<State, Deps>, | ||
{ | ||
store, | ||
v3atom, | ||
}, | ||
); | ||
|
||
// set correct v3atom type, because it's hardcoded in v2 types as v3.Atom | ||
const v2Atom = _v2Atom as Omit<typeof _v2Atom, 'v3atom'> & { v3atom: T }; | ||
|
||
if (v3Actions) { | ||
Object.entries(v3Actions) | ||
.map((act) => actionV3ToV2(act, [v2Atom], store)) | ||
.forEach(({ name, actionCreator }) => | ||
Object.assign(v2Atom, { [name]: actionCreator }), | ||
); | ||
.map(([name, action]) => actionV3ToV2([name, action], [v2Atom], store)) | ||
.forEach(({ name, actionCreator }) => { | ||
(v2Atom as any)[name] = actionCreator; | ||
}); | ||
} | ||
// @ts-expect-error - actions will be assigned later | ||
|
||
return v2Atom; | ||
} | ||
|
||
let actionIdCounter = 0; | ||
|
||
function actionV3ToV2( | ||
[name, action]: [string, v3.Action], | ||
targets?: Atom[], | ||
function actionV3ToV2<T extends any[], V>( | ||
[name, action]: [string, v3.Action<T>], | ||
targets?: V[], | ||
store = globalStore, | ||
): { name: string; actionCreator: ActionCreatorBinded } { | ||
const type = action.__reatom.name ?? `name_${actionIdCounter++}`; // `${actionName}_${v3atom?.__reatom.name}` | ||
): { name: string; actionCreator: ActionCreatorBinded<T[0]> } { | ||
const type = action.__reatom.name ?? `name_${actionIdCounter++}`; | ||
|
||
// @ts-expect-error - props will be assigned later | ||
const actionCreator: ActionCreatorBinded = function (payload) { | ||
const actionCreator: ActionCreatorBinded<T[0]> = function (payload: T[0]) { | ||
return { | ||
payload, | ||
type, | ||
v3action: action, | ||
targets: targets, | ||
targets: targets as Atom[], | ||
}; | ||
}; | ||
|
||
actionCreator.type = type; | ||
actionCreator.dispatch = (...a: any[]) => store.dispatch(actionCreator(...a)); | ||
actionCreator.v3action = v3.action(type); | ||
actionCreator.dispatch = (payload: T[0]) => store.dispatch(actionCreator(payload)); | ||
actionCreator.v3action = action; | ||
|
||
return { name, actionCreator }; | ||
} | ||
|
||
// TEST, to be removed | ||
const a3 = v3.atom(0, 'zoot'); | ||
const a2 = v3toV2(a3); | ||
const a3_in_a2 = a2.v3atom; | ||
const aa = v3.atom((_) => _); | ||
const aa2 = v3toV2(aa); | ||
aa2.v3atom; |