Open History to see the last commit
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 checkProvider: Effect.Effect<ServerProvider, ServerSettingsError>;
15 readonly refreshInterval?: Duration.Input;
16}): Effect.fn.Return<ServerProviderShape, ServerSettingsError, Scope.Scope> {
17 const refreshSemaphore = yield* Semaphore.make(1);
18 const changesPubSub = yield* Effect.acquireRelease(
19 PubSub.unbounded<ServerProvider>(),
20 PubSub.shutdown,
21 );
22 const initialSettings = yield* input.getSettings;
23 const initialSnapshot = yield* input.checkProvider;
24 const snapshotRef = yield* Ref.make(initialSnapshot);
25 const settingsRef = yield* Ref.make(initialSettings);
26
27 const applySnapshotBase = Effect.fn("applySnapshot")(function* (
28 nextSettings: Settings,
29 options?: { readonly forceRefresh?: boolean },
30 ) {
31 const forceRefresh = options?.forceRefresh === true;
32 const previousSettings = yield* Ref.get(settingsRef);
33 if (!forceRefresh && !input.haveSettingsChanged(previousSettings, nextSettings)) {
34 yield* Ref.set(settingsRef, nextSettings);
35 return yield* Ref.get(snapshotRef);
36 }
37
38 const nextSnapshot = yield* input.checkProvider;
39 yield* Ref.set(settingsRef, nextSettings);
40 yield* Ref.set(snapshotRef, nextSnapshot);
41 yield* PubSub.publish(changesPubSub, nextSnapshot);
42 return nextSnapshot;
43 });
44 const applySnapshot = (nextSettings: Settings, options?: { readonly forceRefresh?: boolean }) =>
45 refreshSemaphore.withPermits(1)(applySnapshotBase(nextSettings, options));
46
47 const refreshSnapshot = Effect.fn("refreshSnapshot")(function* () {
48 const nextSettings = yield* input.getSettings;
49 return yield* applySnapshot(nextSettings, { forceRefresh: true });
50 });
51
52 yield* Stream.runForEach(input.streamSettings, (nextSettings) =>
53 Effect.asVoid(applySnapshot(nextSettings)),
54 ).pipe(Effect.forkScoped);
55
56 yield* Effect.forever(
57 Effect.sleep(input.refreshInterval ?? "60 seconds").pipe(
58 Effect.flatMap(() => refreshSnapshot()),
59 Effect.ignoreCause({ log: true }),
60 ),
61 ).pipe(Effect.forkScoped);
62
63 return {
64 getSnapshot: input.getSettings.pipe(
65 Effect.flatMap(applySnapshot),
66 Effect.tapError(Effect.logError),
67 Effect.orDie,
68 ),
69 refresh: refreshSnapshot().pipe(Effect.tapError(Effect.logError), Effect.orDie),
70 get streamChanges() {
71 return Stream.fromPubSub(changesPubSub);
72 },
73 } satisfies ServerProviderShape;
74});