Skip to content

Commit

Permalink
v32 patch type
Browse files Browse the repository at this point in the history
  • Loading branch information
vkozio committed Oct 1, 2024
1 parent e4c7a71 commit de35d08
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 51 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@
"@reatom/core-v2": "^3.1.4",
"@reatom/hooks": "^3.5.5",
"@reatom/logger": "^3.8.4",
"@reatom/npm-react": "^3.8.10",
"@reatom/npm-react": "^3.9.0",
"@reatom/primitives": "^3.7.3",
"@reatom/react-v2": "^3.1.2",
"@sentry/react": "^8.18.0",
"@slack/web-api": "^7.3.1",
Expand Down
21 changes: 12 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/core/resources/bivariateStatisticsResource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const bivariateStatisticsDependencyAtom = v3toV2(
atom((ctx) => {
const focusedGeometry = ctx.spy(focusedGeometryAtom.v3atom);
return { focusedGeometry };
}),
}, 'bivariateStatisticsDependencyAtom'),
);

let worldStatsCache: Stat;
Expand Down
2 changes: 1 addition & 1 deletion src/features/intercom/IntercomBTN.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useAtom } from '@reatom/react-v2';
import { useAtom } from '@reatom/npm-react';
import { intercomVisibleAtom } from '.';
import './intercom.css';

Expand Down
9 changes: 5 additions & 4 deletions src/features/intercom/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { atom } from '@reatom/core';
import { cookieManagementService, permissionStatuses } from '~core/cookie_settings';
import { createBooleanAtom } from '~utils/atoms';
import { configRepo } from '~core/config';

export const intercomVisibleAtom = createBooleanAtom(false, 'intercomVisibleAtom');
import { store } from '~core/store/store';
const ctx = store.v3ctx;
export const intercomVisibleAtom = atom(false, 'intercomVisibleAtom');

export function initIntercom() {
const intercomPermission = cookieManagementService.requestPermission('Intercom');
intercomPermission.onStatusChange((status) => {
if (status === permissionStatuses.granted) {
connectAndConfigureIntercom();
intercomVisibleAtom.setTrue.dispatch(); // Add empty space in footer for intercom
intercomVisibleAtom(ctx, true); // Add empty space in footer for intercom
}
});
}
Expand Down
90 changes: 55 additions & 35 deletions src/utils/atoms/v3tov2.ts
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;

0 comments on commit de35d08

Please sign in to comment.