0.1.2 #4
@@ -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),
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
|
||||
@@ -270,15 +270,13 @@ export class Reffuse<R> {
|
||||
const runFork = this.useRunFork()
|
||||
|
||||
return React.useEffect(() => {
|
||||
const scope = runSync(Scope.make(options?.finalizerExecutionStrategy))
|
||||
const fiber = runFork(Effect.provideService(effect, Scope.Scope, scope), options)
|
||||
|
||||
return () => {
|
||||
Fiber.interrupt(fiber).pipe(
|
||||
Effect.andThen(Scope.close(scope, Exit.void)),
|
||||
runFork,
|
||||
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 () => { runFork(Scope.close(scope, Exit.void)) }
|
||||
}, [
|
||||
...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync, runFork],
|
||||
...(deps ?? []),
|
||||
@@ -322,15 +320,17 @@ export class Reffuse<R> {
|
||||
}),
|
||||
|
||||
// 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],
|
||||
|
||||
Reference in New Issue
Block a user