Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid slow types #2472

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/react/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { createStore, getDefaultStore } from '../vanilla.ts'

type Store = ReturnType<typeof createStore>

const StoreContext = createContext<Store | undefined>(undefined)
type StoreContextType = ReturnType<typeof createContext<Store | undefined>>
const StoreContext: StoreContextType = createContext<Store | undefined>(
undefined,
)

type Options = {
store?: Store
Expand Down
6 changes: 3 additions & 3 deletions src/react/utils/useResetAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import type { WritableAtom } from '../../vanilla.ts'

type Options = Parameters<typeof useSetAtom>[1]

export function useResetAtom(
anAtom: WritableAtom<unknown, [typeof RESET], unknown>,
export function useResetAtom<T>(
anAtom: WritableAtom<unknown, [typeof RESET], T>,
options?: Options,
) {
): () => T {
const setAtom = useSetAtom(anAtom, options)
const resetAtom = useCallback(() => setAtom(RESET), [setAtom])
return resetAtom
Expand Down
26 changes: 19 additions & 7 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type OnUnmount = () => void
type Getter = Parameters<AnyAtom['read']>[0]
type Setter = Parameters<AnyWritableAtom['write']>[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 = <T extends Atom<AnyValue>>(
Expand All @@ -20,7 +20,7 @@ const isActuallyWritableAtom = (atom: AnyAtom): atom is AnyWritableAtom =>
!!(atom as AnyWritableAtom).write

type CancelPromise = (next?: Promise<unknown>) => void
const cancelPromiseMap = new WeakMap<Promise<unknown>, CancelPromise>()
const cancelPromiseMap: WeakMap<Promise<unknown>, CancelPromise> = new WeakMap()

const registerCancelPromise = (
promise: Promise<unknown>,
Expand Down Expand Up @@ -124,6 +124,8 @@ type Mounted = {
u?: OnUnmount
}

type MountedAtoms = Set<AnyAtom>

// for debugging purpose only
type StoreListenerRev2 = (
action:
Expand All @@ -134,7 +136,19 @@ type StoreListenerRev2 = (
| { type: 'restore'; flushed: Set<AnyAtom> },
) => void

type MountedAtoms = Set<AnyAtom>
type Store = {
get: <Value>(atom: Atom<Value>) => Value
set: <Value, Args extends unknown[], Result>(
atom: WritableAtom<Value, Args, Result>,
...args: Args
) => Result
sub: (atom: AnyAtom, listener: () => void) => () => void
dev_subscribe_store?: (l: StoreListenerRev2, rev: 2) => () => void
dev_get_mounted_atoms?: () => IterableIterator<AnyAtom>
dev_get_atom_state?: (a: AnyAtom) => AtomState | undefined
dev_get_mounted?: (a: AnyAtom) => Mounted | undefined
dev_restore_atoms?: (values: Iterable<readonly [AnyAtom, AnyValue]>) => void
}

/**
* Create a new store. Each store is an independent, isolated universe of atom
Expand All @@ -152,7 +166,7 @@ type MountedAtoms = Set<AnyAtom>
*
* @returns A store.
*/
export const createStore = () => {
export const createStore = (): Store => {
const atomStateMap = new WeakMap<AnyAtom, AtomState>()
const mountedMap = new WeakMap<AnyAtom, Mounted>()
const pendingStack: Set<AnyAtom>[] = []
Expand Down Expand Up @@ -824,11 +838,9 @@ export const createStore = () => {
}
}

type Store = ReturnType<typeof createStore>

let defaultStore: Store | undefined

export const getDefaultStore = () => {
export const getDefaultStore = (): Store => {
if (!defaultStore) {
defaultStore = createStore()
if (import.meta.env?.MODE !== 'production') {
Expand Down
6 changes: 0 additions & 6 deletions src/vanilla/utils/atomWithObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import type { Atom, Getter, WritableAtom } from '../../vanilla.ts'
type Timeout = ReturnType<typeof setTimeout>
type AnyError = unknown

declare global {
interface SymbolConstructor {
readonly observable: symbol
}
}

type Subscription = {
unsubscribe: () => void
}
Expand Down
5 changes: 4 additions & 1 deletion src/vanilla/utils/atomWithReset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ type WithInitialValue<Value> = {
init: Value
}

export function atomWithReset<Value>(initialValue: Value) {
export function atomWithReset<Value>(
initialValue: Value,
): WritableAtom<Value, [SetStateActionWithReset<Value>], void> &
WithInitialValue<Value> {
type Update = SetStateActionWithReset<Value>
const anAtom = atom<Value, [Update], void>(
initialValue,
Expand Down
4 changes: 2 additions & 2 deletions src/vanilla/utils/freezeAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ export function freezeAtom<AtomType extends Atom<unknown>>(

export function freezeAtomCreator<
CreateAtom extends (...params: never[]) => Atom<unknown>,
>(createAtom: CreateAtom) {
>(createAtom: CreateAtom): CreateAtom {
return ((...params: never[]) => {
const anAtom = createAtom(...params)
const origRead = anAtom.read
anAtom.read = function (get, options) {
return deepFreeze(origRead.call(this, get, options))
}
return anAtom
}) as CreateAtom
}) as never
}
Loading