From 3aec3475962852c0a9518a61489a07d411e60a1c Mon Sep 17 00:00:00 2001 From: Daishi Kato Date: Fri, 29 Mar 2024 18:12:58 +0900 Subject: [PATCH] fix: avoid slow types (#2472) --- src/react/Provider.ts | 5 ++++- src/react/utils/useResetAtom.ts | 6 +++--- src/vanilla/store.ts | 26 ++++++++++++++++++------- src/vanilla/utils/atomWithObservable.ts | 6 ------ src/vanilla/utils/atomWithReset.ts | 5 ++++- src/vanilla/utils/freezeAtom.ts | 4 ++-- 6 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/react/Provider.ts b/src/react/Provider.ts index 5819561dea..3c57cee40c 100644 --- a/src/react/Provider.ts +++ b/src/react/Provider.ts @@ -4,7 +4,10 @@ import { createStore, getDefaultStore } from '../vanilla.ts' type Store = ReturnType -const StoreContext = createContext(undefined) +type StoreContextType = ReturnType> +const StoreContext: StoreContextType = createContext( + undefined, +) type Options = { store?: Store diff --git a/src/react/utils/useResetAtom.ts b/src/react/utils/useResetAtom.ts index baa7a77639..1d13125350 100644 --- a/src/react/utils/useResetAtom.ts +++ b/src/react/utils/useResetAtom.ts @@ -5,10 +5,10 @@ import type { WritableAtom } from '../../vanilla.ts' type Options = Parameters[1] -export function useResetAtom( - anAtom: WritableAtom, +export function useResetAtom( + anAtom: WritableAtom, options?: Options, -) { +): () => T { const setAtom = useSetAtom(anAtom, options) const resetAtom = useCallback(() => setAtom(RESET), [setAtom]) return resetAtom diff --git a/src/vanilla/store.ts b/src/vanilla/store.ts index 36fa46cf22..453e682c2a 100644 --- a/src/vanilla/store.ts +++ b/src/vanilla/store.ts @@ -8,7 +8,7 @@ type OnUnmount = () => void type Getter = Parameters[0] type Setter = Parameters[1] -const isSelfAtom = (atom: AnyAtom, a: AnyAtom) => +const isSelfAtom = (atom: AnyAtom, a: AnyAtom): boolean => atom.unstable_is ? atom.unstable_is(a) : a === atom const hasInitialValue = >( @@ -20,7 +20,7 @@ const isActuallyWritableAtom = (atom: AnyAtom): atom is AnyWritableAtom => !!(atom as AnyWritableAtom).write type CancelPromise = (next?: Promise) => void -const cancelPromiseMap = new WeakMap, CancelPromise>() +const cancelPromiseMap: WeakMap, CancelPromise> = new WeakMap() const registerCancelPromise = ( promise: Promise, @@ -124,6 +124,8 @@ type Mounted = { u?: OnUnmount } +type MountedAtoms = Set + // for debugging purpose only type StoreListenerRev2 = ( action: @@ -134,7 +136,19 @@ type StoreListenerRev2 = ( | { type: 'restore'; flushed: Set }, ) => void -type MountedAtoms = Set +type Store = { + get: (atom: Atom) => Value + set: ( + atom: WritableAtom, + ...args: Args + ) => Result + sub: (atom: AnyAtom, listener: () => void) => () => void + dev_subscribe_store?: (l: StoreListenerRev2, rev: 2) => () => void + dev_get_mounted_atoms?: () => IterableIterator + dev_get_atom_state?: (a: AnyAtom) => AtomState | undefined + dev_get_mounted?: (a: AnyAtom) => Mounted | undefined + dev_restore_atoms?: (values: Iterable) => void +} /** * Create a new store. Each store is an independent, isolated universe of atom @@ -152,7 +166,7 @@ type MountedAtoms = Set * * @returns A store. */ -export const createStore = () => { +export const createStore = (): Store => { const atomStateMap = new WeakMap() const mountedMap = new WeakMap() const pendingStack: Set[] = [] @@ -824,11 +838,9 @@ export const createStore = () => { } } -type Store = ReturnType - let defaultStore: Store | undefined -export const getDefaultStore = () => { +export const getDefaultStore = (): Store => { if (!defaultStore) { defaultStore = createStore() if (import.meta.env?.MODE !== 'production') { diff --git a/src/vanilla/utils/atomWithObservable.ts b/src/vanilla/utils/atomWithObservable.ts index fa24605456..6c2c8f2293 100644 --- a/src/vanilla/utils/atomWithObservable.ts +++ b/src/vanilla/utils/atomWithObservable.ts @@ -4,12 +4,6 @@ import type { Atom, Getter, WritableAtom } from '../../vanilla.ts' type Timeout = ReturnType type AnyError = unknown -declare global { - interface SymbolConstructor { - readonly observable: symbol - } -} - type Subscription = { unsubscribe: () => void } diff --git a/src/vanilla/utils/atomWithReset.ts b/src/vanilla/utils/atomWithReset.ts index 8dff76b7e5..2d9ae5929c 100644 --- a/src/vanilla/utils/atomWithReset.ts +++ b/src/vanilla/utils/atomWithReset.ts @@ -13,7 +13,10 @@ type WithInitialValue = { init: Value } -export function atomWithReset(initialValue: Value) { +export function atomWithReset( + initialValue: Value, +): WritableAtom], void> & + WithInitialValue { type Update = SetStateActionWithReset const anAtom = atom( initialValue, diff --git a/src/vanilla/utils/freezeAtom.ts b/src/vanilla/utils/freezeAtom.ts index 6918045760..e779c2d4aa 100644 --- a/src/vanilla/utils/freezeAtom.ts +++ b/src/vanilla/utils/freezeAtom.ts @@ -30,7 +30,7 @@ export function freezeAtom>( export function freezeAtomCreator< CreateAtom extends (...params: never[]) => Atom, ->(createAtom: CreateAtom) { +>(createAtom: CreateAtom): CreateAtom { return ((...params: never[]) => { const anAtom = createAtom(...params) const origRead = anAtom.read @@ -38,5 +38,5 @@ export function freezeAtomCreator< return deepFreeze(origRead.call(this, get, options)) } return anAtom - }) as CreateAtom + }) as never }