Skip to content

Commit

Permalink
Init imports
Browse files Browse the repository at this point in the history
  • Loading branch information
rtritto committed Aug 17, 2024
1 parent abf6546 commit 110d194
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 40 deletions.
2 changes: 1 addition & 1 deletion packages/solid-jotai-x/src/atomWithFn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { atom } from 'jotai';
import { atom } from 'solid-jotai';

import type { WritableAtom } from 'jotai/vanilla';

Expand Down
26 changes: 14 additions & 12 deletions packages/solid-jotai-x/src/createAtomProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use client';

import React from 'react';
import { createStore } from 'jotai/vanilla';
import { createStore } from 'solid-jotai';
import { createContext, createEffect, createMemo, createSignal, useContext } from 'solid-js';

import type { Component, JSX } from 'solid-js';

import { JotaiStore, SimpleWritableAtomRecord } from './createAtomStore';
import { useHydrateStore, useSyncStore } from './useHydrateStore';
Expand All @@ -15,7 +17,7 @@ const getFullyQualifiedScope = (storeName: string, scope: string) => {
* to reference any provider belonging to the store, regardless of scope.
*/
const PROVIDER_SCOPE = 'provider';
const AtomStoreContext = React.createContext<Map<string, JotaiStore>>(
const AtomStoreContext = createContext<Map<string, JotaiStore>>(
new Map()
);

Expand All @@ -30,7 +32,7 @@ export const useAtomStore = (
scope: string = PROVIDER_SCOPE,
warnIfUndefined: boolean = true
): JotaiStore | undefined => {
const storeContext = React.useContext(AtomStoreContext);
const storeContext = useContext(AtomStoreContext);
const store =
storeContext.get(getFullyQualifiedScope(storeName, scope)) ??
storeContext.get(getFullyQualifiedScope(storeName, PROVIDER_SCOPE));
Expand All @@ -49,7 +51,7 @@ export type ProviderProps<T extends object> = Partial<T> & {
scope?: string;
initialValues?: Partial<T>;
resetKey?: any;
children: React.ReactNode;
children: JSX.Element;
};

export const HydrateAtoms = <T extends object>({
Expand Down Expand Up @@ -79,24 +81,24 @@ export const HydrateAtoms = <T extends object>({
export const createAtomProvider = <T extends object, N extends string = ''>(
storeScope: N,
atoms: SimpleWritableAtomRecord<T>,
options: { effect?: React.FC } = {}
options: { effect?: Component } = {}
) => {
const Effect = options.effect;

// eslint-disable-next-line react/display-name
return ({ store, scope, children, resetKey, ...props }: ProviderProps<T>) => {
const [storeState, setStoreState] =
React.useState<JotaiStore>(createStore());
createSignal<JotaiStore>(createStore());

React.useEffect(() => {
createEffect(() => {
if (resetKey) {
setStoreState(createStore());
}
}, [resetKey]);
});

const previousStoreContext = React.useContext(AtomStoreContext);
const previousStoreContext = useContext(AtomStoreContext);

const storeContext = React.useMemo(() => {
const storeContext = createMemo(() => {
const newStoreContext = new Map(previousStoreContext);

if (scope) {
Expand All @@ -114,7 +116,7 @@ export const createAtomProvider = <T extends object, N extends string = ''>(
);

return newStoreContext;
}, [previousStoreContext, scope, storeState]);
});

return (
<AtomStoreContext.Provider value={storeContext}>
Expand Down
19 changes: 11 additions & 8 deletions packages/solid-jotai-x/src/createAtomStore.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import '@testing-library/jest-dom';

import React from 'react';
import { act, queryByText, render, renderHook } from '@testing-library/react';
import { atom, PrimitiveAtom, useAtomValue } from 'jotai';
import { splitAtom } from 'jotai/utils';
import { atom, useAtomValue } from 'solid-jotai';
import { createSignal } from 'solid-js';

import type { PrimitiveAtom } from 'jotai';
import type { JSX } from 'solid-js';

import { createAtomStore } from './createAtomStore';

Expand Down Expand Up @@ -54,8 +57,8 @@ describe('createAtomStore', () => {
const MUTABLE_PROVIDER_INITIAL_AGE = 19;
const MUTABLE_PROVIDER_NEW_AGE = 20;

const MutableProvider = ({ children }: { children: React.ReactNode }) => {
const [age, setAge] = React.useState(MUTABLE_PROVIDER_INITIAL_AGE);
const MutableProvider = ({ children }: { children: JSX.Element }) => {
const [age, setAge] = createSignal(MUTABLE_PROVIDER_INITIAL_AGE);

return (
<>
Expand All @@ -74,9 +77,9 @@ describe('createAtomStore', () => {
const BecomeFriendsProvider = ({
children,
}: {
children: React.ReactNode;
children: JSX.Element;
}) => {
const [becameFriends, setBecameFriends] = React.useState(false);
const [becameFriends, setBecameFriends] = createSignal(false);

return (
<>
Expand Down Expand Up @@ -111,7 +114,7 @@ describe('createAtomStore', () => {

const BecomeFriendsSetter = () => {
const setBecomeFriends = useMyTestStoreStore().set.becomeFriends();
const [becameFriends, setBecameFriends] = React.useState(false);
const [becameFriends, setBecameFriends] = createSignal(false);

return (
<>
Expand All @@ -129,7 +132,7 @@ describe('createAtomStore', () => {

const BecomeFriendsUser = () => {
const [, setBecomeFriends] = useMyTestStoreStore().use.becomeFriends();
const [becameFriends, setBecameFriends] = React.useState(false);
const [becameFriends, setBecameFriends] = createSignal(false);

return (
<>
Expand Down
15 changes: 8 additions & 7 deletions packages/solid-jotai-x/src/createAtomStore.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React from 'react';
import { useAtom, useAtomValue, useSetAtom } from 'jotai';
import { useHydrateAtoms } from 'jotai/utils';
import { useAtom, useAtomValue, useSetAtom, type createStore } from 'solid-jotai';
import { useHydrateAtoms } from 'solid-jotai/utils';

import type { Atom, WritableAtom } from 'jotai/vanilla';
import type { Component } from 'solid-js';

import { atomWithFn } from './atomWithFn';
import { createAtomProvider, useAtomStore } from './createAtomProvider';

import type { ProviderProps } from './createAtomProvider';
import type { Atom, createStore, WritableAtom } from 'jotai/vanilla';

export type JotaiStore = ReturnType<typeof createStore>;

Expand Down Expand Up @@ -114,7 +115,7 @@ export type AtomStoreApi<
> = {
name: N;
} & {
[key in keyof Record<NameProvider<N>, object>]: React.FC<
[key in keyof Record<NameProvider<N>, object>]: Component<
ProviderProps<StoreInitialValues<T>>
>;
} & {
Expand Down Expand Up @@ -164,7 +165,7 @@ export interface CreateAtomStoreOptions<
> {
name: N;
delay?: UseAtomOptions['delay'];
effect?: React.FC;
effect?: Component;
extend?: (atomsWithoutExtend: StoreAtomsWithoutExtend<T>) => E;
}

Expand Down Expand Up @@ -287,7 +288,7 @@ export const createAtomStore = <
}
}

const Provider: React.FC<ProviderProps<MyStoreInitialValues>> =
const Provider: Component<ProviderProps<MyStoreInitialValues>> =
createAtomProvider<MyStoreInitialValues, N>(
name,
writableAtomsWithoutExtend,
Expand Down
16 changes: 9 additions & 7 deletions packages/solid-jotai-x/src/elementAtom.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import '@testing-library/jest-dom';

import React from 'react';
import { act, render } from '@testing-library/react';
import { createMemo, createSignal } from 'solid-js';

import type { JSX } from 'solid-js';

import { createAtomStore } from './createAtomStore';

Expand Down Expand Up @@ -63,9 +65,9 @@ describe('ElementProvider', () => {
children,
}: {
name: string;
children: React.ReactNode;
children: JSX.Element;
}) => {
const element = React.useMemo(() => makeNameElement(name), [name]);
const element = createMemo(() => makeNameElement(name), [name]);

return (
<ElementProvider element={element} scope="name">
Expand All @@ -79,9 +81,9 @@ describe('ElementProvider', () => {
children,
}: {
age: number;
children: React.ReactNode;
children: JSX.Element;
}) => {
const element = React.useMemo(() => makeAgeElement(age), [age]);
const element = createMemo(() => makeAgeElement(age), [age]);

return (
<ElementProvider element={element} scope="age">
Expand All @@ -99,9 +101,9 @@ describe('ElementProvider', () => {
initialAge: number;
increment: number;
buttonLabel: string;
children: React.ReactNode;
children: JSX.Element;
}) => {
const [age, setAge] = React.useState(initialAge);
const [age, setAge] = createSignal(initialAge);

return (
<AgeElementProvider age={age}>
Expand Down
10 changes: 5 additions & 5 deletions packages/solid-jotai-x/src/useHydrateStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { useSetAtom } from 'jotai';
import { useHydrateAtoms } from 'jotai/utils';
import { createEffect } from 'solid-js';
import { useSetAtom } from 'solid-jotai';
import { useHydrateAtoms } from 'solid-jotai/utils';

import {
SimpleWritableAtomRecord,
Expand Down Expand Up @@ -43,10 +43,10 @@ export const useSyncStore = (

const set = useSetAtom(atom, { store });

React.useEffect(() => {
createEffect(() => {
if (value !== undefined && value !== null) {
set(value);
}
}, [set, value]);
});
}
};

0 comments on commit 110d194

Please sign in to comment.