From 110d1941df64947cae1e0f3bab590bb352de156b Mon Sep 17 00:00:00 2001 From: rtritto Date: Sat, 17 Aug 2024 14:26:45 +0200 Subject: [PATCH] Init imports --- packages/solid-jotai-x/src/atomWithFn.ts | 2 +- .../solid-jotai-x/src/createAtomProvider.tsx | 26 ++++++++++--------- .../src/createAtomStore.spec.tsx | 19 ++++++++------ packages/solid-jotai-x/src/createAtomStore.ts | 15 ++++++----- .../solid-jotai-x/src/elementAtom.spec.tsx | 16 +++++++----- packages/solid-jotai-x/src/useHydrateStore.ts | 10 +++---- 6 files changed, 48 insertions(+), 40 deletions(-) diff --git a/packages/solid-jotai-x/src/atomWithFn.ts b/packages/solid-jotai-x/src/atomWithFn.ts index 5c759a1..3b28a6d 100644 --- a/packages/solid-jotai-x/src/atomWithFn.ts +++ b/packages/solid-jotai-x/src/atomWithFn.ts @@ -1,4 +1,4 @@ -import { atom } from 'jotai'; +import { atom } from 'solid-jotai'; import type { WritableAtom } from 'jotai/vanilla'; diff --git a/packages/solid-jotai-x/src/createAtomProvider.tsx b/packages/solid-jotai-x/src/createAtomProvider.tsx index 20b8e8a..f55c6e9 100644 --- a/packages/solid-jotai-x/src/createAtomProvider.tsx +++ b/packages/solid-jotai-x/src/createAtomProvider.tsx @@ -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'; @@ -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>( +const AtomStoreContext = createContext>( new Map() ); @@ -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)); @@ -49,7 +51,7 @@ export type ProviderProps = Partial & { scope?: string; initialValues?: Partial; resetKey?: any; - children: React.ReactNode; + children: JSX.Element; }; export const HydrateAtoms = ({ @@ -79,24 +81,24 @@ export const HydrateAtoms = ({ export const createAtomProvider = ( storeScope: N, atoms: SimpleWritableAtomRecord, - 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) => { const [storeState, setStoreState] = - React.useState(createStore()); + createSignal(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) { @@ -114,7 +116,7 @@ export const createAtomProvider = ( ); return newStoreContext; - }, [previousStoreContext, scope, storeState]); + }); return ( diff --git a/packages/solid-jotai-x/src/createAtomStore.spec.tsx b/packages/solid-jotai-x/src/createAtomStore.spec.tsx index 56465c3..54a3450 100644 --- a/packages/solid-jotai-x/src/createAtomStore.spec.tsx +++ b/packages/solid-jotai-x/src/createAtomStore.spec.tsx @@ -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'; @@ -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 ( <> @@ -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 ( <> @@ -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 ( <> @@ -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 ( <> diff --git a/packages/solid-jotai-x/src/createAtomStore.ts b/packages/solid-jotai-x/src/createAtomStore.ts index c52c764..3875986 100644 --- a/packages/solid-jotai-x/src/createAtomStore.ts +++ b/packages/solid-jotai-x/src/createAtomStore.ts @@ -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; @@ -114,7 +115,7 @@ export type AtomStoreApi< > = { name: N; } & { - [key in keyof Record, object>]: React.FC< + [key in keyof Record, object>]: Component< ProviderProps> >; } & { @@ -164,7 +165,7 @@ export interface CreateAtomStoreOptions< > { name: N; delay?: UseAtomOptions['delay']; - effect?: React.FC; + effect?: Component; extend?: (atomsWithoutExtend: StoreAtomsWithoutExtend) => E; } @@ -287,7 +288,7 @@ export const createAtomStore = < } } - const Provider: React.FC> = + const Provider: Component> = createAtomProvider( name, writableAtomsWithoutExtend, diff --git a/packages/solid-jotai-x/src/elementAtom.spec.tsx b/packages/solid-jotai-x/src/elementAtom.spec.tsx index 22fb729..49ba631 100644 --- a/packages/solid-jotai-x/src/elementAtom.spec.tsx +++ b/packages/solid-jotai-x/src/elementAtom.spec.tsx @@ -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'; @@ -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 ( @@ -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 ( @@ -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 ( diff --git a/packages/solid-jotai-x/src/useHydrateStore.ts b/packages/solid-jotai-x/src/useHydrateStore.ts index 138ce50..164d7dc 100644 --- a/packages/solid-jotai-x/src/useHydrateStore.ts +++ b/packages/solid-jotai-x/src/useHydrateStore.ts @@ -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, @@ -43,10 +43,10 @@ export const useSyncStore = ( const set = useSetAtom(atom, { store }); - React.useEffect(() => { + createEffect(() => { if (value !== undefined && value !== null) { set(value); } - }, [set, value]); + }); } };