Skip to content

Commit 3aec347

Browse files
authored
fix: avoid slow types (#2472)
1 parent 42f324c commit 3aec347

6 files changed

Lines changed: 32 additions & 20 deletions

File tree

src/react/Provider.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { createStore, getDefaultStore } from '../vanilla.ts'
44

55
type Store = ReturnType<typeof createStore>
66

7-
const StoreContext = createContext<Store | undefined>(undefined)
7+
type StoreContextType = ReturnType<typeof createContext<Store | undefined>>
8+
const StoreContext: StoreContextType = createContext<Store | undefined>(
9+
undefined,
10+
)
811

912
type Options = {
1013
store?: Store

src/react/utils/useResetAtom.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import type { WritableAtom } from '../../vanilla.ts'
55

66
type Options = Parameters<typeof useSetAtom>[1]
77

8-
export function useResetAtom(
9-
anAtom: WritableAtom<unknown, [typeof RESET], unknown>,
8+
export function useResetAtom<T>(
9+
anAtom: WritableAtom<unknown, [typeof RESET], T>,
1010
options?: Options,
11-
) {
11+
): () => T {
1212
const setAtom = useSetAtom(anAtom, options)
1313
const resetAtom = useCallback(() => setAtom(RESET), [setAtom])
1414
return resetAtom

src/vanilla/store.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type OnUnmount = () => void
88
type Getter = Parameters<AnyAtom['read']>[0]
99
type Setter = Parameters<AnyWritableAtom['write']>[1]
1010

11-
const isSelfAtom = (atom: AnyAtom, a: AnyAtom) =>
11+
const isSelfAtom = (atom: AnyAtom, a: AnyAtom): boolean =>
1212
atom.unstable_is ? atom.unstable_is(a) : a === atom
1313

1414
const hasInitialValue = <T extends Atom<AnyValue>>(
@@ -20,7 +20,7 @@ const isActuallyWritableAtom = (atom: AnyAtom): atom is AnyWritableAtom =>
2020
!!(atom as AnyWritableAtom).write
2121

2222
type CancelPromise = (next?: Promise<unknown>) => void
23-
const cancelPromiseMap = new WeakMap<Promise<unknown>, CancelPromise>()
23+
const cancelPromiseMap: WeakMap<Promise<unknown>, CancelPromise> = new WeakMap()
2424

2525
const registerCancelPromise = (
2626
promise: Promise<unknown>,
@@ -124,6 +124,8 @@ type Mounted = {
124124
u?: OnUnmount
125125
}
126126

127+
type MountedAtoms = Set<AnyAtom>
128+
127129
// for debugging purpose only
128130
type StoreListenerRev2 = (
129131
action:
@@ -134,7 +136,19 @@ type StoreListenerRev2 = (
134136
| { type: 'restore'; flushed: Set<AnyAtom> },
135137
) => void
136138

137-
type MountedAtoms = Set<AnyAtom>
139+
type Store = {
140+
get: <Value>(atom: Atom<Value>) => Value
141+
set: <Value, Args extends unknown[], Result>(
142+
atom: WritableAtom<Value, Args, Result>,
143+
...args: Args
144+
) => Result
145+
sub: (atom: AnyAtom, listener: () => void) => () => void
146+
dev_subscribe_store?: (l: StoreListenerRev2, rev: 2) => () => void
147+
dev_get_mounted_atoms?: () => IterableIterator<AnyAtom>
148+
dev_get_atom_state?: (a: AnyAtom) => AtomState | undefined
149+
dev_get_mounted?: (a: AnyAtom) => Mounted | undefined
150+
dev_restore_atoms?: (values: Iterable<readonly [AnyAtom, AnyValue]>) => void
151+
}
138152

139153
/**
140154
* Create a new store. Each store is an independent, isolated universe of atom
@@ -152,7 +166,7 @@ type MountedAtoms = Set<AnyAtom>
152166
*
153167
* @returns A store.
154168
*/
155-
export const createStore = () => {
169+
export const createStore = (): Store => {
156170
const atomStateMap = new WeakMap<AnyAtom, AtomState>()
157171
const mountedMap = new WeakMap<AnyAtom, Mounted>()
158172
const pendingStack: Set<AnyAtom>[] = []
@@ -824,11 +838,9 @@ export const createStore = () => {
824838
}
825839
}
826840

827-
type Store = ReturnType<typeof createStore>
828-
829841
let defaultStore: Store | undefined
830842

831-
export const getDefaultStore = () => {
843+
export const getDefaultStore = (): Store => {
832844
if (!defaultStore) {
833845
defaultStore = createStore()
834846
if (import.meta.env?.MODE !== 'production') {

src/vanilla/utils/atomWithObservable.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ import type { Atom, Getter, WritableAtom } from '../../vanilla.ts'
44
type Timeout = ReturnType<typeof setTimeout>
55
type AnyError = unknown
66

7-
declare global {
8-
interface SymbolConstructor {
9-
readonly observable: symbol
10-
}
11-
}
12-
137
type Subscription = {
148
unsubscribe: () => void
159
}

src/vanilla/utils/atomWithReset.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ type WithInitialValue<Value> = {
1313
init: Value
1414
}
1515

16-
export function atomWithReset<Value>(initialValue: Value) {
16+
export function atomWithReset<Value>(
17+
initialValue: Value,
18+
): WritableAtom<Value, [SetStateActionWithReset<Value>], void> &
19+
WithInitialValue<Value> {
1720
type Update = SetStateActionWithReset<Value>
1821
const anAtom = atom<Value, [Update], void>(
1922
initialValue,

src/vanilla/utils/freezeAtom.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ export function freezeAtom<AtomType extends Atom<unknown>>(
3030

3131
export function freezeAtomCreator<
3232
CreateAtom extends (...params: never[]) => Atom<unknown>,
33-
>(createAtom: CreateAtom) {
33+
>(createAtom: CreateAtom): CreateAtom {
3434
return ((...params: never[]) => {
3535
const anAtom = createAtom(...params)
3636
const origRead = anAtom.read
3737
anAtom.read = function (get, options) {
3838
return deepFreeze(origRead.call(this, get, options))
3939
}
4040
return anAtom
41-
}) as CreateAtom
41+
}) as never
4242
}

0 commit comments

Comments
 (0)