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

makeManagedServerProvider.ts

75 lines3.0 KB

1import type { ServerProvider } from "@t3tools/contracts";
2import { Duration, Effect, PubSub, Ref, Scope, Stream } from "effect";
3import * as Semaphore from "effect/Semaphore";
4​
5import type { ServerProviderShape } from "./Services/ServerProvider";
6import { ServerSettingsError } from "@t3tools/contracts";
7​
8export const makeManagedServerProvider = Effect.fn("makeManagedServerProvider")(function* <
9 Settings,
10>(input: {
11 readonly getSettings: Effect.Effect<Settings>;
12 readonly streamSettings: Stream.Stream<Settings>;
13 readonly haveSettingsChanged: (previous: Settings, next: Settings) => boolean;
14 readonly initialSnapshot: (settings: Settings) => ServerProvider;
15 readonly checkProvider: Effect.Effect<ServerProvider, ServerSettingsError>;
16 readonly refreshInterval?: Duration.Input;
17}): Effect.fn.Return<ServerProviderShape, ServerSettingsError, Scope.Scope> {
18 const refreshSemaphore = yield* Semaphore.make(1);
19 const changesPubSub = yield* Effect.acquireRelease(
20 PubSub.unbounded<ServerProvider>(),
21 PubSub.shutdown,
22 );
23 const initialSettings = yield* input.getSettings;
24 const initialSnapshot = input.initialSnapshot(initialSettings);
25 const snapshotRef = yield* Ref.make(initialSnapshot);
26 const settingsRef = yield* Ref.make(initialSettings);
27​
28 const applySnapshotBase = Effect.fn("applySnapshot")(function* (
29 nextSettings: Settings,
30 options?: { readonly forceRefresh?: boolean },
31 ) {
32 const forceRefresh = options?.forceRefresh === true;
33 const previousSettings = yield* Ref.get(settingsRef);
34 if (!forceRefresh && !input.haveSettingsChanged(previousSettings, nextSettings)) {
35 yield* Ref.set(settingsRef, nextSettings);
36 return yield* Ref.get(snapshotRef);
37 }
38​
39 const nextSnapshot = yield* input.checkProvider;
40 yield* Ref.set(settingsRef, nextSettings);
41 yield* Ref.set(snapshotRef, nextSnapshot);
42 yield* PubSub.publish(changesPubSub, nextSnapshot);
43 return nextSnapshot;
44 });
45 const applySnapshot = (nextSettings: Settings, options?: { readonly forceRefresh?: boolean }) =>
46 refreshSemaphore.withPermits(1)(applySnapshotBase(nextSettings, options));
47​
48 const refreshSnapshot = Effect.fn("refreshSnapshot")(function* () {
49 const nextSettings = yield* input.getSettings;
50 return yield* applySnapshot(nextSettings, { forceRefresh: true });
51 });
52​
53 yield* Stream.runForEach(input.streamSettings, (nextSettings) =>
54 Effect.asVoid(applySnapshot(nextSettings)),
55 ).pipe(Effect.forkScoped);
56​
57 yield* Effect.forever(
58 Effect.sleep(input.refreshInterval ?? "60 seconds").pipe(
59 Effect.flatMap(() => refreshSnapshot()),
60 Effect.ignoreCause({ log: true }),
61 ),
62 ).pipe(Effect.forkScoped);
63​
64 return {
65 getSnapshot: input.getSettings.pipe(
66 Effect.flatMap(applySnapshot),
67 Effect.tapError(Effect.logError),
68 Effect.orDie,
69 ),
70 refresh: refreshSnapshot().pipe(Effect.tapError(Effect.logError), Effect.orDie),
71 get streamChanges() {
72 return Stream.fromPubSub(changesPubSub);
73 },
74 } satisfies ServerProviderShape;
75});