Skip to main content
Module

x/jotai/src/core/atom.ts

👻 Primitive and flexible state management for React
Go to Latest
File
type Awaited<T> = T extends Promise<infer V> ? Awaited<V> : T
type Getter = { <Value>(atom: Atom<Value | Promise<Value>>): Value <Value>(atom: Atom<Promise<Value>>): Value <Value>(atom: Atom<Value>): Awaited<Value>}
type WriteGetter = Getter & { <Value>( atom: Atom<Value | Promise<Value>>, options: { unstable_promise: true } ): Promise<Value> | Value <Value>(atom: Atom<Promise<Value>>, options: { unstable_promise: true }): | Promise<Value> | Value <Value>(atom: Atom<Value>, options: { unstable_promise: true }): | Promise<Awaited<Value>> | Awaited<Value>}
type Setter = { <Value, Result extends void | Promise<void>>( atom: WritableAtom<Value, undefined, Result> ): Result <Value, Update, Result extends void | Promise<void>>( atom: WritableAtom<Value, Update, Result>, update: Update ): Result}
type Read<Value> = (get: Getter) => Value
type Write<Update, Result extends void | Promise<void>> = ( get: WriteGetter, set: Setter, update: Update) => Result
type WithInitialValue<Value> = { init: Value}
export type Scope = symbol | string | number
// Not exported for public API// Are there better typings?export type SetAtom< Update, Result extends void | Promise<void>> = undefined extends Update ? (update?: Update) => Result : (update: Update) => Result
type OnUnmount = () => voidtype OnMount<Update, Result extends void | Promise<void>> = < S extends SetAtom<Update, Result>>( setAtom: S) => OnUnmount | void
export type Atom<Value> = { toString: () => string debugLabel?: string read: Read<Value>}
export type WritableAtom< Value, Update, Result extends void | Promise<void> = void> = Atom<Value> & { write: Write<Update, Result> onMount?: OnMount<Update, Result>}
type SetStateAction<Value> = Value | ((prev: Value) => Value)
export type PrimitiveAtom<Value> = WritableAtom<Value, SetStateAction<Value>>
let keyCount = 0 // global key count for all atoms
// writable derived atomexport function atom<Value, Update, Result extends void | Promise<void> = void>( read: Read<Value>, write: Write<Update, Result>): WritableAtom<Value, Update, Result>
// read-only derived atomexport function atom<Value>(read: Read<Value>): Atom<Value>
// invalid function in the first argumentexport function atom(invalidFunction: (...args: any) => any, write?: any): never
// write-only derived atomexport function atom<Value, Update, Result extends void | Promise<void> = void>( initialValue: Value, write: Write<Update, Result>): WritableAtom<Value, Update, Result> & WithInitialValue<Value>
// primitive atomexport function atom<Value>( initialValue: Value): PrimitiveAtom<Value> & WithInitialValue<Value>
export function atom<Value, Update, Result extends void | Promise<void>>( read: Value | Read<Value>, write?: Write<Update, Result>) { const key = `atom${++keyCount}` const config = { toString: () => key, } as WritableAtom<Value, Update, Result> & { init?: Value } if (typeof read === 'function') { config.read = read as Read<Value> } else { config.init = read config.read = (get) => get(config) config.write = (get, set, update) => set(config, typeof update === 'function' ? update(get(config)) : update) } if (write) { config.write = write } return config}