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

Class MicroScopeImpl

packages/effect/src/Micro.ts:3318–3379  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected