diff --git a/packages/effect-fc-next/src/ScopeRegistry.ts b/packages/effect-fc-next/src/ScopeRegistry.ts index 0bdee76..1506987 100644 --- a/packages/effect-fc-next/src/ScopeRegistry.ts +++ b/packages/effect-fc-next/src/ScopeRegistry.ts @@ -11,7 +11,7 @@ export interface ScopeRegistryService { commit(key: ScopeRegistryService.Key): Effect.Effect release(key: ScopeRegistryService.Key): Effect.Effect - readonly run: Effect.Effect + readonly run: Effect.Effect } export declare namespace ScopeRegistryService { @@ -81,19 +81,20 @@ export class ScopeRegistryServiceImpl implements ScopeRegistryService { ) } - get run(): Effect.Effect { - return SubscriptionRef.changes(this.ref).pipe( - Stream.switchMap(entries => Option.match(this.getNextExpiration(entries), { - onNone: () => Stream.never, - onSome: expiresAt => Stream.fromEffect(DateTime.now.pipe( - Effect.flatMap(now => DateTime.isLessThan(now, expiresAt) - ? Effect.sleep(DateTime.distance(now, expiresAt)) - : Effect.void), - Effect.andThen(Effect.uninterruptible(this.closeExpired)), - )), - })), - Stream.runDrain, - Effect.ensuring(this.dispose), + get run(): Effect.Effect { + return Effect.addFinalizer(() => this.dispose).pipe( + Effect.andThen(SubscriptionRef.changes(this.ref).pipe( + Stream.switchMap(entries => Option.match(this.getNextExpiration(entries), { + onNone: () => Stream.never, + onSome: expiresAt => Stream.fromEffect(DateTime.now.pipe( + Effect.flatMap(now => DateTime.isLessThan(now, expiresAt) + ? Effect.sleep(DateTime.distance(now, expiresAt)) + : Effect.void), + Effect.andThen(Effect.uninterruptible(this.closeExpired)), + )), + })), + Stream.runDrain, + )), this.runSemaphore.withPermit, ) }