MCPcopy Create free account
hub / github.com/Effect-TS/effect / tx

Function tx

packages/effect/src/Effect.ts:14528–14568  ·  view source on GitHub ↗
(
  effect: Effect<A, E, R>
)

Source from the content-addressed store, hash-verified

14526 * @since 4.0.0
14527 */
14528export const tx = <A, E, R>(
14529 effect: Effect<A, E, R>
14530): Effect<A, E, Exclude<R, Transaction>> =>
14531 withFiber((fiber) => {
14532 let state = Context.getOrUndefined(fiber.context, Transaction)
14533 if (state) {
14534 return effect as Effect<A, E, Exclude<R, Transaction>>
14535 }
14536 // Create transaction state only at the outermost boundary
14537 state = { journal: new Map(), retry: false }
14538 let result: Exit.Exit<A, E> | undefined
14539 return uninterruptibleMask((restore) =>
14540 flatMap(
14541 whileLoop({
14542 while: () => !result,
14543 body: constant(
14544 restore(effect).pipe(
14545 provideService(Transaction, state),
14546 tapCause(() => {
14547 if (!state.retry) return void_
14548 return restore(awaitPendingTransaction(state))
14549 }),
14550 exit
14551 )
14552 ),
14553 step(exit: Exit.Exit<A, E>) {
14554 if (state.retry || !isTransactionConsistent(state)) {
14555 return clearTransaction(state)
14556 }
14557 if (Exit.isSuccess(exit)) {
14558 commitTransaction(fiber, state)
14559 } else {
14560 clearTransaction(state)
14561 }
14562 result = exit
14563 }
14564 }),
14565 () => result!
14566 )
14567 )
14568 })
14569
14570const isTransactionConsistent = (state: Transaction["Service"]) => {
14571 for (const [ref, { version }] of state.journal) {

Callers

nothing calls this directly

Calls 4

constantFunction · 0.90
provideServiceFunction · 0.85
awaitPendingTransactionFunction · 0.85
pipeMethod · 0.65

Tested by

no test coverage detected