UNPKG

@stainless-code/svelte-layers

Version:

Svelte adapter for @stainless-code/layers — call a layer like an async function and await its response.

154 lines 8.69 kB
import { ControlledPromise, DataTag, DataTag as DataTag$1, DefaultLayerError, ErrorOf, ErrorOf as ErrorOf$1, InferDataTagError, InferDataTagResponse, InferValidatorInput, InferValidatorOutput, InferValidatorOutput as InferValidatorOutput$1, Layer, LayerCallContext, LayerClient, LayerClient as LayerClient$1, LayerComponentProps, LayerGroupHandle, LayerGroupOptions, LayerGroupOptions as LayerGroupOptions$1, LayerHandle, LayerHandle as LayerHandle$1, LayerKey, LayerOptions, LayerStack, LayerState, OmitKeyof, OpenLayerOptions, OpenValidatePayload, PayloadValidationError, Reject, Resolve, ResponseOf, ResponseOf as ResponseOf$1, StandardSchemaV1, Subscribable, ValidatedLayerHandle, ValidatedLayerHandle as ValidatedLayerHandle$1, ValidationIssue, Validator, Validator as Validator$1, childStackId, createCallContext, createLayer as createLayerHandle, createLayerGroup, hashKey, isPayloadValidationError, keySignature, layerKey, layerOptions, notifyManager, shallowArrayEqual } from "@stainless-code/layers"; export type * from "@stainless-code/layers"; //#region src/index.d.ts /** * Provides a {@link LayerClient} to descendant components. * * @param client Client to provide. A new client is created when omitted. * @returns The provided client. */ declare function setLayerClient(client?: LayerClient$1): LayerClient$1; /** * Reads the nearest {@link LayerClient} from Svelte context. * * @returns The nearest provided client. */ declare function useLayerClient(): LayerClient$1; type NoValidateOptions<Opts> = Opts extends { validate: Validator$1<unknown>; } ? never : Opts; interface UseStackOptions<T = LayerState[]> { stack?: string; select?: (states: LayerState[]) => T; compare?: (a: T, b: T) => boolean; } interface UseLayerStateOptions<Key extends LayerKey, P = unknown, D = unknown, U = LayerState<P, ResponseOf$1<Key>, ErrorOf$1<Key>, D>[]> { key: Key; stack?: string; select?: (states: LayerState<P, ResponseOf$1<Key>, ErrorOf$1<Key>, D>[]) => U; compare?: (a: U, b: U) => boolean; } type WiredLayerHandle<P, R, E = DefaultLayerError, D = unknown, RP = unknown> = LayerHandle$1<P, R, E, D, RP> & { readonly state: LayerState<P, R, E, D>[]; readonly queued: LayerState<P, R, E, D>[]; readonly top: LayerState<P, R, E, D> | null; }; type WiredValidatedLayerHandle<V extends Validator$1<unknown>, R, E = DefaultLayerError, D = unknown, RP = unknown> = ValidatedLayerHandle$1<V, R, E, D, RP> & { readonly state: LayerState<InferValidatorOutput$1<V>, R, E, D>[]; readonly queued: LayerState<InferValidatorOutput$1<V>, R, E, D>[]; readonly top: LayerState<InferValidatorOutput$1<V>, R, E, D> | null; }; /** Exposes a stack through Svelte 5 runes reactivity. */ interface SvelteStack<RootProps = unknown, T = LayerState[]> { /** * The selected stack snapshot. * * Read it inside `$derived`, `$effect`, `{#if}`, or `{#each}` to subscribe. */ readonly current: T; /** * Builds the call context for a layer state. * * @param state State whose layer should receive the call. * @param rootProps Props supplied by the layer host. * @returns The call context, or `null` if the layer is no longer mounted. */ callFor(state: LayerState, rootProps?: RootProps): LayerCallContext<unknown, unknown, RootProps> | null; } /** * Exposes a {@link LayerClient} stack through Svelte 5 runes reactivity. * * @param opts Stack id, selector, and compare options. * @param client Optional client override; defaults to {@link useLayerClient}. * @returns A reactive stack accessor. * @default `stack` is `"default"`; `select` is identity; `compare` is * `Object.is`. * @example * ```svelte * <script> * import { useStack } from "@stainless-code/svelte-layers"; * * const stack = useStack({ stack: "confirm" }); * </script> * {#each stack.current as state (state.id)} * {@const call = stack.callFor(state)} * {#if call}<Confirm {call} />{/if} * {/each} * ``` */ declare function useStack<RootProps = unknown, T = LayerState[]>(opts?: UseStackOptions<T>, client?: LayerClient$1): SvelteStack<RootProps, T>; /** * Exposes a stack's queued snapshot through Svelte 5 runes reactivity. */ declare function createQueuedStack<RootProps = unknown, T = LayerState[]>(opts?: UseStackOptions<T>, client?: LayerClient$1): SvelteStack<RootProps, T>; /** * Observe all mounted layers matching a key. * * A {@link DataTag} key infers its response and error types. */ declare function createLayerState<Key extends LayerKey, P = unknown, D = unknown, U = LayerState<P, ResponseOf$1<Key>, ErrorOf$1<Key>, D>[]>(opts: UseLayerStateOptions<Key, P, D, U>, client?: LayerClient$1): SvelteStack<unknown, U>; /** Observe all queued layers matching a key. */ declare function createLayerQueuedState<Key extends LayerKey, P = unknown, D = unknown, U = LayerState<P, ResponseOf$1<Key>, ErrorOf$1<Key>, D>[]>(opts: UseLayerStateOptions<Key, P, D, U>, client?: LayerClient$1): SvelteStack<unknown, U>; /** Wired handle: wraps core {@link createLayerHandle} with runes reactivity. */ declare function createLayer<V extends Validator$1<unknown>, R, E = DefaultLayerError, D = unknown, RP = unknown>(options: LayerOptions<InferValidatorOutput$1<V>, R, E, D, RP> & { key: LayerKey; validate: V; }, client?: LayerClient$1): WiredValidatedLayerHandle<V, R, E, D, RP>; declare function createLayer<P, R, E = DefaultLayerError, D = unknown, RP = unknown>(options: NoValidateOptions<LayerOptions<P, R, E, D, RP> & { key: LayerKey; }>, client?: LayerClient$1): WiredLayerHandle<P, R, E, D, RP>; interface MutationRun<R> { /** On success, end the layer with `response`; on failure, leave it open and rethrow. */ orEnd: (response: R) => Promise<void>; } interface MutationFlow<R> { /** * True while a `run(...)` async action is in flight. Read it in markup or * `$derived` to subscribe. Mirrors the layer's `actionStatus: "running"`. */ readonly pending: boolean; run: (fn: () => Promise<void> | void) => MutationRun<R>; } /** * Coordinate a layer's pending state with an async mutation and end it on success. * * @example * ```svelte * <script lang="ts"> * import { type LayerComponentProps, useMutationFlow } from "@stainless-code/svelte-layers"; * * let { call }: LayerComponentProps<void, boolean> = $props(); * const flow = useMutationFlow(call); * </script> * <button * disabled={flow.pending} * onclick={() => void flow.run(() => Promise.resolve()).orEnd(true)} * > * Confirm * </button> * ``` */ declare function useMutationFlow<P, R, RootProps = unknown>(call: LayerCallContext<P, R, RootProps>): MutationFlow<R>; /** Open a layer on a pre-bound stack, with {@link DataTag} response and error inference. */ interface ScopedOpen { <P, R, E = DefaultLayerError, D = unknown, RootProps = unknown>(options: OmitKeyof<OpenLayerOptions<P, R, E, D, RootProps> & { key: DataTag$1<LayerKey, R, E>; }, "stack" | "validate">): Promise<R>; <P, R = void, E = DefaultLayerError, D = unknown, RootProps = unknown>(options: OmitKeyof<OpenLayerOptions<P, R, E, D, RootProps>, "stack">): Promise<R>; } interface LayerGroup<RootProps = unknown> { open: ScopedOpen; dismissAll: (response?: unknown) => void; /** The child stack — render with `{#each group.stack.current as s}` + `group.stack.callFor(s)`. */ stack: SvelteStack<RootProps>; stackId: string; } /** * Create a child stack scoped to the calling layer's lifetime. * * The child stack is disposed and cleared via `cancelAll` when its parent * layer unmounts (`LayerCancelledError`). */ declare function useLayerGroup<P, R, RootProps = unknown>(call: LayerCallContext<P, R, RootProps>, options?: LayerGroupOptions$1): LayerGroup<RootProps>; //#endregion export { ControlledPromise, type DataTag, type ErrorOf, type InferDataTagError, type InferDataTagResponse, type InferValidatorInput, type InferValidatorOutput, Layer, LayerClient, type LayerComponentProps, LayerGroup, type LayerGroupHandle, type LayerGroupOptions, type LayerHandle, LayerStack, MutationFlow, MutationRun, type OpenValidatePayload, PayloadValidationError, type Reject, type Resolve, type ResponseOf, ScopedOpen, type StandardSchemaV1, Subscribable, SvelteStack, UseLayerStateOptions, UseStackOptions, type ValidatedLayerHandle, type ValidationIssue, type Validator, WiredLayerHandle, WiredValidatedLayerHandle, childStackId, createCallContext, createLayer, createLayerGroup, createLayerHandle, createLayerQueuedState, createLayerState, createQueuedStack, hashKey, isPayloadValidationError, keySignature, layerKey, layerOptions, notifyManager, setLayerClient, shallowArrayEqual, useLayerClient, useLayerGroup, useMutationFlow, useStack };