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

liveActivityPreferences.ts

73 lines2.3 KB

1import * as Effect from "effect/Effect";
2​
3import type { SavedRemoteConnection } from "../../lib/connection";
4import { linkEnvironmentToCloudWithPreference } from "../cloud/linkEnvironment";
5import { updateAgentAwarenessRegistrationPreferences } from "./remoteRegistration";
6​
7export const setLiveActivityUpdatesEnabled = Effect.fn("setLiveActivityUpdatesEnabled")(
8 function* (input: {
9 readonly enabled: boolean;
10 readonly previousEnabled: boolean;
11 readonly clerkToken: string | null;
12 readonly connections: ReadonlyArray<SavedRemoteConnection>;
13 }) {
14 const linkedConnections = input.connections.filter(
15 (connection) => connection.bearerToken !== null,
16 );
17​
18 const updateRelayPreference = Effect.fn("updateRelayPreference")(function* (enabled: boolean) {
19 yield* updateAgentAwarenessRegistrationPreferences({
20 liveActivitiesEnabled: enabled,
21 });
22​
23 const clerkToken = input.clerkToken;
24 if (!clerkToken) return;
25​
26 yield* Effect.forEach(
27 linkedConnections,
28 (connection) =>
29 linkEnvironmentToCloudWithPreference({
30 clerkToken,
31 connection,
32 liveActivitiesEnabled: enabled,
33 }),
34 { concurrency: "unbounded" },
35 );
36 });
37​
38 const restoreRelayPreference = Effect.fn("restoreRelayPreference")(function* () {
39 yield* updateAgentAwarenessRegistrationPreferences({
40 liveActivitiesEnabled: input.previousEnabled,
41 }).pipe(
42 Effect.catchCause((cause) =>
43 Effect.logWarning("Could not restore Live Activity device preference.", cause),
44 ),
45 );
46​
47 const clerkToken = input.clerkToken;
48 if (!clerkToken) return;
49​
50 yield* Effect.forEach(
51 linkedConnections,
52 (connection) =>
53 linkEnvironmentToCloudWithPreference({
54 clerkToken,
55 connection,
56 liveActivitiesEnabled: input.previousEnabled,
57 }).pipe(
58 Effect.catchCause((cause) =>
59 Effect.logWarning(
60 `Could not restore Live Activity preference for environment ${connection.environmentId}.`,
61 cause,
62 ),
63 ),
64 ),
65 { concurrency: "unbounded" },
66 );
67 });
68​
69 yield* updateRelayPreference(input.enabled).pipe(
70 Effect.onError(() => restoreRelayPreference()),
71 );
72 },
73);