| 1 | import type { ServerSettings, ServerSettingsError } from "@t3tools/contracts"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Equal from "effect/Equal"; |
| 4 | import * as Stream from "effect/Stream"; |
| 5 | |
| 6 | import * as ProviderHost from "./ProviderHost.ts"; |
| 7 | |
| 8 | export interface ProviderSnapshotSettings<Settings> { |
| 9 | readonly provider: Settings; |
| 10 | readonly enableProviderUpdateChecks: boolean; |
| 11 | } |
| 12 | |
| 13 | function makeProviderSnapshotSettings<Settings>( |
| 14 | provider: Settings, |
| 15 | settings: ServerSettings, |
| 16 | ): ProviderSnapshotSettings<Settings> { |
| 17 | return { |
| 18 | provider, |
| 19 | enableProviderUpdateChecks: settings.enableProviderUpdateChecks, |
| 20 | }; |
| 21 | } |
| 22 | |
| 23 | export function haveProviderSnapshotSettingsChanged<Settings>( |
| 24 | previous: ProviderSnapshotSettings<Settings>, |
| 25 | next: ProviderSnapshotSettings<Settings>, |
| 26 | ): boolean { |
| 27 | return !Equal.equals(previous, next); |
| 28 | } |
| 29 | |
| 30 | /** The instance's settings paired with the host's server settings, as snapshots read them. */ |
| 31 | export const makeProviderSnapshotSettingsSource = <Settings>( |
| 32 | provider: Settings, |
| 33 | ): Effect.Effect< |
| 34 | { |
| 35 | readonly getSettings: Effect.Effect<ProviderSnapshotSettings<Settings>, ServerSettingsError>; |
| 36 | readonly streamSettings: Stream.Stream<ProviderSnapshotSettings<Settings>>; |
| 37 | }, |
| 38 | never, |
| 39 | ProviderHost.ProviderHost |
| 40 | > => |
| 41 | Effect.gen(function* () { |
| 42 | const { settings } = yield* ProviderHost.ProviderHost; |
| 43 | const mapSettings = (current: ServerSettings) => |
| 44 | makeProviderSnapshotSettings(provider, current); |
| 45 | return { |
| 46 | getSettings: settings.get.pipe(Effect.map(mapSettings)), |
| 47 | streamSettings: settings.changes.pipe(Stream.map(mapSettings)), |
| 48 | }; |
| 49 | }); |