Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProviderInstanceRegistry.ts

84 lines3.9 KB

1/**
2 * ProviderInstanceRegistry — the single Effect service in the new model.
3 *
4 * Owns a `Map<ProviderInstanceId, ProviderInstance>` produced by running
5 * registered driver factories against `ServerSettings.providerInstances`.
6 * The registry watches settings; when an instance's config changes (or
7 * the entry disappears), the registry tears down the affected instance's
8 * scope and rebuilds — that's the entire hot-reload story.
9 *
10 * What rest-of-server reads from here:
11 * - `getInstance(instanceId)` — for routing turn/session calls.
12 * - `listInstances` — for snapshot aggregation in `ProviderRegistry`.
13 * - `listUnavailable` — `ServerProvider` shadows for instances whose
14 * driver is not registered in this build (rollback / fork tolerance).
15 * - `streamChanges` — coalesced "registry mutated" pings so consumers
16 * can re-pull lists or re-broadcast.
17 *
18 * @module provider/Services/ProviderInstanceRegistry
19 */
20import type { ProviderInstanceId, ServerProvider } from "@t3tools/contracts";
21import { Context } from "effect";
22import type { Effect, PubSub, Scope, Stream } from "effect";
23​
24import type { ProviderInstance } from "../ProviderDriver.ts";
25​
26export interface ProviderInstanceRegistryShape {
27 /**
28 * Look up one instance by id. Returns `undefined` (not Option) when the
29 * id is unknown — callers branch on falsy and emit
30 * `ProviderInstanceNotFoundError`.
31 */
32 readonly getInstance: (
33 instanceId: ProviderInstanceId,
34 ) => Effect.Effect<ProviderInstance | undefined>;
35 /**
36 * Every available (driver-registered, successfully created) instance,
37 * in stable settings-author order.
38 */
39 readonly listInstances: Effect.Effect<ReadonlyArray<ProviderInstance>>;
40 /**
41 * Wire-shape shadow snapshots for instances whose driver is unknown to
42 * this build (or whose config failed to decode). Suitable for merging
43 * directly into `ProviderRegistry` output.
44 */
45 readonly listUnavailable: Effect.Effect<ReadonlyArray<ServerProvider>>;
46 /**
47 * Push notification stream emitted whenever the registry's contents
48 * change — instance added, removed, or rebuilt. The payload is `void`
49 * because consumers always want to re-pull `listInstances` /
50 * `listUnavailable` together.
51 *
52 * NOTE: because `Stream.fromPubSub` defers `PubSub.subscribe` until the
53 * stream starts running, forking a consumer via
54 * `Stream.runForEach(...).pipe(Effect.forkScoped)` races the next
55 * publish — the forked fiber may not have subscribed yet when the
56 * publish lands. Hot-reload consumers that must not miss a publish
57 * should use `subscribeChanges` below instead, which acquires the
58 * subscription synchronously in the caller's fiber before the consumer
59 * loop is forked.
60 */
61 readonly streamChanges: Stream.Stream<void>;
62 /**
63 * Acquire a subscription to the registry's change channel synchronously
64 * in the caller's fiber. Returns a `PubSub.Subscription<void>` whose
65 * lifetime is scoped to the provided `Scope` (the subscription is
66 * released when the scope closes). Consumers typically `yield*` this
67 * in the same fiber that forks their consumer loop, then drain with
68 * `PubSub.take(subscription)` inside `Effect.forever`. Because the
69 * subscription is registered with the PubSub before this `yield*`
70 * returns, no subsequent publish can land in a gap.
71 *
72 * This exists because the `ProviderInstanceRegistry` publishes on a
73 * PubSub and `Stream.fromPubSub` defers subscription until the stream
74 * starts executing — a consumer that `forkScoped`s the stream
75 * consumption can miss a publish that lands in the narrow window
76 * between "fiber scheduled" and "fiber starts running".
77 */
78 readonly subscribeChanges: Effect.Effect<PubSub.Subscription<void>, never, Scope.Scope>;
80​
81export class ProviderInstanceRegistry extends Context.Service<
82 ProviderInstanceRegistry,
83 ProviderInstanceRegistryShape
84>()("t3/provider/Services/ProviderInstanceRegistry") {}