0.1.13 #18

Merged
Thilawyn merged 359 commits from next into master 2025-06-18 00:12:19 +02:00
5 changed files with 34 additions and 20 deletions
Showing only changes of commit 8fa24b1791 - Show all commits

View File

@@ -10,11 +10,13 @@ export class Uuid4Query extends QueryService.Tag("Uuid4Query")<Uuid4Query,
HttpClientError.HttpClientError | ParseResult.ParseError
>() {}
export const Uuid4QueryLive = QueryService.layer(Uuid4Query, Console.log("Querying...").pipe(
Effect.andThen(Effect.sleep("500 millis")),
Effect.andThen(HttpClient.get("https://www.uuidtools.com/api/generate/v4")),
HttpClient.withTracerPropagation(false),
Effect.flatMap(res => res.json),
Effect.flatMap(Schema.decodeUnknown(Result)),
Effect.scoped,
))
export const Uuid4QueryLive = QueryService.layer(Uuid4Query, {
query: Console.log("Querying...").pipe(
Effect.andThen(Effect.sleep("500 millis")),
Effect.andThen(HttpClient.get("https://www.uuidtools.com/api/generate/v4")),
HttpClient.withTracerPropagation(false),
Effect.flatMap(res => res.json),
Effect.flatMap(Schema.decodeUnknown(Result)),
Effect.scoped,
)
})

View File

@@ -17,7 +17,7 @@ function RouteComponent() {
const runSync = R.useRunSync()
const { state, refresh } = R.useQuery({
effect: () => Console.log("Querying...").pipe(
query: () => Console.log("Querying...").pipe(
Effect.andThen(Effect.sleep("500 millis")),
Effect.andThen(HttpClient.get("https://www.uuidtools.com/api/generate/v4")),
HttpClient.withTracerPropagation(false),
@@ -25,7 +25,7 @@ function RouteComponent() {
Effect.flatMap(Schema.decodeUnknown(Result)),
Effect.scoped,
),
deps: [],
key: [],
})
const [queryState] = R.useRefState(state)

View File

@@ -7,8 +7,8 @@ import * as QueryRunner from "./QueryRunner.js"
export interface UseQueryProps<A, E, R> {
effect: () => Effect.Effect<A, E, R>
readonly deps: React.DependencyList
readonly query: () => Effect.Effect<A, E, R>
readonly key: React.DependencyList
}
export interface UseQueryResult<A, E> {
@@ -22,12 +22,14 @@ export const QueryExtension = ReffuseExtension.make(() => ({
this: ReffuseHelpers.ReffuseHelpers<R>,
props: UseQueryProps<A, E, R>,
): UseQueryResult<A, E> {
const runner = this.useMemo(() => QueryRunner.make(props.effect()), [])
const runner = this.useMemo(() => QueryRunner.make({
query: props.query()
}), [])
this.useEffect(() => Effect.addFinalizer(() => runner.forkInterrupt).pipe(
Effect.andThen(Ref.set(runner.queryRef, props.effect())),
Effect.andThen(Ref.set(runner.queryRef, props.query())),
Effect.andThen(runner.forkFetch),
), [runner, ...props.deps])
), [runner, ...props.key])
this.useFork(() => Stream.runForEach(
BrowserStream.fromEventListenerWindow("focus"),

View File

@@ -13,12 +13,16 @@ export interface QueryRunner<A, E, R> {
}
export interface MakeProps<A, E, R> {
readonly query: Effect.Effect<A, E, R>
}
export const make = <A, E, R>(
query: Effect.Effect<A, E, R>
props: MakeProps<A, E, R>
): Effect.Effect<QueryRunner<A, E, R>, never, R> => Effect.gen(function*() {
const context = yield* Effect.context<R>()
const queryRef = yield* SubscriptionRef.make(query)
const queryRef = yield* SubscriptionRef.make(props.query)
const stateRef = yield* SubscriptionRef.make(AsyncData.noData<A, E>())
const fiberRef = yield* SubscriptionRef.make(Option.none<Fiber.RuntimeFiber<void>>())

View File

@@ -8,16 +8,22 @@ export interface QueryService<A, E> {
readonly refresh: Effect.Effect<Fiber.RuntimeFiber<void>>
}
export const Tag = <const Id extends string>(id: Id) => <
Self, A, E = never,
>() => Effect.Tag(id)<Self, QueryService<A, E>>()
export interface LayerProps<A, E, R> {
readonly query: Effect.Effect<A, E, R>
}
export const layer = <Self, Id extends string, A, E, R>(
tag: Context.TagClass<Self, Id, QueryService<A, E>>,
query: Effect.Effect<A, E, R>,
props: LayerProps<A, E, R>,
): Layer.Layer<Self, never, R> => Layer.effect(tag, Effect.gen(function*() {
const runner = yield* QueryRunner.make(query)
const runner = yield* QueryRunner.make({
query: props.query
})
return {
state: runner.stateRef,