UNPKG

@dvcol/svelte-utils

Version:

Svelte library for common utility functions and constants

90 lines (89 loc) 3.4 kB
export type WatchOptions = { /** * Run outside tracked scope. * @see https://svelte.dev/docs/svelte/$effect#$effect.root */ root?: boolean; /** * Run before tick * @see https://svelte.dev/docs/svelte/$effect#$effect.pre */ pre?: boolean; /** * Skip the first n runs */ skip?: number; /** * Skip while the condition is true */ until?: () => boolean; /** * Run on the next tick * @see https://svelte.dev/docs/svelte/lifecycle-hooks#tick */ next?: () => void; /** * Whether to run the logic inside the tracked scope */ tracked?: boolean; }; /** * Create a function that wraps the logic to run when the sources change. * * If change returns a callback, it will run: * * immediately before the effect re-runs * * when the component is destroyed * @see https://svelte.dev/docs/svelte/$effect * * @param change - logic to run when sources change * @param sources - getter function including tracked dependencies * @param options - watch options to control the behavior * @param options.tracked - whether to run the logic inside a tracked scope (default: false with sources, true without) */ export declare const useEffect: (change: Parameters<typeof $effect>[0], sources?: () => unknown, { tracked, ...options }?: Omit<WatchOptions, "pre" | "root">) => Parameters<typeof $effect>[0]; /** * Watch for changes in the sources and run the logic. * * Logic will run outside of the tracked scope if `sources` is provided or if `options.tracked` is explicitly false. * * If change returns a callback, it will run: * * immediately before the effect re-runs * * when the component is destroyed * @see https://svelte.dev/docs/svelte/$effect * * @param change - logic to run when sources change * @param sources - getter function including tracked dependencies * @param options - watch options * @param options.root - run outside tracked scope * @param options.pre - run before tick */ export declare function watch(change: Parameters<typeof $effect>[0], sources?: () => unknown, { root, pre, ...options }?: WatchOptions): void; /** * Watch for changes in the sources and run the logic. * * Logic will run inside of the tracked scope unless `options.tracked` is explicitly false. * * If change returns a callback, it will run: * * immediately before the effect re-runs * * when the component is destroyed * @see https://svelte.dev/docs/svelte/$effect * * @param change - logic to run when sources change * @param sources - getter function including tracked dependencies * @param options - watch options */ export declare function effect(change: Parameters<typeof $effect>[0], sources?: () => unknown, options?: WatchOptions): void; /** * Watch for differences between two getters output and run the logic if they change. * @param outer - getter function to watch * @param inner - getter function to watch * @param input - logic to run when outer changes (only if different from inner) * @param output - logic to run when inner changes (only if different from outer) * @param options - watch options */ export declare const doubleBind: <T = unknown>({ outer, inner, input, output, }: { outer: () => T; inner: () => T; input: Parameters<typeof $effect>[0]; output: Parameters<typeof $effect>[0]; }, options?: WatchOptions) => void;