import type { Signal } from "../reactor-core/index.ts" import { signal } from "../reactor-core/index.ts" export interface TimerOptions { delay: number interval: number initialValue?: number step?: number } /** * @description Creates a signal that starts emitting values after a specified delay, * and continues to emit incremented values at specified intervals. * * The value starts at 0, becomes 1 after the delay, and increments by 1. */ export const timer = (options: TimerOptions): Signal => { const { delay, interval, initialValue = 0, step = 1 } = options const result = signal(() => initialValue, { onDispose: () => { clearTimeout(timeoutId) clearInterval(intervalId) }, }) let intervalId: ReturnType | undefined = undefined const timeoutId = setTimeout(() => { result.set(result.getWithoutTrack() + step) intervalId = setInterval(() => { result.set(result.getWithoutTrack() + step) }, interval) }, delay) return result } export interface IntervalOptions { interval: number initialValue?: number step?: number } /** * @description Creates a signal that increments its value at specified intervals. * * The value starts at 0 and increments by 1 every interval. */ export const interval = (options: IntervalOptions): Signal => { const { interval, initialValue = 0, step = 1 } = options const result = signal(() => initialValue, { onDispose: () => { clearInterval(intervalId) }, }) const intervalId = setInterval(() => { result.set(result.getWithoutTrack() + step) }, interval) return result }