This commit is contained in:
@@ -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.andThen(HashMap.get(key)),
|
Effect.map(scopeMap => scopeMap.ref),
|
||||||
Effect.tap(entry => Option.match(entry.closeFiber, {
|
Effect.tap(ref => ref.pipe(
|
||||||
onSome: fiber => Effect.andThen(
|
Effect.andThen(HashMap.get(key)),
|
||||||
Ref.update(scopeMap.ref, HashMap.set(key, { ...entry, closeFiber: Option.none() })),
|
Effect.andThen(entry => Option.match(entry.closeFiber, {
|
||||||
Fiber.interruptFork(fiber),
|
onSome: Fiber.interruptFork,
|
||||||
),
|
onNone: () => Effect.void,
|
||||||
onNone: () => Effect.void,
|
})),
|
||||||
})),
|
)),
|
||||||
Effect.map(({ scope }) =>
|
Effect.map(ref =>
|
||||||
() => Runtime.runSync(runtimeRef.current)(Effect.andThen(
|
() => Runtime.runSync(runtimeRef.current)(Effect.andThen(
|
||||||
Effect.forkDaemon(Effect.sleep(options?.finalizerExecutionDebounce ?? defaultOptions.finalizerExecutionDebounce).pipe(
|
Effect.sleep(options?.finalizerExecutionDebounce ?? defaultOptions.finalizerExecutionDebounce).pipe(
|
||||||
Effect.andThen(Scope.close(scope, Exit.void)),
|
Effect.andThen(Scope.close(scope, Exit.void)),
|
||||||
Effect.andThen(Ref.update(scopeMap.ref, HashMap.remove(key))),
|
Effect.onExit(() => Ref.update(ref, HashMap.remove(key))),
|
||||||
)),
|
Effect.forkDaemon,
|
||||||
fiber => Ref.update(scopeMap.ref, HashMap.set(key, {
|
),
|
||||||
|
fiber => Ref.update(ref, HashMap.set(key, {
|
||||||
scope,
|
scope,
|
||||||
closeFiber: Option.some(fiber),
|
closeFiber: Option.some(fiber),
|
||||||
})),
|
})),
|
||||||
|
|||||||
Reference in New Issue
Block a user