MCPcopy
hub / github.com/Effect-TS/effect / unsafeAtomically

Function unsafeAtomically

packages/effect/src/internal/stm/core.ts:192–235  ·  view source on GitHub ↗
(
  self: STM.STM<A, E, R>,
  onDone: (exit: Exit.Exit<A, E>) => unknown,
  onInterrupt: LazyArg<unknown>
)

Source from the content-addressed store, hash-verified

190
191/** @internal */
192export const unsafeAtomically = <A, E, R>(
193 self: STM.STM<A, E, R>,
194 onDone: (exit: Exit.Exit<A, E>) => unknown,
195 onInterrupt: LazyArg<unknown>
196): Effect.Effect<A, E, R> =>
197 withFiberRuntime((state) => {
198 const fiberId = state.id()
199 const env = state.getFiberRef(FiberRef.currentContext) as Context.Context<R>
200 const scheduler = state.getFiberRef(FiberRef.currentScheduler)
201 const priority = state.getFiberRef(FiberRef.currentSchedulingPriority)
202 const commitResult = tryCommitSync(fiberId, self, env, scheduler, priority)
203 switch (commitResult._tag) {
204 case TryCommitOpCodes.OP_DONE: {
205 onDone(commitResult.exit)
206 return commitResult.exit
207 }
208 case TryCommitOpCodes.OP_SUSPEND: {
209 const txnId = TxnId.make()
210 const state: { value: STMState.STMState<A, E> } = { value: STMState.running }
211 const effect = Effect.async(
212 (k: (effect: Effect.Effect<A, E, R>) => unknown): void =>
213 tryCommitAsync(fiberId, self, txnId, state, env, scheduler, priority, k)
214 )
215 return Effect.uninterruptibleMask((restore) =>
216 pipe(
217 restore(effect),
218 Effect.catchAllCause((cause) => {
219 let currentState = state.value
220 if (STMState.isRunning(currentState)) {
221 state.value = STMState.interrupted
222 }
223 currentState = state.value
224 if (STMState.isDone(currentState)) {
225 onDone(currentState.exit)
226 return currentState.exit
227 }
228 onInterrupt()
229 return Effect.failCause(cause)
230 })
231 )
232 )
233 }
234 }
235 })
236
237/** @internal */
238const tryCommit = <A, E, R>(

Callers 2

commitMethod · 0.85
commitFunction · 0.85

Calls 10

withFiberRuntimeFunction · 0.85
tryCommitSyncFunction · 0.85
tryCommitAsyncFunction · 0.85
restoreFunction · 0.85
failCauseMethod · 0.80
idMethod · 0.65
getFiberRefMethod · 0.65
makeMethod · 0.65
onDoneFunction · 0.50
pipeFunction · 0.50

Tested by

no test coverage detected