import type { Resolve, Subscription } from "./types.ts"; import { action } from "./action.ts"; /** * A FIFO queue which can be used to implement the {@link Subscription} * interface directly. Most of the time, you will use either a {@link Signal} * or a {@link Channel} as the mechanism, but `Queue` allows you to manage * a single subscription directly. * * @typeParam T the type of the items in the queue * @typeParam TClose the type of the value that the queue is closed with * @since 3.0 */ export interface Queue extends Subscription { /** * Add a value to the queue. The oldest value currently in the queue will * be the first to be read. * @param item - the value to add */ add(item: T): void; /** * Close the queue. * @param value - the queue's final value. */ close(value: TClose): void; } /** * Creates a new queue. Queues are unlimited in size and sending a message to a * queue is always synchronous. * * @example * * ```javascript * import { each, main, createQueue } from 'effection'; * * await main(function*() { * let queue = createQueue(); * queue.add(1); * queue.add(2); * queue.add(3); * * let next = yield* queue.subscription.next(); * while (!next.done) { * console.log("got number", next.value); * next = yield* queue.subscription.next(); * } * }); * ``` * * @typeParam T the type of the items in the queue * @typeParam TClose the type of the value that the queue is closed with * @since 3.0 */ export function createQueue(): Queue { type Item = IteratorResult; let items: Item[] = []; let consumers = new Set>(); function enqueue(item: Item) { items.unshift(item); while (items.length > 0 && consumers.size > 0) { let [consume] = consumers; let top = items.pop() as Item; consume(top); } } return { add: (value) => enqueue({ done: false, value }), close: (value) => enqueue({ done: true, value }), *next() { let item = items.pop(); if (item) { return item; } else { return yield* action((resolve) => { consumers.add(resolve); return () => consumers.delete(resolve); }); } }, }; }