0.1.11 #14

Merged
Thilawyn merged 318 commits from next into master 2025-05-19 14:01:41 +02:00
3 changed files with 47 additions and 21 deletions
Showing only changes of commit f99d18b846 - Show all commits

View File

@@ -21,13 +21,23 @@ function RouteComponent() {
const query = R.useQuery({ const query = R.useQuery({
query: () => Console.log(`Querying ${ count } IDs...`).pipe( query: () => Console.log(`Querying ${ count } IDs...`).pipe(
// Effect.andThen(Effect.sleep("500 millis")), Effect.andThen(Effect.sleep("500 millis")),
Effect.andThen(HttpClient.get(`https://www.uuidtools.com/api/generate/v4/count/${ count }`)), Effect.andThen(HttpClient.get(`https://www.uuidtools.com/api/generate/v4/count/${ count }`)),
HttpClient.withTracerPropagation(false), HttpClient.withTracerPropagation(false),
Effect.flatMap(res => res.json), Effect.flatMap(res => res.json),
Effect.flatMap(Schema.decodeUnknown(Result)), Effect.flatMap(Schema.decodeUnknown(Result)),
Effect.scoped, Effect.scoped,
), ),
// query: () => Console.log(`Creating ${ count } IDs...`).pipe(
// Effect.andThen(Effect.sleep("500 millis")),
// Effect.andThen(pipe(
// Array.range(1, count),
// Array.map(() => makeUuid4),
// Effect.all,
// )),
// Effect.flatMap(Schema.decode(Result)),
// Effect.provide(GetRandomValues.CryptoRandom),
// ),
key: ["uuid4", count], key: ["uuid4", count],
}) })

View File

@@ -21,14 +21,20 @@ export const QueryExtension = ReffuseExtension.make(() => ({
this: ReffuseHelpers.ReffuseHelpers<R>, this: ReffuseHelpers.ReffuseHelpers<R>,
props: UseQueryProps<A, E, R>, props: UseQueryProps<A, E, R>,
): UseQueryResult<A, E> { ): UseQueryResult<A, E> {
const runSync = this.useRunSync()
const runner = this.useMemo(() => QueryRunner.make({ const runner = this.useMemo(() => QueryRunner.make({
query: props.query() query: props.query()
}), []) }), [])
this.useEffect(() => Effect.addFinalizer(() => runner.forkInterrupt).pipe( React.useEffect(() => {
Effect.andThen(Ref.set(runner.queryRef, props.query())), Ref.set(runner.queryRef, props.query()).pipe(
Effect.andThen(runner.forkFetch), Effect.andThen(runner.forkFetch),
), [runner, ...props.key]) runSync,
)
return () => { runSync(runner.forkInterrupt) }
}, [runner, ...props.key])
this.useFork(() => runner.refreshOnWindowFocus, [runner]) this.useFork(() => runner.refreshOnWindowFocus, [runner])

View File

@@ -31,17 +31,28 @@ export const make = <A, E, R>(
const interrupt = fiberRef.pipe( const interrupt = fiberRef.pipe(
Effect.flatMap(Option.match({ Effect.flatMap(Option.match({
onSome: Fiber.interrupt, onSome: fiber => Ref.set(fiberRef, Option.none()).pipe(
Effect.andThen(Fiber.interrupt(fiber))
),
onNone: () => Effect.void, onNone: () => Effect.void,
})) }))
) )
const forkInterrupt = Effect.forkDaemon(interrupt) const forkInterrupt = fiberRef.pipe(
Effect.flatMap(Option.match({
onSome: fiber => Ref.set(fiberRef, Option.none()).pipe(
Effect.andThen(Fiber.interrupt(fiber).pipe(
Effect.asVoid,
Effect.forkDaemon,
))
),
onNone: () => Effect.forkDaemon(Effect.void),
}))
)
const forkFetch = interrupt.pipe( const forkFetch = interrupt.pipe(
Effect.andThen( Effect.andThen(
Effect.addFinalizer(() => Ref.set(fiberRef, Option.none())).pipe( Ref.set(stateRef, AsyncData.loading()).pipe(
Effect.andThen(Ref.set(stateRef, AsyncData.loading())),
Effect.andThen(queryRef.pipe(Effect.flatMap(identity))), Effect.andThen(queryRef.pipe(Effect.flatMap(identity))),
Effect.matchCauseEffect({ Effect.matchCauseEffect({
onSuccess: v => Ref.set(stateRef, AsyncData.success(v)), onSuccess: v => Ref.set(stateRef, AsyncData.success(v)),
@@ -49,14 +60,14 @@ export const make = <A, E, R>(
}), }),
Effect.provide(context), Effect.provide(context),
Effect.scoped,
Effect.fork, Effect.fork,
) )
), ),
Effect.flatMap(fiber => Effect.flatMap(fiber =>
Ref.set(fiberRef, Option.some(fiber)).pipe( Ref.set(fiberRef, Option.some(fiber)).pipe(
Effect.andThen(Fiber.join(fiber)) Effect.andThen(Fiber.join(fiber)),
Effect.andThen(Ref.set(fiberRef, Option.none())),
) )
), ),
@@ -65,14 +76,13 @@ export const make = <A, E, R>(
const forkRefresh = interrupt.pipe( const forkRefresh = interrupt.pipe(
Effect.andThen( Effect.andThen(
Effect.addFinalizer(() => Ref.set(fiberRef, Option.none())).pipe( Ref.update(stateRef, previous => {
Effect.andThen(Ref.update(stateRef, previous => { if (AsyncData.isSuccess(previous) || AsyncData.isFailure(previous))
if (AsyncData.isSuccess(previous) || AsyncData.isFailure(previous)) return AsyncData.refreshing(previous)
return AsyncData.refreshing(previous) if (AsyncData.isRefreshing(previous))
if (AsyncData.isRefreshing(previous)) return AsyncData.refreshing(previous.previous)
return AsyncData.refreshing(previous.previous) return AsyncData.loading()
return AsyncData.loading() }).pipe(
})),
Effect.andThen(queryRef.pipe(Effect.flatMap(identity))), Effect.andThen(queryRef.pipe(Effect.flatMap(identity))),
Effect.matchCauseEffect({ Effect.matchCauseEffect({
onSuccess: v => Ref.set(stateRef, AsyncData.success(v)), onSuccess: v => Ref.set(stateRef, AsyncData.success(v)),
@@ -80,14 +90,14 @@ export const make = <A, E, R>(
}), }),
Effect.provide(context), Effect.provide(context),
Effect.scoped,
Effect.fork, Effect.fork,
) )
), ),
Effect.flatMap(fiber => Effect.flatMap(fiber =>
Ref.set(fiberRef, Option.some(fiber)).pipe( Ref.set(fiberRef, Option.some(fiber)).pipe(
Effect.andThen(Fiber.join(fiber)) Effect.andThen(Fiber.join(fiber)),
Effect.andThen(Ref.set(fiberRef, Option.none())),
) )
), ),