UNPKG

@daimo/pay

Version:

Seamless crypto payments. Onboard users from any chain, any coin into your app with one click.

67 lines (66 loc) 2.67 kB
/** * A store that manages state and dispatches events to a list of subscribers. * - getState: Returns the current state of the store. * - dispatch: Transition to a new state by applying the reducer function to * the current state and the event. * - subscribe: Takes a function as an argument and adds it to the list of * subscribers. When the state changes, all subscribers will be called. The * function returns a function that can be used to unsubscribe from the store. * * @template S The type of the state. * @template E The type of the events. */ export interface Store<S, E> { getState: () => S; dispatch: (e: E) => void; subscribe: (l: listener<S, E>) => () => void; } type listener<S, E> = ({ prev, next, event, }: { prev: S; next: S; event: E; }) => void; /** * Creates a store that manages state and dispatches events to a list of * subscribers. * * @param reducer The reducer function that takes the current state and an event * and deterministically returns a new state. * @param init The initial state of the store. * @returns An object with three methods: * - getState: Returns the current state of the store. * - dispatch: Transition to a new state by applying the reducer function to * the current state and the event. * - subscribe: Takes a function as an argument and adds it to the list of * subscribers. When the state changes, all subscribers will be called. The * function returns a function that can be used to unsubscribe from the store. */ export declare function createStore<S, E>(reducer: (s: S, e: E) => S, init: S): Store<S, E>; /** * Wait until the store enters any state that satisfies the given predicate(s), * or reject on error. * * @returns * `Promise<T>` resolving with the first state where `predicate` is true, * or rejecting if `isError` ever returns true. * * @example After hydrating an order, wait for the payment FSM to transition to * the new state before returning the updated order. * * // 1. Kick off the preview transition * store.dispatch({ type: "hydrate_order" }); * * // 2. Pause until the order is successfully hydrated or errors * const newState = await waitForState( * store, * (s): s is NewState => s.type === "payment_unpaid", * s => s.type === "error", * s => (s as ErrorState).message * ); * * // 3. Now newState.order is updated with the hydrated order * return newState.order; * ``` */ export declare function waitForState<S, E, T extends S>(store: Store<S, E>, predicates: ((s: S) => s is T) | ReadonlyArray<(s: S) => s is T>, isError?: (s: S) => boolean, getErrorMessage?: (s: S) => string): Promise<T>; export {};