import type { MutableAtomToken, ReadableFamilyToken, ReadableToken, ReadonlySelectorFamilyToken, ReadonlySelectorToken, RegularAtomToken, WritableFamilyToken, WritableSelectorFamilyToken, WritableSelectorToken, WritableToken, } from "atom.io" import type { Canonical } from "atom.io/foundations/canonical" import type { Transceiver } from "atom.io/internal" import { findInStore, IMPLICIT } from "atom.io/internal" import type { MutableAtomFamilyToken, RegularAtomFamilyToken } from "./tokens.ts" /** * Finds a {@link MutableAtomToken} in the store, without accessing its value. * * In an ephemeral store, this will create a new atom if one does not exist with the given key. * * In an immortal store, a "counterfeit" atom token will be returned in this case and a warning will be logged. * * @param token - A {@link MutableAtomFamilyToken} * @param key - The key of the state * @returns * The current value of the state * @overload Mutable Atom */ export function findState< T extends Transceiver, K extends Canonical, Key extends K, >(token: MutableAtomFamilyToken, key: Key): MutableAtomToken /** * Finds a {@link RegularAtomToken} in the store, without accessing its value. * * In an ephemeral store, this will create a new atom if one does not exist with the given key. * * In an immortal store, a "counterfeit" atom token will be returned in this case and a warning will be logged. * * @param token - The token of the state family * @param key - The key of the state * @returns * The current value of the state * @overload Regular Atom */ export function findState( token: RegularAtomFamilyToken, key: Key, ): RegularAtomToken /** * Finds a {@link WritableSelectorToken} in the store, without accessing its value. * * In an ephemeral store, this will create a new selector if one does not exist with the given key. * * In an immortal store, a "counterfeit" selector token will be returned in this case and a warning will be logged. * * @param token - The token of the state family * @param key - The key of the state * @returns * The current value of the state * @overload Writable Selector */ export function findState( token: WritableSelectorFamilyToken, key: Key, ): WritableSelectorToken /** * Finds a {@link ReadonlySelectorToken} in the store, without accessing its value. * * In an ephemeral store, this will create a new selector if one does not exist with the given key. * * In an immortal store, a "counterfeit" selector token will be returned in this case and a warning will be logged. * * @param token - The token of the state family * @param key - The key of the state * @returns * The current value of the state * @overload Readonly Selector */ export function findState( token: ReadonlySelectorFamilyToken, key: Key, ): ReadonlySelectorToken /** * Finds a {@link WritableToken} in the store, without accessing its value. * * In an ephemeral store, this will create a new atom or selector if one does not exist with the given key. * * In an immortal store, a "counterfeit" token will be returned in this case and a warning will be logged. * * @param token - The token of the state family * @param key - The key of the state * @returns * The current value of the state * @overload Writable State */ export function findState( token: WritableFamilyToken, key: Key, ): WritableToken /** * Finds a {@link MutableAtomToken} in the store, without accessing its value. * * In an ephemeral store, this will create a new atom or selector if one does not exist with the given key. * * In an immortal store, a "counterfeit" token will be returned in this case and a warning will be logged. * * @param token - A {@link ReadableFamilyToken} * @param key - The key of the state * @returns * The current value of the state * @overload Unknown * @default */ export function findState( token: ReadableFamilyToken, key: Key, ): ReadableToken export function findState( token: ReadableFamilyToken, key: Canonical, ): ReadableToken { const state = findInStore(IMPLICIT.STORE, token, key) return state }