Open History to see the last commit
1import { safeErrorLogAttributes } from "@t3tools/client-runtime/errors";
2import { Persistence } from "@t3tools/client-runtime/platform";
3import type { EnvironmentId } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Schema from "effect/Schema";
7
8import { clearThreadComposerErrorsForEnvironment } from "../state/thread-composer-error";
9import { clearThreadOutboxEnvironment } from "../state/thread-outbox-removal";
10import { clearComposerDraftsEnvironment } from "../state/use-composer-drafts";
11import { mobileThreadTriageOutboxStore } from "../state/thread-triage-outbox-storage";
12
13class EnvironmentOwnedDataClearError extends Schema.TaggedError<EnvironmentOwnedDataClearError>()(
14 "EnvironmentOwnedDataClearError",
15 { cause: Schema.Defect() },
16) {}
17
18export const layer = Layer.succeed(
19 Persistence.EnvironmentOwnedDataCleanup,
20 Persistence.EnvironmentOwnedDataCleanup.of({
21 clear: Effect.fn("Persistence.EnvironmentOwnedDataCleanup.clear")(function (environmentId) {
22 // Each clear settles on its own: a failure must not interrupt a clear that is still running.
23 const settle = (
24 operation: "outbox" | "drafts",
25 clear: (environmentId: EnvironmentId) => Promise<void>,
26 ) =>
27 Effect.tryPromise({
28 try: () => clear(environmentId),
29 catch: (cause) => new EnvironmentOwnedDataClearError({ cause }),
30 }).pipe(
31 Effect.catch((error) =>
32 Effect.logWarning("Could not clear mobile environment-owned data.", {
33 environmentId,
34 operation,
35 ...safeErrorLogAttributes(error.cause),
36 }),
37 ),
38 );
39 return Effect.all(
40 [
41 settle("outbox", clearThreadOutboxEnvironment),
42 settle("drafts", clearComposerDraftsEnvironment),
43 Effect.sync(() => clearThreadComposerErrorsForEnvironment(environmentId)),
44 Effect.ignore(mobileThreadTriageOutboxStore.clearEnvironment(environmentId)),
45 ],
46 { concurrency: "unbounded", discard: true },
47 );
48 }),
49 }),
50);