The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Scope from "effect/Scope"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as TxQueue from "effect/TxQueue"; |
| 4 | import * as TxRef from "effect/TxRef"; |
| 5 | |
| 6 | export interface KeyedCoalescingWorker<K, V> { |
| 7 | readonly enqueue: (key: K, value: V) => Effect.Effect<void>; |
| 8 | readonly drainKey: (key: K) => Effect.Effect<void>; |
| 9 | } |
| 10 | |
| 11 | interface KeyedCoalescingWorkerState<K, V> { |
| 12 | readonly latestByKey: Map<K, V>; |
| 13 | readonly queuedKeys: Set<K>; |
| 14 | readonly activeKeys: Set<K>; |
| 15 | } |
| 16 | |
| 17 | export const makeKeyedCoalescingWorker = Effect.fn( |
| 18 | "KeyedCoalescingWorker.makeKeyedCoalescingWorker", |
| 19 | )(function* <K, V, E, R>(options: { |
| 20 | readonly merge: (current: V, next: V) => V; |
| 21 | readonly process: (key: K, value: V) => Effect.Effect<void, E, R>; |
| 22 | }): Effect.fn.Return<KeyedCoalescingWorker<K, V>, never, Scope.Scope | R> { |
| 23 | const queue = yield* Effect.acquireRelease(TxQueue.unbounded<K>(), TxQueue.shutdown); |
| 24 | const stateRef = yield* TxRef.make<KeyedCoalescingWorkerState<K, V>>({ |
| 25 | latestByKey: new Map(), |
| 26 | queuedKeys: new Set(), |
| 27 | activeKeys: new Set(), |
| 28 | }); |
| 29 | |
| 30 | const completeKey = (key: K): Effect.Effect<void> => |
| 31 | TxRef.modify(stateRef, (state) => { |
| 32 | const activeKeys = new Set(state.activeKeys); |
| 33 | activeKeys.delete(key); |
| 34 | |
| 35 | if (state.latestByKey.has(key) && !state.queuedKeys.has(key)) { |
| 36 | const queuedKeys = new Set(state.queuedKeys); |
| 37 | queuedKeys.add(key); |
| 38 | return [true, { ...state, activeKeys, queuedKeys }] as const; |
| 39 | } |
| 40 | |
| 41 | return [false, { ...state, activeKeys }] as const; |
| 42 | }).pipe( |
| 43 | Effect.flatMap((shouldRequeue) => (shouldRequeue ? TxQueue.offer(queue, key) : Effect.void)), |
| 44 | Effect.tx, |
| 45 | Effect.asVoid, |
| 46 | ); |
| 47 | |
| 48 | yield* TxQueue.take(queue).pipe( |
| 49 | Effect.flatMap((key) => |
| 50 | TxRef.modify(stateRef, (state) => { |
| 51 | const queuedKeys = new Set(state.queuedKeys); |
| 52 | queuedKeys.delete(key); |
| 53 | |
| 54 | const value = state.latestByKey.get(key); |
| 55 | if (value === undefined) { |
| 56 | return [null, { ...state, queuedKeys }] as const; |
| 57 | } |
| 58 | |
| 59 | const latestByKey = new Map(state.latestByKey); |
| 60 | latestByKey.delete(key); |
| 61 | const activeKeys = new Set(state.activeKeys); |
| 62 | activeKeys.add(key); |
| 63 | |
| 64 | return [ |
| 65 | { key, value } as const, |
| 66 | { ...state, latestByKey, queuedKeys, activeKeys }, |
| 67 | ] as const; |
| 68 | }).pipe(Effect.tx), |
| 69 | ), |
| 70 | Effect.flatMap((item) => |
| 71 | item === null |
| 72 | ? Effect.void |
| 73 | : Effect.suspend(() => options.process(item.key, item.value)).pipe( |
| 74 | Effect.ignoreCause, |
| 75 | Effect.ensuring(completeKey(item.key)), |
| 76 | ), |
| 77 | ), |
| 78 | Effect.forever, |
| 79 | Effect.forkScoped, |
| 80 | ); |
| 81 | |
| 82 | const enqueue: KeyedCoalescingWorker<K, V>["enqueue"] = (key, value) => |
| 83 | TxRef.modify(stateRef, (state) => { |
| 84 | const latestByKey = new Map(state.latestByKey); |
| 85 | const existing = latestByKey.get(key); |
| 86 | latestByKey.set(key, existing === undefined ? value : options.merge(existing, value)); |
| 87 | |
| 88 | if (state.queuedKeys.has(key) || state.activeKeys.has(key)) { |
| 89 | return [false, { ...state, latestByKey }] as const; |
| 90 | } |
| 91 | |
| 92 | const queuedKeys = new Set(state.queuedKeys); |
| 93 | queuedKeys.add(key); |
| 94 | return [true, { ...state, latestByKey, queuedKeys }] as const; |
| 95 | }).pipe( |
| 96 | Effect.flatMap((shouldOffer) => (shouldOffer ? TxQueue.offer(queue, key) : Effect.void)), |
| 97 | Effect.tx, |
| 98 | Effect.asVoid, |
| 99 | ); |
| 100 | |
| 101 | const drainKey: KeyedCoalescingWorker<K, V>["drainKey"] = (key) => |
| 102 | TxRef.get(stateRef).pipe( |
| 103 | Effect.tap((state) => |
| 104 | state.latestByKey.has(key) || state.queuedKeys.has(key) || state.activeKeys.has(key) |
| 105 | ? Effect.txRetry |
| 106 | : Effect.void, |
| 107 | ), |
| 108 | Effect.asVoid, |
| 109 | Effect.tx, |
| 110 | ); |
| 111 | |
| 112 | return { enqueue, drainKey } satisfies KeyedCoalescingWorker<K, V>; |
| 113 | }); |