vanilla.ts
This file
Path
src/vanilla.ts
100 lines · 3.3 KiB · TypeScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than f85898f point at it.
chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 1–100why, one step back1type SetStateInternal<T> = {2 _(3 partial: T | Partial<T> | { _(state: T): T | Partial<T> }['_'],4 replace?: false,5 ): void6 _(state: T | { _(state: T): T }['_'], replace: true): void7}['_']89export interface StoreApi<T> {10 setState: SetStateInternal<T>11 getState: () => T12 getInitialState: () => T13 subscribe: (listener: (state: T, prevState: T) => void) => () => void14}1516export type ExtractState<S> = S extends { getState: () => infer T } ? T : never1718type Get<T, K, F> = K extends keyof T ? T[K] : F1920export type Mutate<S, Ms> = number extends Ms['length' & keyof Ms]21 ? S22 : Ms extends []23 ? S24 : Ms extends [[infer Mi, infer Ma], ...infer Mrs]25 ? Mutate<StoreMutators<S, Ma>[Mi & StoreMutatorIdentifier], Mrs>26 : never2728export type StateCreator<29 T,30 Mis extends [StoreMutatorIdentifier, unknown][] = [],31 Mos extends [StoreMutatorIdentifier, unknown][] = [],32 U = T,33> = ((34 setState: Get<Mutate<StoreApi<T>, Mis>, 'setState', never>,35 getState: Get<Mutate<StoreApi<T>, Mis>, 'getState', never>,36 store: Mutate<StoreApi<T>, Mis>,37) => U) & { $$storeMutators?: Mos }3839// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-object-type40export interface StoreMutators<S, A> {}41export type StoreMutatorIdentifier = keyof StoreMutators<unknown, unknown>4243type CreateStore = {44 <T, Mos extends [StoreMutatorIdentifier, unknown][] = []>(45 initializer: StateCreator<T, [], Mos>,46 ): Mutate<StoreApi<T>, Mos>4748 <T>(): <Mos extends [StoreMutatorIdentifier, unknown][] = []>(49 initializer: StateCreator<T, [], Mos>,50 ) => Mutate<StoreApi<T>, Mos>51}5253type CreateStoreImpl = <54 T,55 Mos extends [StoreMutatorIdentifier, unknown][] = [],56>(57 initializer: StateCreator<T, [], Mos>,58) => Mutate<StoreApi<T>, Mos>5960const createStoreImpl: CreateStoreImpl = (createState) => {61 type TState = ReturnType<typeof createState>62 type Listener = (state: TState, prevState: TState) => void63 let state: TState64 const listeners: Set<Listener> = new Set()6566 const setState: StoreApi<TState>['setState'] = (partial, replace) => {67 // TODO: Remove type assertion once https://github.com/microsoft/TypeScript/issues/37663 is resolved68 // https://github.com/microsoft/TypeScript/issues/37663#issuecomment-75972834269 const nextState =70 typeof partial === 'function'71 ? (partial as (state: TState) => TState)(state)72 : partial73 if (!Object.is(nextState, state)) {74 const previousState = state75 state =76 (replace ?? (typeof nextState !== 'object' || nextState === null))77 ? (nextState as TState)78 : Object.assign({}, state, nextState)79 listeners.forEach((listener) => listener(state, previousState))80 }81 }8283 const getState: StoreApi<TState>['getState'] = () => state8485 const getInitialState: StoreApi<TState>['getInitialState'] = () =>86 initialState8788 const subscribe: StoreApi<TState>['subscribe'] = (listener) => {89 listeners.add(listener)90 // Unsubscribe91 return () => listeners.delete(listener)92 }9394 const api = { setState, getState, getInitialState, subscribe }95 const initialState = (state = createState(setState, getState, api))96 return api as any97}9899export const createStore = ((createState) =>100 createState ? createStoreImpl(createState) : createStoreImpl) as CreateStore