@reffuse/extension-query 0.1.4 #15
@@ -8,7 +8,7 @@ import * as QueryService from "./QueryService.js"
|
|||||||
|
|
||||||
export interface UseQueryProps<K extends readonly unknown[], A, E, R> {
|
export interface UseQueryProps<K extends readonly unknown[], A, E, R> {
|
||||||
readonly key: Stream.Stream<K>
|
readonly key: Stream.Stream<K>
|
||||||
readonly query: () => Effect.Effect<A, E, R>
|
readonly query: (key: K) => Effect.Effect<A, E, R>
|
||||||
readonly refreshOnWindowFocus?: boolean
|
readonly refreshOnWindowFocus?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,10 +5,12 @@ import { Effect, Fiber, identity, Option, Ref, Stream, SubscriptionRef } from "e
|
|||||||
|
|
||||||
export interface QueryRunner<K extends readonly unknown[], A, E, R> {
|
export interface QueryRunner<K extends readonly unknown[], A, E, R> {
|
||||||
readonly key: Stream.Stream<K>
|
readonly key: Stream.Stream<K>
|
||||||
readonly queryRef: SubscriptionRef.SubscriptionRef<Effect.Effect<A, E, R>>
|
readonly query: (key: K) => Effect.Effect<A, E, R>
|
||||||
|
|
||||||
readonly stateRef: SubscriptionRef.SubscriptionRef<AsyncData.AsyncData<A, E>>
|
readonly stateRef: SubscriptionRef.SubscriptionRef<AsyncData.AsyncData<A, E>>
|
||||||
readonly fiberRef: SubscriptionRef.SubscriptionRef<Option.Option<Fiber.RuntimeFiber<void>>>
|
readonly fiberRef: SubscriptionRef.SubscriptionRef<Option.Option<Fiber.RuntimeFiber<void>>>
|
||||||
|
|
||||||
|
readonly interrupt: Effect.Effect<void>
|
||||||
readonly forkInterrupt: Effect.Effect<Fiber.RuntimeFiber<void>>
|
readonly forkInterrupt: Effect.Effect<Fiber.RuntimeFiber<void>>
|
||||||
readonly forkFetch: Effect.Effect<Fiber.RuntimeFiber<void>>
|
readonly forkFetch: Effect.Effect<Fiber.RuntimeFiber<void>>
|
||||||
readonly forkRefresh: Effect.Effect<Fiber.RuntimeFiber<void>>
|
readonly forkRefresh: Effect.Effect<Fiber.RuntimeFiber<void>>
|
||||||
@@ -19,7 +21,7 @@ export interface QueryRunner<K extends readonly unknown[], A, E, R> {
|
|||||||
|
|
||||||
export interface MakeProps<K extends readonly unknown[], A, E, R> {
|
export interface MakeProps<K extends readonly unknown[], A, E, R> {
|
||||||
readonly key: Stream.Stream<K>
|
readonly key: Stream.Stream<K>
|
||||||
readonly query: Effect.Effect<A, E, R>
|
readonly query: (key: K) => Effect.Effect<A, E, R>
|
||||||
}
|
}
|
||||||
|
|
||||||
export const make = <K extends readonly unknown[], A, E, R>(
|
export const make = <K extends readonly unknown[], A, E, R>(
|
||||||
@@ -27,7 +29,6 @@ export const make = <K extends readonly unknown[], A, E, R>(
|
|||||||
): Effect.Effect<QueryRunner<K, A, E, R>, never, R> => Effect.gen(function*() {
|
): Effect.Effect<QueryRunner<K, A, E, R>, never, R> => Effect.gen(function*() {
|
||||||
const context = yield* Effect.context<R>()
|
const context = yield* Effect.context<R>()
|
||||||
|
|
||||||
const queryRef = yield* SubscriptionRef.make(props.query)
|
|
||||||
const stateRef = yield* SubscriptionRef.make(AsyncData.noData<A, E>())
|
const stateRef = yield* SubscriptionRef.make(AsyncData.noData<A, E>())
|
||||||
const fiberRef = yield* SubscriptionRef.make(Option.none<Fiber.RuntimeFiber<void>>())
|
const fiberRef = yield* SubscriptionRef.make(Option.none<Fiber.RuntimeFiber<void>>())
|
||||||
|
|
||||||
@@ -86,8 +87,8 @@ export const make = <K extends readonly unknown[], A, E, R>(
|
|||||||
return AsyncData.refreshing(previous.previous)
|
return AsyncData.refreshing(previous.previous)
|
||||||
return AsyncData.loading()
|
return AsyncData.loading()
|
||||||
}).pipe(
|
}).pipe(
|
||||||
Effect.andThen(queryRef),
|
|
||||||
Effect.flatMap(identity),
|
Effect.andThen(props.query()),
|
||||||
Effect.matchCauseEffect({
|
Effect.matchCauseEffect({
|
||||||
onSuccess: v => Ref.set(stateRef, AsyncData.success(v)),
|
onSuccess: v => Ref.set(stateRef, AsyncData.success(v)),
|
||||||
onFailure: c => Ref.set(stateRef, AsyncData.failure(c)),
|
onFailure: c => Ref.set(stateRef, AsyncData.failure(c)),
|
||||||
@@ -115,7 +116,8 @@ export const make = <K extends readonly unknown[], A, E, R>(
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
key: props.key,
|
key: props.key,
|
||||||
queryRef,
|
query: props.query,
|
||||||
|
|
||||||
stateRef,
|
stateRef,
|
||||||
fiberRef,
|
fiberRef,
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user