import { createSignal, For } from "solid-js"; import preview from "../../../.storybook/preview.js"; import { createPolled, createIntervalCounter } from "@solid-primitives/timer"; import { Stat, Container } from "../../../.storybook/ui/index.js"; const meta = preview.meta({ title: "Browser APIs/Timer", parameters: { layout: "centered" }, }); export default meta; export const CreatePolled = meta.story({ name: "Polling accessor", parameters: { docs: { description: { story: "`createPolled` executes a function on every tick and returns a reactive accessor to the latest value. The polling function receives its previous return value as `prev`, enabling accumulation patterns.", }, }, }, render: () => { const [interval, setInterval] = createSignal(1000); const timestamp = createPolled(() => Date.now(), interval); const history = createPolled( (prev: string[]) => { const t = new Date().toLocaleTimeString(); return prev.length >= 5 ? [...prev.slice(1), t] : [...prev, t]; }, interval, [] as string[], ); return (

createPolled

{timestamp()}
Last 5 ticks
{(t, i) => ( {t()} )}
Interval: {([250, 500, 1000, 2000] as const).map(ms => ( ))}
); }, }); export const CreateIntervalCounter = meta.story({ name: "Interval counter", parameters: { docs: { description: { story: "`createIntervalCounter` returns an accessor that increments on every tick. Pass a reactive accessor as the interval to change the speed at runtime.", }, }, }, render: () => { const [interval, setInterval] = createSignal(1000); const count = createIntervalCounter(interval); return (

createIntervalCounter

{count()}
Interval: {([100, 250, 500, 1000] as const).map(ms => ( ))}
); }, });