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

Class MicroScopeImpl

packages/effect/src/Micro.ts:3317–3378  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3315export const MicroScope: Context.Tag<MicroScope, MicroScope> = Context.GenericTag<MicroScope>("effect/Micro/MicroScope")
3316
3317class MicroScopeImpl implements MicroScope.Closeable {
3318 readonly [MicroScopeTypeId]: MicroScopeTypeId
3319 state: {
3320 readonly _tag: "Open"
3321 readonly finalizers: Set<(exit: MicroExit<any, any>) => Micro<void>>
3322 } | {
3323 readonly _tag: "Closed"
3324 readonly exit: MicroExit<any, any>
3325 } = { _tag: "Open", finalizers: new Set() }
3326
3327 constructor() {
3328 this[MicroScopeTypeId] = MicroScopeTypeId
3329 }
3330
3331 unsafeAddFinalizer(finalizer: (exit: MicroExit<any, any>) => Micro<void>): void {
3332 if (this.state._tag === "Open") {
3333 this.state.finalizers.add(finalizer)
3334 }
3335 }
3336 addFinalizer(finalizer: (exit: MicroExit<any, any>) => Micro<void>): Micro<void> {
3337 return suspend(() => {
3338 if (this.state._tag === "Open") {
3339 this.state.finalizers.add(finalizer)
3340 return void_
3341 }
3342 return finalizer(this.state.exit)
3343 })
3344 }
3345 unsafeRemoveFinalizer(finalizer: (exit: MicroExit<any, any>) => Micro<void>): void {
3346 if (this.state._tag === "Open") {
3347 this.state.finalizers.delete(finalizer)
3348 }
3349 }
3350 close(microExit: MicroExit<any, any>): Micro<void> {
3351 return suspend(() => {
3352 if (this.state._tag === "Open") {
3353 const finalizers = Array.from(this.state.finalizers).reverse()
3354 this.state = { _tag: "Closed", exit: microExit }
3355 return flatMap(
3356 forEach(finalizers, (finalizer) => exit(finalizer(microExit))),
3357 exitVoidAll
3358 )
3359 }
3360 return void_
3361 })
3362 }
3363 get fork() {
3364 return sync(() => {
3365 const newScope = new MicroScopeImpl()
3366 if (this.state._tag === "Closed") {
3367 newScope.state = this.state
3368 return newScope
3369 }
3370 function fin(exit: MicroExit<any, any>) {
3371 return newScope.close(exit)
3372 }
3373 this.state.finalizers.add(fin)
3374 newScope.unsafeAddFinalizer((_) => sync(() => this.unsafeRemoveFinalizer(fin)))

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…