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

Function getState

packages/effect/src/internal/rcRef.ts:96–137  ·  view source on GitHub ↗
(self: RcRefImpl<A, E>)

Source from the content-addressed store, hash-verified

94 })
95
96const getState = <A, E>(self: RcRefImpl<A, E>) =>
97 Effect.uninterruptibleMask(function loop(restore): Effect.Effect<State.Acquired<A>, E> {
98 switch (self.state._tag) {
99 case "Closed": {
100 return Effect.interrupt
101 }
102 case "Acquired": {
103 self.state.refCount++
104 return self.state.fiber
105 ? Effect.as(Fiber.interrupt(self.state.fiber), self.state)
106 : Effect.succeed(self.state)
107 }
108 case "Empty": {
109 const scope = Scope.makeUnsafe()
110 return self.semaphore.withPermit(
111 Effect.suspend(() => {
112 if (self.state._tag !== "Empty") {
113 return loop(restore)
114 }
115 return restore(Effect.provideContext(
116 self.acquire as Effect.Effect<A, E>,
117 Context.add(self.context, Scope.Scope, scope)
118 )).pipe(
119 Effect.map((value) => {
120 const state: State.Acquired<A> = {
121 _tag: "Acquired",
122 value,
123 scope,
124 fiber: undefined,
125 refCount: 1,
126 invalidated: false
127 }
128 self.state = state
129 return state
130 }),
131 Effect.onExit((exit) => Exit.isFailure(exit) ? Scope.close(scope, exit) : Effect.void)
132 )
133 })
134 )
135 }
136 }
137 })
138
139/** @internal */
140export const get = Effect.fnUntraced(function*<A, E>(

Callers 1

rcRef.tsFile · 0.70

Calls 8

withPermitMethod · 0.80
loopFunction · 0.70
pipeMethod · 0.65
addMethod · 0.65
closeMethod · 0.65
interruptMethod · 0.45
succeedMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected