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