UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

46 lines (45 loc) 2.47 kB
import { DeferredSequence } from "../sequence/DeferredSequence.js"; import { NONE } from "../util/constants.js"; import { type PossibleStarter } from "../util/start.js"; /** Any `Store` instance. */ export type AnyStore = Store<any>; /** * Store that retains its most recent value and is async-iterable to allow values to be observed. * - Current value can be read at `store.value` and `store.data` * - Stores also send their most-recent value to any new subscribers immediately when a new subscriber is added. * - Stores can also be in a loading store where they do not have a current value. * * @param initial The initial value for the store, a `Promise` that resolves to the initial value, a source `Subscribable` to subscribe to, or another `Store` instance to take the initial value from and subscribe to. * - To set the store to be loading, use the `NONE` constant or a `Promise` value. * - To set the store to an explicit value, use that value or another `Store` instance with a value. * */ export declare class Store<T> implements AsyncIterable<T> { /** Deferred sequence this store uses to issue values as they change. */ readonly next: DeferredSequence<T>; /** Current value of the store (or throw a promise that resolves when this store receives its next value or error). */ get value(): T; set value(value: T); private _value; /** Is there a current value, or is it still loading. */ get loading(): boolean; /** Time (in milliseconds) this store was last updated with a new value, or `undefined` if this store is currently loading. */ get time(): number | undefined; private _time; /** How old this store's value is (in milliseconds). */ get age(): number; /** Current error of this store (or `undefined` if there is no reason). */ get reason(): unknown; set reason(reason: unknown); private _reason; /** Set a starter for this store to allow a function to execute when this store has subscribers or not. */ set starter(start: PossibleStarter<[this]>); private _starter; /** Store is initiated with an initial store. */ constructor(value: T | typeof NONE, time?: number); /** Set the value of the store. */ set(next: T): void; /** Set the value of the store as values are pulled from a sequence. */ through(sequence: AsyncIterable<T>): AsyncIterable<T>; [Symbol.asyncIterator](): AsyncGenerator<T, void, void>; private _iterating; }