|
| 1 | +import type { Resolve, Subscription } from "effection"; |
| 2 | +import { action, resource } from "effection"; |
| 3 | + |
| 4 | +export function createReplaySignal<T, TClose>() { |
| 5 | + const subscribers = new Set<Subscription<T, TClose>>(); |
| 6 | + // single shared durable queue storage |
| 7 | + const queue = createDurableQueue<T, TClose>(); |
| 8 | + |
| 9 | + // each subscriber gets its own iterator over the shared items by |
| 10 | + // calling `queue.subscribe()` which returns a Stream |
| 11 | + const subscribe = resource<Subscription<T, TClose>>(function* (provide) { |
| 12 | + const queued = queue.stream(); |
| 13 | + subscribers.add(queued); |
| 14 | + |
| 15 | + try { |
| 16 | + yield* provide({ next: queued.next }); |
| 17 | + } finally { |
| 18 | + subscribers.delete(queued); |
| 19 | + } |
| 20 | + }); |
| 21 | + |
| 22 | + function send(value: T) { |
| 23 | + queue.add(value); |
| 24 | + } |
| 25 | + |
| 26 | + function close(value?: TClose) { |
| 27 | + queue.close(value); |
| 28 | + } |
| 29 | + |
| 30 | + return { ...subscribe, send, close }; |
| 31 | +} |
| 32 | + |
| 33 | +function createDurableQueue<T, TClose = never>() { |
| 34 | + type Item = IteratorResult<T, TClose>; |
| 35 | + |
| 36 | + const items: Item[] = []; |
| 37 | + |
| 38 | + // a set of active subscribers; each subscription has its own iterator |
| 39 | + // and its own waiting notifier set |
| 40 | + const subscribers = new Set<{ |
| 41 | + notify: Set<Resolve<Item>>; |
| 42 | + }>(); |
| 43 | + |
| 44 | + function enqueue(item: Item) { |
| 45 | + items.push(item); |
| 46 | + for (const sub of subscribers) { |
| 47 | + if (sub.notify.size > 0) { |
| 48 | + const [resolve] = sub.notify; |
| 49 | + // use resolve from yield* action to notify waiting subscribers |
| 50 | + resolve(item); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + function stream(): Subscription<T, TClose> { |
| 56 | + const iter = items[Symbol.iterator](); |
| 57 | + const notify = new Set<Resolve<Item>>(); |
| 58 | + const sub = { notify }; |
| 59 | + subscribers.add(sub); |
| 60 | + |
| 61 | + return { |
| 62 | + *next() { |
| 63 | + const item = iter.next().value; |
| 64 | + // item will be `undefined` when we've iterated to the end of the |
| 65 | + // current `items` array; in that case we wait for new items to be |
| 66 | + // enqueued and the resolve will be called with the new `Item`. |
| 67 | + if (item !== undefined) { |
| 68 | + return item; |
| 69 | + } |
| 70 | + return yield* action<Item>((resolve) => { |
| 71 | + notify.add(resolve); |
| 72 | + return () => notify.delete(resolve); |
| 73 | + }); |
| 74 | + }, |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + return { |
| 79 | + add: (value: T) => enqueue({ done: false, value }), |
| 80 | + close: (value?: TClose) => enqueue({ done: true, value: value as TClose }), |
| 81 | + stream, |
| 82 | + }; |
| 83 | +} |
0 commit comments