Fix useScope
All checks were successful
Lint / lint (push) Successful in 41s

This commit is contained in:
Julien Valverdé
2026-01-23 01:20:11 +01:00
parent 3be9d94aa8
commit 581bab027c

View File

@@ -522,43 +522,40 @@ export const useScope = Effect.fnUntraced(function*(
const runtimeRef = React.useRef<Runtime.Runtime<never>>(null!) const runtimeRef = React.useRef<Runtime.Runtime<never>>(null!)
runtimeRef.current = yield* Effect.runtime() runtimeRef.current = yield* Effect.runtime()
const scopeMap = yield* ScopeMap as unknown as Effect.Effect<ScopeMap> const { key, scope } = React.useMemo(() => Runtime.runSync(runtimeRef.current)(Effect.Do.pipe(
Effect.bind("scopeMapRef", () => Effect.map(
const [key, scope] = React.useMemo(() => Runtime.runSync(runtimeRef.current)(Effect.andThen( ScopeMap as unknown as Effect.Effect<ScopeMap>,
Effect.all([Effect.succeed({}), scopeMap.ref]), scopeMap => scopeMap.ref,
([key, map]) => Effect.andThen( )),
Option.match(HashMap.get(map, key), { Effect.let("key", () => ({})),
onSome: entry => Effect.succeed(entry.scope), Effect.bind("scope", () => Scope.make(options?.finalizerExecutionStrategy ?? defaultOptions.finalizerExecutionStrategy)),
onNone: () => Effect.tap( Effect.tap(({ scopeMapRef, key, scope }) =>
Scope.make(options?.finalizerExecutionStrategy ?? defaultOptions.finalizerExecutionStrategy), Ref.update(scopeMapRef, HashMap.set(key, {
scope => Ref.update(scopeMap.ref, HashMap.set(key, {
scope, scope,
closeFiber: Option.none(), closeFiber: Option.none(),
})), }))
),
}),
scope => [key, scope] as const,
), ),
// biome-ignore lint/correctness/useExhaustiveDependencies: use of React.DependencyList // biome-ignore lint/correctness/useExhaustiveDependencies: use of React.DependencyList
)), deps) )), deps)
// biome-ignore lint/correctness/useExhaustiveDependencies: only reactive on "key" // biome-ignore lint/correctness/useExhaustiveDependencies: only reactive on "key"
React.useEffect(() => Runtime.runSync(runtimeRef.current)(scopeMap.ref.pipe( React.useEffect(() => Runtime.runSync(runtimeRef.current)((ScopeMap as unknown as Effect.Effect<ScopeMap>).pipe(
Effect.map(scopeMap => scopeMap.ref),
Effect.tap(ref => ref.pipe(
Effect.andThen(HashMap.get(key)), Effect.andThen(HashMap.get(key)),
Effect.tap(entry => Option.match(entry.closeFiber, { Effect.andThen(entry => Option.match(entry.closeFiber, {
onSome: fiber => Effect.andThen( onSome: Fiber.interruptFork,
Ref.update(scopeMap.ref, HashMap.set(key, { ...entry, closeFiber: Option.none() })),
Fiber.interruptFork(fiber),
),
onNone: () => Effect.void, onNone: () => Effect.void,
})), })),
Effect.map(({ scope }) =>
() => Runtime.runSync(runtimeRef.current)(Effect.andThen(
Effect.forkDaemon(Effect.sleep(options?.finalizerExecutionDebounce ?? defaultOptions.finalizerExecutionDebounce).pipe(
Effect.andThen(Scope.close(scope, Exit.void)),
Effect.andThen(Ref.update(scopeMap.ref, HashMap.remove(key))),
)), )),
fiber => Ref.update(scopeMap.ref, HashMap.set(key, { Effect.map(ref =>
() => Runtime.runSync(runtimeRef.current)(Effect.andThen(
Effect.sleep(options?.finalizerExecutionDebounce ?? defaultOptions.finalizerExecutionDebounce).pipe(
Effect.andThen(Scope.close(scope, Exit.void)),
Effect.onExit(() => Ref.update(ref, HashMap.remove(key))),
Effect.forkDaemon,
),
fiber => Ref.update(ref, HashMap.set(key, {
scope, scope,
closeFiber: Option.some(fiber), closeFiber: Option.some(fiber),
})), })),