Download packages/session-ui/src/components/markdown-worker-queue.test.ts from SaylorTwift/opencode: direct link, hf CLI and curl.
- Browser
- Download file 1.56 kB
-
https://huggingface.co/SaylorTwift/opencode/resolve/main/packages/session-ui/src/components/markdown-worker-queue.test.ts
- Command line
-
hf download hf://SaylorTwift/opencode/packages/session-ui/src/components/markdown-worker-queue.test.ts
-
curl -L -o markdown-worker-queue.test.ts https://huggingface.co/SaylorTwift/opencode/resolve/main/packages/session-ui/src/components/markdown-worker-queue.test.ts
1.56 kB
| import { expect, test } from "bun:test" | |
| import { createLatestWorkerQueue } from "./markdown-worker-queue" | |
| test("keeps only the latest queued request for each key", async () => { | |
| const processed: number[] = [] | |
| const superseded: number[] = [] | |
| let release = () => {} | |
| const blocked = new Promise<void>((resolve) => { | |
| release = resolve | |
| }) | |
| const queue = createLatestWorkerQueue<{ id: number; key: string }>({ | |
| run: async (request) => { | |
| processed.push(request.id) | |
| if (request.id === 1) await blocked | |
| }, | |
| supersede: (request) => superseded.push(request.id), | |
| dispose: () => {}, | |
| }) | |
| queue.highlight({ id: 1, key: "code" }) | |
| await Promise.resolve() | |
| queue.highlight({ id: 2, key: "code" }) | |
| queue.highlight({ id: 3, key: "code" }) | |
| queue.highlight({ id: 4, key: "code" }) | |
| expect(queue.pending()).toBe(1) | |
| expect(superseded).toEqual([2, 3]) | |
| release() | |
| await queue.idle() | |
| expect(processed).toEqual([1, 4]) | |
| }) | |
| test("serializes disposal before a later request for the same key", async () => { | |
| const events: string[] = [] | |
| const queue = createLatestWorkerQueue<{ id: number; key: string }>({ | |
| run: async (request) => { | |
| events.push(`highlight:${request.id}`) | |
| }, | |
| supersede: (request) => events.push(`supersede:${request.id}`), | |
| dispose: (key) => events.push(`dispose:${key}`), | |
| }) | |
| queue.highlight({ id: 1, key: "code" }) | |
| queue.dispose("code") | |
| queue.highlight({ id: 2, key: "code" }) | |
| await queue.idle() | |
| expect(events).toEqual(["supersede:1", "dispose:code", "highlight:2"]) | |
| }) | |