UNPKG

svelte-interval-rune

Version:

Svelte 5 utility for creating intervals

109 lines (107 loc) 3.42 kB
interface IntervalOptions { /** * Create the interval immediately upon initialization. * * @default false */ immediate?: boolean; } /** * A reactive interval timer that integrates with Svelte's reactivity system. * The interval runs in the background and can be paused/resumed while maintaining tick count. * * @example * ```javascript * // Static duration * const timer = new Interval(1000); * console.log(timer.current); // Gets current time and starts the interval * * // Reactive duration * let delay = $state(500); * const reactiveTimer = new Interval(() => delay); * delay = 1000; // Duration updates automatically * * // Pause and resume * timer.pause(); * timer.resume(); // Resume normally * timer.resume(true); // Resume with immediate tick * * // Check status * console.log(timer.tickCount); // Number of times interval has fired * console.log(timer.paused); // Current pause state * ``` */ declare class Interval { #private; /** @private @deprecated DO NOT USE OR YOU WILL BE FIRED */ _: { run_func: () => void; is_active: boolean; tick_count: number; increment_tick: () => void; update: (() => void) | undefined; trigger_update: () => void; force_restart: () => void; }; /** * Creates a new Interval instance. * * @param duration - The interval duration in milliseconds. Can be a number or a reactive function. */ constructor(duration: number | (() => number), options?: IntervalOptions); /** * Resumes the interval if it was paused. * * @param immediate - If true, immediately triggers a tick and resets the interval timing. */ resume(immediate?: boolean): void; /** * Pauses the interval. The interval continues running in the background but stops executing callbacks and incrementing tick count. */ pause(): void; /** * Gets the current paused state of the interval. */ get isActive(): boolean; /** * Gets the number of times the interval has fired (tick count). * This count persists across pause/resume cycles and duration changes. */ get tickCount(): number; /** * Gets the current date and time, and starts the interval if not already started. * This is the primary way to activate the interval's reactivity. */ get current(): Date; /** * Gets the current duration of the interval in milliseconds. */ get duration(): number | (() => number); set duration(value: number | (() => number)); /** * Completely stops and clears the interval. Cannot be resumed. * Use pause()/resume() if you want to temporarily stop. */ stop(): void; /** * Check if interval has been completely stopped */ get isStopped(): boolean; [Symbol.dispose](): void; } declare function sync(...intervals: Interval[]): { enable(): void; disable(): void; readonly isSynced: boolean; readonly leader: Interval; }; declare class LimitedInterval extends Interval { #private; constructor(duration: number | (() => number), maxTicks: number, options?: IntervalOptions); get isCompleted(): boolean; get remainingTicks(): number; get maxTicks(): number; reset(): void; set maxTicks(new_max: number); } export { Interval, type IntervalOptions, LimitedInterval, sync };