import { createSignal } from "solid-js"; import { For, Show } from "@solidjs/web"; import preview from "../../../.storybook/preview.js"; import { createConcurrentTaskQueue, createPriorityQueue, createQueue, createTaskQueue, } from "../src/index.js"; import readme from "../README.md?raw"; import { Badge, BoolRow, Button, ButtonRow, Container, EventLog, Section, Separator, StatRow, inputStyle, } from "../../../.storybook/ui/index.js"; const meta = preview.meta({ title: "Utilities/Queue", tags: ["autodocs"], parameters: { layout: "centered", docs: { description: { component: readme, }, }, }, }); export default meta; export const FifoOrder = meta.story({ name: "FIFO order", parameters: { docs: { description: { story: "`createQueue` maintains strict insertion order: items enter at the back and leave from the front. The blue badge marks the next item to be dequeued.", }, }, }, render: () => { const [input, setInput] = createSignal(""); const { queue, first, size, isEmpty, add, remove, clear } = createQueue(); const submit = () => { const val = input().trim(); if (val) { add(val); setInput(""); } }; return (
setInput(e.currentTarget.value)} onKeyDown={e => e.key === "Enter" && submit()} placeholder="Item name…" style={{ ...inputStyle, flex: "1" }} />
0}>
{(item, i) => {item}}
); }, }); export const SortOnInsert = meta.story({ name: "Sort-on-insert", parameters: { docs: { description: { story: "`createPriorityQueue` re-sorts the internal array on every `add`. The comparator `(a, b) => a - b` keeps the smallest value at the front — the green badge is always the next to dequeue.", }, }, }, render: () => { const NUMS = [5, 2, 8, 1, 9, 3, 7, 4]; const { queue, first, isEmpty, add, remove } = createPriorityQueue((a, b) => a - b); return (
{n => ( )}
0}>
{(n, i) => {n}}
); }, }); export const SerialExecution = meta.story({ name: "One-at-a-time tasks", parameters: { docs: { description: { story: "`createTaskQueue` drains tasks sequentially — the next task starts only after the current one settles. Enqueue several tasks rapidly and watch them execute one by one.", }, }, }, render: () => { const { enqueue, size, active } = createTaskQueue(); const [log, setLog] = createSignal<{ label: string; time: string }[]>([]); const addTask = (label: string, ms: number) => { enqueue(async () => { await new Promise(r => setTimeout(r, ms)); return label; }).then(result => { setLog(prev => [{ label: result, time: new Date().toLocaleTimeString() }, ...prev].slice(0, 5), ); }); }; const TASKS = [ { label: "fast · 200ms", ms: 200 }, { label: "medium · 600ms", ms: 600 }, { label: "slow · 1.2s", ms: 1200 }, ]; return (
{t => ( )}
); }, }); export const ConcurrentSlots = meta.story({ name: "Concurrent slots", parameters: { docs: { description: { story: "`createConcurrentTaskQueue(3)` runs at most 3 tasks at once. Tasks beyond that limit queue and start as slots open. `active` is a count, not a boolean.", }, }, }, render: () => { const CONCURRENCY = 3; const { enqueue, size, active } = createConcurrentTaskQueue(CONCURRENCY); const [log, setLog] = createSignal<{ label: string; time: string }[]>([]); let counter = 0; const addBatch = (count: number) => { for (let i = 0; i < count; i++) { const id = ++counter; const ms = 600 + ((id * 300) % 900); enqueue(async () => { await new Promise(r => setTimeout(r, ms)); return `task-${id}`; }).then(result => { setLog(prev => [{ label: result, time: new Date().toLocaleTimeString() }, ...prev].slice(0, 6), ); }); } }; return (
); }, });