import { t as $store } from "./store-CLLtJRzY.js"; import { Component, createMemo as $memo } from "@fictjs/runtime"; //#region src/resource.d.ts /** * @fileoverview Async data fetching with caching and Suspense support. * * The `resource` function creates a reactive data fetcher that: * - Cancels no-cache requests when their last owner leaves * - Supports Suspense for loading states * - Provides caching with TTL and stale-while-revalidate * - Handles errors gracefully */ /** * The result of reading a resource. * * @typeParam T - The type of data returned by the fetcher */ interface ResourceResult { /** The fetched data, or undefined if not yet loaded or on error */ readonly data: T | undefined; /** Whether the resource is currently loading (initial fetch or refetch) */ readonly loading: boolean; /** * Any error that occurred during fetching. * Type is unknown since errors can be any value in JavaScript. */ readonly error: unknown; /** Manually trigger a refetch of the resource */ refresh: () => void; } /** * Cache configuration options for a resource. */ interface ResourceCacheOptions { /** * Caching mode: * - `'memory'`: Cache responses in memory (default) * - `'none'`: No persistent cache; active readers and Suspense retries share work * @default 'memory' */ mode?: 'memory' | 'none'; /** * Memory-cache scope during server-side rendering: * - `'request'`: Isolate entries to the active SSR request (default) * - `'shared'`: Share entries across SSR requests for explicitly public data * * In the browser, memory caching remains scoped to the resource instance. * @default 'request' */ scope?: 'request' | 'shared'; /** * Time-to-live in milliseconds before cached data is considered stale. * @default Infinity */ ttlMs?: number; /** * If true, return stale cached data immediately while refetching in background. * @default false */ staleWhileRevalidate?: boolean; /** * If true, cache error responses as well. * @default false */ cacheErrors?: boolean; /** * Maximum number of cached entries. Least-recently-used entries beyond * this bound are evicted (in-flight entries are never evicted). * @default 256 */ maxEntries?: number; } /** * Configuration options for creating a resource. * * @typeParam T - The type of data returned by the fetcher * @typeParam Args - The type of arguments passed to the fetcher */ interface ResourceOptions { /** * Custom cache key. Can be a static value or a function that computes * the key from the args. If not provided, args are used as the key. */ key?: unknown | ((args: Args) => unknown); /** * The fetcher function that performs the async data retrieval. * Receives an AbortController signal for cancellation support. */ fetch: (ctx: { signal: AbortSignal; }, args: Args) => Promise; /** * If true, the resource will throw a Suspense token while loading, * enabling React-like Suspense boundaries. * @default false */ suspense?: boolean; /** * Cache configuration options. */ cache?: ResourceCacheOptions; /** * A value or explicitly marked reactive getter that, when changed, resets the resource. * Useful for clearing cache when certain conditions change. */ reset?: unknown | (() => unknown); } /** * Return type of the resource factory. * * @typeParam T - The type of data returned by the fetcher * @typeParam Args - The type of arguments passed to the fetcher */ interface Resource { /** * Read the resource data, triggering a fetch if needed. * Can accept static args or an explicitly marked reactive getter. * * @param argsAccessor - Arguments or an explicitly marked getter returning arguments */ read(argsAccessor: (() => Args) | Args): ResourceResult; /** * Invalidate cached data, causing the next read to refetch. * * @param key - Optional specific key to invalidate. If omitted, invalidates all. */ invalidate(key?: unknown): void; /** * Prefetch data without reading it. Useful for eager loading. * * @param args - Arguments to pass to the fetcher * @param keyOverride - Optional cache key override */ prefetch(args: Args, keyOverride?: unknown): void; /** * Optimistically update cached data for a given args/key. * * @param argsAccessor - Arguments or an explicitly marked getter returning arguments * @param value - New value or updater function * @param options - Optional settings (key override, revalidate) */ mutate(argsAccessor: (() => Args) | Args, value: T | ((prev: T | undefined) => T), options?: { key?: unknown; revalidate?: boolean; }): void; } /** * Create a reactive async data resource. * * Resources handle async data fetching with automatic caching, cancellation, * and optional Suspense integration. * * @param optionsOrFetcher - A fetcher function or full configuration object * @returns A resource factory with read, invalidate, and prefetch methods * * @example * ```tsx * import { reactive } from 'fict/advanced' * import { resource } from 'fict/plus' * * // Simple fetcher * const userResource = resource( * ({ signal }, userId: string) => * fetch(`/api/users/${userId}`, { signal }).then(r => r.json()) * ) * * // With full options * const postsResource = resource({ * fetch: ({ signal }, userId: string) => * fetch(`/api/users/${userId}/posts`, { signal }).then(r => r.json()), * suspense: true, * cache: { * ttlMs: 60_000, * staleWhileRevalidate: true, * }, * }) * * // Usage in component * function UserProfile({ userId }: { userId: string }) { * const { data, loading, error, refresh } = userResource.read(reactive(() => userId)) * * if (loading) return * if (error) return * return
{data.name}
* } * ``` * * @public */ declare function resource(optionsOrFetcher: ((ctx: { signal: AbortSignal; }, args: Args) => Promise) | ResourceOptions): Resource; //#endregion //#region src/lazy.d.ts /** Module shape produced by a typical dynamic import. */ interface LazyModule> { default: Component; } /** Options for lazy loading behavior. */ interface LazyOptions { /** Maximum retry attempts after the initial load. Defaults to `0`. */ maxRetries?: number; /** Initial exponential-backoff delay in milliseconds. Defaults to `1000`. */ retryDelay?: number; } /** Component with explicit preload and retry-reset controls. */ interface LazyComponent> extends Component { /** Clear a cached failure so the next render or preload starts a new load. */ reset: () => void; /** Load the component without rendering it. */ preload: () => Promise; } /** * Create a lazy component that suspends while loading. * * The loader may resolve to a dynamic-import module or directly to a * component. Failed loads stay observable until `reset()` is called; optional * automatic retries use exponential backoff. * * @example * ```tsx * const LazyDashboard = lazy(() => import('./Dashboard'), { * maxRetries: 2, * retryDelay: 250, * }) * ``` * * @public */ declare function lazy = Record>(loader: () => Promise | Component>, options?: LazyOptions): LazyComponent; //#endregion export { $memo, $store, type LazyComponent, type LazyModule, type LazyOptions, type ResourceCacheOptions, type ResourceOptions, type ResourceResult, lazy, resource }; //# sourceMappingURL=plus.d.ts.map