diff --git a/packages/example/src/routes/promise.tsx b/packages/example/src/routes/promise.tsx index d5c9b57..9577aca 100644 --- a/packages/example/src/routes/promise.tsx +++ b/packages/example/src/routes/promise.tsx @@ -2,7 +2,7 @@ import { R } from "@/reffuse" import { HttpClient } from "@effect/platform" import { Text } from "@radix-ui/themes" import { createFileRoute } from "@tanstack/react-router" -import { Effect, Schema } from "effect" +import { Console, Effect, Schema } from "effect" import { Suspense, use } from "react" @@ -15,7 +15,8 @@ const Result = Schema.Tuple(Schema.String) type Result = typeof Result.Type function RouteComponent() { - const promise = R.usePromiseScoped(HttpClient.HttpClient.pipe( + const promise = R.usePromiseScoped(Effect.addFinalizer(() => Console.log("Cleanup")).pipe( + Effect.andThen(HttpClient.HttpClient), Effect.flatMap(client => client.get("https://www.uuidtools.com/api/generate/v4")), HttpClient.withTracerPropagation(false), Effect.flatMap(res => res.json), diff --git a/packages/example/src/routes/time.tsx b/packages/example/src/routes/time.tsx index 3a60060..7332f6b 100644 --- a/packages/example/src/routes/time.tsx +++ b/packages/example/src/routes/time.tsx @@ -1,6 +1,6 @@ import { R } from "@/reffuse" import { createFileRoute } from "@tanstack/react-router" -import { DateTime, Effect, Ref, Schedule, Stream, SubscriptionRef } from "effect" +import { Console, DateTime, Effect, Ref, Schedule, Stream, SubscriptionRef } from "effect" const timeEverySecond = Stream.repeatEffectWithSchedule( @@ -16,7 +16,11 @@ export const Route = createFileRoute("/time")({ function Time() { const timeRef = R.useMemo(DateTime.now.pipe(Effect.flatMap(SubscriptionRef.make))) - R.useFork(Stream.runForEach(timeEverySecond, v => Ref.set(timeRef, v)), [timeRef]) + + R.useFork(Effect.addFinalizer(() => Console.log("Cleanup")).pipe( + Effect.andThen(Stream.runForEach(timeEverySecond, v => Ref.set(timeRef, v))) + ), [timeRef]) + const [time] = R.useRefState(timeRef) diff --git a/packages/reffuse/src/Reffuse.ts b/packages/reffuse/src/Reffuse.ts index 6eb0753..08700cb 100644 --- a/packages/reffuse/src/Reffuse.ts +++ b/packages/reffuse/src/Reffuse.ts @@ -270,15 +270,13 @@ export class Reffuse { const runFork = this.useRunFork() return React.useEffect(() => { - const scope = runSync(Scope.make(options?.finalizerExecutionStrategy)) - const fiber = runFork(Effect.provideService(effect, Scope.Scope, scope), options) + const scope = runSync(options?.scope + ? Scope.fork(options.scope, options?.finalizerExecutionStrategy ?? ExecutionStrategy.sequential) + : Scope.make(options?.finalizerExecutionStrategy) + ) + runFork(Effect.provideService(effect, Scope.Scope, scope), { ...options, scope }) - return () => { - Fiber.interrupt(fiber).pipe( - Effect.andThen(Scope.close(scope, Exit.void)), - runFork, - ) - } + return () => { runFork(Scope.close(scope, Exit.void)) } }, [ ...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync, runFork], ...(deps ?? []), @@ -322,15 +320,17 @@ export class Reffuse { }), // TODO: use scope from RunForkOptions? - effect => runFork(effect, options), + effect => runFork(effect, { ...options, scope }), ) return () => { - Fiber.interrupt(fiber).pipe( - Effect.andThen(Scope.close(scope, Exit.void)), - Effect.andThen(Effect.sync(() => { reject() })), // TODO: Relevant? - runFork, - ) + // Fiber.interrupt(fiber).pipe( + // Effect.andThen(Scope.close(scope, Exit.void)), + // // \/ TODO: should interrupted promises reject? + // // Effect.andThen(Effect.sync(() => { reject() })), + // runFork, + // ) + runFork(Scope.close(scope, Exit.void)) } }, [ ...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync, runFork],