import type { Canonical } from "atom.io/foundations/canonical" import type { stringified } from "atom.io/foundations/json" import type { AsJSON, Fn, Transceiver } from "atom.io/internal" /** * A token is an object that uniquely identifies a particular state, family, timeline, or transaction. * * While they represent one of these resources, they are not the resource itself. Think of them like paper currency representing money in the bank. * * Tokens are returned from resource creation functions, such as {@link atom} and {@link transaction}. * * Tokens can be used as parameters to functions that take a token, such as {@link getState}, {@link setState}, or {@link runTransaction}. * * Tokens are fully serializable, so they can be passed between processes. */ export type AtomIOToken = | ReadableFamilyToken | ReadableToken | TimelineFamilyToken | TimelineToken | TransactionToken export type ReadableToken = | AtomToken | SelectorToken export type WritableToken = | AtomToken | WritableSelectorToken /** * States belonging to this family can be read from the store. */ export type ReadableFamilyToken = | AtomFamilyToken | SelectorFamilyToken /** * States belonging to this family can be written directly. */ export type WritableFamilyToken = | AtomFamilyToken | WritableSelectorFamilyToken export type TimelineToken = { /** The unique identifier of the timeline */ key: string /** Discriminator */ type: `timeline` /** Present if the timeline belongs to a family. */ family?: FamilyMetadata /** Never present. This is a marker that preserves the type of the managed atoms */ __M?: M } /** * A serializable reference to a family of keyed timelines. * * Use {@link findTimeline} to get a timeline-family member. */ export type TimelineFamilyToken = { /** The unique identifier of the timeline family */ key: string /** Discriminator */ type: `timeline_family` /** Never present. This is a marker that preserves the type of keys used for timelines in this family */ __K?: K /** Never present. This is a marker that preserves the type of the managed atoms */ __M?: M } export type TransactionToken = { /** The unique identifier of the transaction */ key: string /** Discriminator */ type: `transaction` /** Never present. This is a marker that preserves the type of the transaction function */ __F?: F } export type TokenType< Comparison extends | ReadableFamilyToken | ReadableToken | TransactionToken, > = Comparison extends ReadableToken< infer RepresentedValue, any, infer RepresentedError > ? RepresentedValue | RepresentedError : Comparison extends ReadableFamilyToken< infer RepresentedValue, any, infer RepresentedError > ? RepresentedValue | RepresentedError : Comparison extends TransactionToken ? TokenFn : never export type AtomToken = | MutableAtomToken ? T : never, K> | RegularAtomToken export type RegularAtomToken = { /** The unique identifier of the atom. */ key: string /** Discriminator. */ type: `atom` /** Present if the atom belongs to a family. */ family?: FamilyMetadata /** Never present. This is a marker that preserves the type of the atom's value. */ __T?: T /** Never present. This is a marker that preserves the type of errors this atom is capable of catching and setting as its value. */ __E?: E } export type MutableAtomToken< T extends Transceiver, K extends Canonical = any, > = { /** The unique identifier of the atom. */ key: string /** Discriminator. */ type: `mutable_atom` /** Present if the atom belongs to a family. */ family?: FamilyMetadata /** Never present. This is a marker that preserves the JSON form of the atom's transceiver value. */ __J?: AsJSON } export type SelectorToken = | ReadonlySelectorToken | WritableSelectorToken export type ReadonlySelectorToken = | ReadonlyHeldSelectorToken | ReadonlyPureSelectorToken export type WritableSelectorToken = | WritableHeldSelectorToken | WritablePureSelectorToken export type PureSelectorToken = | ReadonlyPureSelectorToken | WritablePureSelectorToken export type HeldSelectorToken = | ReadonlyHeldSelectorToken | WritableHeldSelectorToken export type WritablePureSelectorToken< T, K extends Canonical = any, E = never, > = { /** The unique identifier of the selector. */ key: string /** Discriminator. */ type: `writable_pure_selector` /** Present if the selector belongs to a family. */ family?: FamilyMetadata /** Never present. This is a marker that preserves the type of the selector's value. */ __T?: T /** Never present. This is a marker that preserves the type of errors this selector is capable of catching and setting as its value. */ __E?: E } export type WritableHeldSelectorToken = { /** The unique identifier of the selector. */ key: string /** Discriminator. */ type: `writable_held_selector` /** Present if the selector belongs to a family. */ family?: FamilyMetadata /** Never present. This is a marker that preserves the type of the selector's value. */ __T?: T } export type ReadonlyPureSelectorToken< T, K extends Canonical = any, E = never, > = { /** The unique identifier of the selector. */ key: string /** Discriminator. */ type: `readonly_pure_selector` /** Present if the selector belongs to a family. */ family?: FamilyMetadata /** Never present. This is a marker that preserves the type of the selector's value. */ __T?: T /** Never present. This is a marker that preserves the type of errors this selector is capable of catching and setting as its value. */ __E?: E } export type ReadonlyHeldSelectorToken = { /** The unique identifier of the selector. */ key: string /** Discriminator. */ type: `readonly_held_selector` /** Present if the selector belongs to a family. */ family?: FamilyMetadata /** Never present. This is a marker that preserves the type of the selector's value. */ __T?: T } /** * Identifies a state's connection to its family. */ export type FamilyMetadata = { /** The family's unique key. */ key: string /** The family member's unique identifier, in the form of a string. */ subKey: stringified } export type AtomFamilyToken = | MutableAtomFamilyToken ? T : never, K> | RegularAtomFamilyToken export type RegularAtomFamilyToken = { /** The unique identifier of the atom family */ key: string /** Discriminator */ type: `atom_family` /** Never present. This is a marker that preserves the type of atoms in this family */ __T?: T /** Never present. This is a marker that preserves the type of keys used for atoms in this family */ __K?: K /** Never present. This is a marker that preserves the type of errors this family is capable of catching and setting as its value. */ __E?: E } export type MutableAtomFamilyToken< T extends Transceiver, K extends Canonical, > = { /** The unique identifier of the atom family */ key: string /** Discriminator */ type: `mutable_atom_family` /** Never present. This is a marker that preserves the type of atoms in this family */ __T?: T /** Never present. This is a marker that preserves the type of keys used for atoms in this family */ __K?: K } export type SelectorFamilyToken = | ReadonlySelectorFamilyToken | WritableSelectorFamilyToken export type ReadonlySelectorFamilyToken = | ReadonlyHeldSelectorFamilyToken | ReadonlyPureSelectorFamilyToken export type WritableSelectorFamilyToken = | WritableHeldSelectorFamilyToken | WritablePureSelectorFamilyToken export type PureSelectorFamilyToken = | ReadonlyPureSelectorFamilyToken | WritablePureSelectorFamilyToken export type HeldSelectorFamilyToken = | ReadonlyHeldSelectorFamilyToken | WritableHeldSelectorFamilyToken export type WritablePureSelectorFamilyToken< T, K extends Canonical, E = never, > = { /** The unique identifier of the family */ key: string /** Discriminator */ type: `writable_pure_selector_family` /** Never present. This is a marker that preserves the type of the value of each family member */ __T?: T /** Never present. This is a marker that preserves the type of keys used for each family member */ __K?: K /** Never present. This is a marker that preserves the type of errors this family is capable of catching and setting as its value. */ __E?: E } export type ReadonlyPureSelectorFamilyToken< T, K extends Canonical, E = never, > = { /** The unique identifier of the family */ key: string /** Discriminator */ type: `readonly_pure_selector_family` /** Never present. This is a marker that preserves the type of the value of each family member */ __T?: T /** Never present. This is a marker that preserves the type of keys used for each family member */ __K?: K /** Never present. This is a marker that preserves the type of errors this family is capable of catching and setting as its value. */ __E?: E } export type WritableHeldSelectorFamilyToken = { /** The unique identifier of the family */ key: string /** Discriminator */ type: `writable_held_selector_family` /** Never present. This is a marker that preserves the type of the value of each family member */ __T?: T /** Never present. This is a marker that preserves the type of keys used for each family member */ __K?: K } export type ReadonlyHeldSelectorFamilyToken = { /** The unique identifier of the family */ key: string /** Discriminator */ type: `readonly_held_selector_family` /** Never present. This is a marker that preserves the type of the value of each family member */ __T?: T /** Never present. This is a marker that preserves the type of keys used for each family member */ __K?: K }