Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
999 B · 30 lines
TypeScript
12345678910111213141516171819202122232425262728293031import * as Cause from "effect/Cause";import * as Effect from "effect/Effect";import * as Exit from "effect/Exit";
// Cloudflare must see a rejected callback to roll back. Carry the local Effect// cause through that Promise boundary without flattening typed failures into// defects or committing a failed program's partial writes.export function transaction<A, E>( storage: DurableObjectStorage, action: (tx: DurableObjectTransaction) => Effect.Effect<A, E>,) { class Rollback { constructor(readonly cause: Cause.Cause<E>) {} } return Effect.tryPromise({ try: (signal) => storage.transaction(async (tx) => { const exit = await Effect.runPromiseExit(action(tx), { signal }); if (Exit.isSuccess(exit)) return exit.value; throw new Rollback(exit.cause); }), catch: (error) => error, }).pipe( Effect.catch((error) => error instanceof Rollback ? Effect.failCause(error.cause) : Effect.die(error), ), );}