Fork Horse repository, listed by its owner. Publisher snapshot.

packages/forkhorse-gatekeeper/test/fixture-ui.ts

Snapshot at 34e3175

Containing directory

1,224 bytes · typescriptView raw
1import { RpcTarget, type RpcStub } from "capnweb";
2import { SnapshotFeed } from "../src/feed.ts";
3import type { UpdateReceiver } from "../src/ui-types.ts";
4
5/** Fixture-only RPC endpoint; no account connection, core calls, credentials or Git transport. */
6export class FixtureUi extends RpcTarget {
7  readonly #feed: SnapshotFeed;
8  readonly #push: boolean;
9  calls = { snapshot: 0, subscribe: 0, poll: 0 };
10  constructor(feed: SnapshotFeed, push = true) { super(); this.#feed = feed; this.#push = push; }
11  async snapshot() { this.calls.snapshot++; return this.#feed.snapshot(); }
12  async subscribe(receiver: RpcStub<UpdateReceiver>) {
13    this.calls.subscribe++;
14    if (!this.#push) throw new Error("Fixture: push unavailable.");
15    const retained = receiver.dup();
16    let disposed = false;
17    const close = this.#feed.subscribe({
18      receive: (batch) => retained.receive(batch),
19      dispose() {
20        if (!disposed) { disposed = true; retained[Symbol.dispose](); }
21      },
22    });
23    return new class extends RpcTarget {
24      async close() { close(); }
25      [Symbol.dispose]() { close(); }
26    }();
27  }
28  async waitForChanges(after: number) { this.calls.poll++; return this.#feed.waitForChanges(after); }
29}
30