diff --git a/packages/effect-fc-next/src/ScopeRegistry.ts b/packages/effect-fc-next/src/ScopeRegistry.ts index 643f1d8..7720aaf 100644 --- a/packages/effect-fc-next/src/ScopeRegistry.ts +++ b/packages/effect-fc-next/src/ScopeRegistry.ts @@ -1,4 +1,4 @@ -import { type Cause, Context, DateTime, type Duration, Effect, Equal, Exit, flow, HashMap, Layer, Option, Order, Scope, Semaphore, Stream, SubscriptionRef } from "effect" +import { type Cause, Chunk, Context, DateTime, type Duration, Effect, Equal, Exit, HashMap, Layer, Option, Order, Scope, Semaphore, Stream, SubscriptionRef } from "effect" export interface ScopeRegistryService { @@ -115,22 +115,19 @@ export class ScopeRegistryServiceImpl implements ScopeRegistryService { get closeExpired(): Effect.Effect { return Effect.flatMap(DateTime.now, now => SubscriptionRef.modify( this.ref, - entries => { - const expired: ScopeRegistryService.Entry[] = [] - const remaining = HashMap.filter(entries, entry => { - const shouldClose = Option.exists( - entry.expiresAt, - expiresAt => DateTime.isLessThanOrEqualTo(expiresAt, now), - ) - - if (shouldClose) - expired.push(entry) - - return !shouldClose - }) - - return [expired, remaining] - }, + entries => HashMap.reduce( + entries, + [ + Chunk.empty(), + HashMap.empty(), + ] as const, + ([expired, remaining], entry, key) => Option.exists( + entry.expiresAt, + expiresAt => DateTime.isLessThanOrEqualTo(expiresAt, now), + ) + ? [Chunk.append(expired, entry), remaining] as const + : [expired, HashMap.set(remaining, key, entry)] as const, + ), )).pipe( Effect.flatMap(entries => Effect.forEach( entries,