This commit is contained in:
@@ -37,6 +37,9 @@
|
||||
"clean:dist": "rm -rf dist",
|
||||
"clean:modules": "rm -rf node_modules"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@effect/platform-browser": "^0.74.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": "^19.2.0",
|
||||
"effect": "^3.19.0",
|
||||
|
||||
@@ -16,6 +16,7 @@ extends Pipeable.Pipeable {
|
||||
readonly initialProgress: P
|
||||
|
||||
readonly staleTime: Duration.DurationInput
|
||||
readonly refreshOnWindowFocus: boolean
|
||||
|
||||
readonly latestKey: Subscribable.Subscribable<Option.Option<K>>
|
||||
readonly fiber: Subscribable.Subscribable<Option.Option<Fiber.Fiber<A, E>>>
|
||||
@@ -47,6 +48,7 @@ extends Pipeable.Class() implements Query<K, A, E, R, P> {
|
||||
readonly initialProgress: P,
|
||||
|
||||
readonly staleTime: Duration.DurationInput,
|
||||
readonly refreshOnWindowFocus: boolean,
|
||||
|
||||
readonly latestKey: SubscriptionRef.SubscriptionRef<Option.Option<K>>,
|
||||
readonly fiber: SubscriptionRef.SubscriptionRef<Option.Option<Fiber.Fiber<A, E>>>,
|
||||
@@ -59,17 +61,22 @@ extends Pipeable.Class() implements Query<K, A, E, R, P> {
|
||||
}
|
||||
|
||||
get run(): Effect.Effect<void> {
|
||||
return Stream.runForEach(this.key, key => this.interrupt.pipe(
|
||||
Effect.andThen(SubscriptionRef.set(this.latestKey, Option.some(key))),
|
||||
Effect.andThen(this.latestFinalResult),
|
||||
Effect.andThen(previous => this.startCached(key, Option.isSome(previous)
|
||||
? Result.willFetch(previous.value) as Result.Final<A, E, P>
|
||||
: Result.initial()
|
||||
)),
|
||||
Effect.andThen(sub => Effect.forkScoped(this.watch(key, sub))),
|
||||
)).pipe(
|
||||
return Effect.all([
|
||||
Stream.runForEach(this.key, key => this.fetchSubscribable(key)),
|
||||
|
||||
Effect.promise(() => import("@effect/platform-browser")).pipe(
|
||||
Effect.andThen(({ BrowserStream }) => this.refreshOnWindowFocus
|
||||
? Stream.runForEach(
|
||||
BrowserStream.fromEventListenerWindow("focus"),
|
||||
() => this.refreshSubscribable,
|
||||
)
|
||||
: Effect.void
|
||||
),
|
||||
Effect.catchAllDefect(() => Effect.void),
|
||||
),
|
||||
], { concurrency: "unbounded" }).pipe(
|
||||
Effect.ignore,
|
||||
this.runSemaphore.withPermits(1),
|
||||
Effect.provide(this.context),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -269,6 +276,7 @@ export declare namespace make {
|
||||
readonly f: (key: NoInfer<K>) => Effect.Effect<A, E, Result.forkEffect.InputContext<R, NoInfer<P>>>
|
||||
readonly initialProgress?: P
|
||||
readonly staleTime?: Duration.DurationInput
|
||||
readonly refreshOnWindowFocus?: boolean
|
||||
}
|
||||
}
|
||||
|
||||
@@ -288,6 +296,7 @@ export const make = Effect.fnUntraced(function* <K extends Query.AnyKey, A, E =
|
||||
options.initialProgress as P,
|
||||
|
||||
options.staleTime ?? client.defaultStaleTime,
|
||||
options.refreshOnWindowFocus ?? client.defaultRefreshOnWindowFocus,
|
||||
|
||||
yield* SubscriptionRef.make(Option.none<K>()),
|
||||
yield* SubscriptionRef.make(Option.none<Fiber.Fiber<A, E>>()),
|
||||
|
||||
@@ -11,6 +11,7 @@ export interface QueryClientService extends Pipeable.Pipeable {
|
||||
readonly cache: SubscriptionRef.SubscriptionRef<HashMap.HashMap<QueryClientCacheKey, QueryClientCacheEntry>>
|
||||
readonly gcTime: Duration.DurationInput
|
||||
readonly defaultStaleTime: Duration.DurationInput
|
||||
readonly defaultRefreshOnWindowFocus: boolean
|
||||
}
|
||||
|
||||
export class QueryClient extends Effect.Service<QueryClient>()("@effect-fc/QueryClient/QueryClient", {
|
||||
@@ -26,6 +27,7 @@ implements QueryClientService {
|
||||
readonly cache: SubscriptionRef.SubscriptionRef<HashMap.HashMap<QueryClientCacheKey, QueryClientCacheEntry>>,
|
||||
readonly gcTime: Duration.DurationInput,
|
||||
readonly defaultStaleTime: Duration.DurationInput,
|
||||
readonly defaultRefreshOnWindowFocus: boolean,
|
||||
readonly runSemaphore: Effect.Semaphore,
|
||||
) {
|
||||
super()
|
||||
@@ -38,6 +40,7 @@ export declare namespace make {
|
||||
export interface Options {
|
||||
readonly gcTime?: Duration.DurationInput
|
||||
readonly defaultStaleTime?: Duration.DurationInput
|
||||
readonly defaultRefreshOnWindowFocus?: boolean
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +49,7 @@ export const make = Effect.fnUntraced(function* (options: make.Options = {}): Ef
|
||||
yield* SubscriptionRef.make(HashMap.empty<QueryClientCacheKey, QueryClientCacheEntry>()),
|
||||
options.gcTime ?? "5 minutes",
|
||||
options.defaultStaleTime ?? "0 minutes",
|
||||
options.defaultRefreshOnWindowFocus ?? true,
|
||||
yield* Effect.makeSemaphore(1),
|
||||
)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user