Open History to see the last commit
1import * as Effect from "effect/Effect";
2import * as Queue from "effect/Queue";
3import * as Stream from "effect/Stream";
4
5/**
6 * A manual "the client's view looks wrong, reconcile it" signal.
7 *
8 * Snapshot-backed subscriptions (shell, threads) already reconcile on a
9 * foreground wake: they reload the HTTP snapshot and resume the stream from
10 * its sequence. That is the same work a client restart does, and until now a
11 * restart was the only way to ask for it. Callers that *observe* a
12 * disagreement — a lifecycle command whose effect never lands in the local
13 * view — can request it here instead of stranding the user.
14 *
15 * Deliberately not Effect-scoped: the callers are UI event handlers, and a
16 * request is fire-and-forget. Requests raised before the wakeups layer is
17 * built are dropped rather than buffered; there is nothing to reconcile
18 * before the first subscription exists.
19 */
20export type ResyncListener = () => void;
21
22const listeners = new Set<ResyncListener>();
23
24/** Ask every snapshot-backed subscription to reconcile against the server.
25 *
26 * One reconcile covers every thread in the environment, so callers that can
27 * fire in bursts (bulk lifecycle actions) should coalesce before calling
28 * this rather than asking once per item. */
29export function requestResync(): void {
30 for (const listener of listeners) {
31 listener();
32 }
33}
34
35/** Subscribe to resync requests. Returns an unsubscribe function. */
36export function onResyncRequested(listener: ResyncListener): () => void {
37 listeners.add(listener);
38 return () => {
39 listeners.delete(listener);
40 };
41}
42
43/** Resync requests as a `ConnectionWakeup` stream, for the wakeups layer. */
44export const resyncRequestStream: Stream.Stream<"resync-requested"> =
45 Stream.callback<"resync-requested">((queue) =>
46 Effect.acquireRelease(
47 Effect.sync(() => onResyncRequested(() => Queue.offerUnsafe(queue, "resync-requested"))),
48 (unsubscribe) => Effect.sync(unsubscribe),
49 ).pipe(Effect.asVoid),
50 );