@stainless-code/svelte-layers
Version:
Svelte adapter for @stainless-code/layers — call a layer like an async function and await its response.
144 lines • 8.64 kB
text/typescript
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";
import { Readable } from "svelte/store";
export type * from "@stainless-code/layers";
//#region src/store.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 WiredLayerStoreHandle<P, R, E = DefaultLayerError, D = unknown, RP = unknown> = LayerHandle$1<P, R, E, D, RP> & {
state: Readable<LayerState<P, R, E, D>[]>;
queued: Readable<LayerState<P, R, E, D>[]>;
top: Readable<LayerState<P, R, E, D> | null>;
};
type WiredValidatedLayerStoreHandle<V extends Validator$1<unknown>, R, E = DefaultLayerError, D = unknown, RP = unknown> = ValidatedLayerHandle$1<V, R, E, D, RP> & {
state: Readable<LayerState<InferValidatorOutput$1<V>, R, E, D>[]>;
queued: Readable<LayerState<InferValidatorOutput$1<V>, R, E, D>[]>;
top: Readable<LayerState<InferValidatorOutput$1<V>, R, E, D> | null>;
};
/**
* Exposes a {@link LayerClient} stack as a Svelte readable store.
*
* Use `$stack` in components to subscribe. Pair each state with {@link callFor}
* when rendering a layer.
*
* @param opts Stack id, selector, and compare options.
* @param client Optional client override; defaults to {@link useLayerClient}.
* @returns A readable store of the selected stack value.
* @default `stack` is `"default"`; `select` is identity; `compare` is
* `Object.is`.
* @example
* ```svelte
* <script>
* import {
* callFor,
* useLayerClient,
* useStack,
* } from "@stainless-code/svelte-layers/store";
*
* const client = useLayerClient();
* const stack = useStack({ stack: "confirm" });
* </script>
* {#each $stack as state (state.id)}
* {@const call = callFor(client, "confirm", state)}
* {#if call}<Confirm {call} />{/if}
* {/each}
* ```
*/
declare function useStack<T = LayerState[]>(opts?: UseStackOptions<T>, client?: LayerClient$1): Readable<T>;
/** Exposes a stack's queued snapshot as a Svelte readable store. */
declare function createQueuedStack<T = LayerState[]>(opts?: UseStackOptions<T>, client?: LayerClient$1): Readable<T>;
/** Observe all mounted layers matching a key. */
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): Readable<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): Readable<U>;
/** Wired handle: wraps core {@link createLayerHandle} with store 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): WiredValidatedLayerStoreHandle<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): WiredLayerStoreHandle<P, R, E, D, RP>;
/**
* Builds the call context for a layer state.
*
* @param client Client that owns the stack.
* @param stackId Stack containing the 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.
*/
declare function callFor<RootProps = unknown>(client: LayerClient$1, stackId: string, state: LayerState, rootProps?: RootProps): LayerCallContext<unknown, unknown, RootProps> | null;
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> {
/** Store that is `true` while a `run(...)` async action is in flight; subscribe with `$pending`. Mirrors the layer's `actionStatus: "running"`. */
pending: Readable<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/store";
*
* let { call }: LayerComponentProps<void, boolean> = $props();
* const { pending, run } = useMutationFlow(call);
* </script>
* <button disabled={$pending} onclick={() => void run(() => Promise.resolve()).orEnd(true)}>
* Confirm
* </button>
* ```
*/
declare function useMutationFlow<P, R, RootProps = unknown>(call: LayerCallContext<P, R, RootProps>): MutationFlow<R>;
interface LayerGroup {
open: ScopedOpen;
dismissAll: (response?: unknown) => void;
/** The child stack store — render `{#each $stack as s}` and pair with `callFor(client, stackId, s)`. */
stack: Readable<LayerState[]>;
stackId: string;
}
/** 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>;
}
/**
* 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;
//#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, UseLayerStateOptions, UseStackOptions, type ValidatedLayerHandle, type ValidationIssue, type Validator, WiredLayerStoreHandle, WiredValidatedLayerStoreHandle, callFor, childStackId, createCallContext, createLayer, createLayerGroup, createLayerHandle, createLayerQueuedState, createLayerState, createQueuedStack, hashKey, isPayloadValidationError, keySignature, layerKey, layerOptions, notifyManager, setLayerClient, shallowArrayEqual, useLayerClient, useLayerGroup, useMutationFlow, useStack };