@@ -1,4 +1,4 @@
|
||||
import { type DateTime, type Duration, Effect, Equal, Equivalence, Hash, HashMap, Pipeable, Predicate, Ref, type Scope } from "effect"
|
||||
import { type DateTime, type Duration, Effect, Equal, Equivalence, Hash, HashMap, Pipeable, Predicate, type Scope, SubscriptionRef } from "effect"
|
||||
import type * as Query from "./Query.js"
|
||||
import type * as Result from "./Result.js"
|
||||
|
||||
@@ -8,8 +8,9 @@ export type QueryClientServiceTypeId = typeof QueryClientServiceTypeId
|
||||
|
||||
export interface QueryClientService extends Pipeable.Pipeable {
|
||||
readonly [QueryClientServiceTypeId]: QueryClientServiceTypeId
|
||||
readonly cache: Ref.Ref<HashMap.HashMap<QueryClientCacheKey, QueryClientCacheEntry>>
|
||||
readonly defaultTtl: Duration.DurationInput
|
||||
readonly cache: SubscriptionRef.SubscriptionRef<HashMap.HashMap<QueryClientCacheKey, QueryClientCacheEntry>>
|
||||
readonly gcTime: Duration.DurationInput
|
||||
readonly defaultStaleTime: Duration.DurationInput
|
||||
}
|
||||
|
||||
export class QueryClient extends Effect.Service<QueryClient>()("@effect-fc/QueryClient/QueryClient", {
|
||||
@@ -22,8 +23,10 @@ implements QueryClientService {
|
||||
readonly [QueryClientServiceTypeId]: QueryClientServiceTypeId = QueryClientServiceTypeId
|
||||
|
||||
constructor(
|
||||
readonly cache: Ref.Ref<HashMap.HashMap<QueryClientCacheKey, QueryClientCacheEntry>>,
|
||||
readonly defaultTtl: Duration.DurationInput,
|
||||
readonly cache: SubscriptionRef.SubscriptionRef<HashMap.HashMap<QueryClientCacheKey, QueryClientCacheEntry>>,
|
||||
readonly gcTime: Duration.DurationInput,
|
||||
readonly defaultStaleTime: Duration.DurationInput,
|
||||
readonly runSemaphore: Effect.Semaphore,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
@@ -33,14 +36,17 @@ export const isQueryClientService = (u: unknown): u is QueryClientService => Pre
|
||||
|
||||
export declare namespace make {
|
||||
export interface Options {
|
||||
readonly defaultTtl?: Duration.DurationInput
|
||||
readonly gcTime?: Duration.DurationInput
|
||||
readonly defaultStaleTime?: Duration.DurationInput
|
||||
}
|
||||
}
|
||||
|
||||
export const make = Effect.fnUntraced(function* (options: make.Options = {}): Effect.fn.Return<QueryClientService> {
|
||||
return new QueryClientServiceImpl(
|
||||
yield* Ref.make(HashMap.empty<QueryClientCacheKey, QueryClientCacheEntry>()),
|
||||
options.defaultTtl ?? "5 minutes",
|
||||
yield* SubscriptionRef.make(HashMap.empty<QueryClientCacheKey, QueryClientCacheEntry>()),
|
||||
options.gcTime ?? "5 minutes",
|
||||
options.defaultStaleTime ?? "0 minutes",
|
||||
yield* Effect.makeSemaphore(1),
|
||||
)
|
||||
})
|
||||
|
||||
@@ -63,6 +69,7 @@ export class QueryClientCacheKey
|
||||
extends Pipeable.Class()
|
||||
implements Pipeable.Pipeable, Equal.Equal {
|
||||
readonly [QueryClientCacheKeyTypeId]: QueryClientCacheKeyTypeId = QueryClientCacheKeyTypeId
|
||||
|
||||
constructor(
|
||||
readonly key: Query.Query.AnyKey,
|
||||
readonly f: (key: Query.Query.AnyKey) => Effect.Effect<unknown, unknown, unknown>,
|
||||
@@ -90,9 +97,8 @@ implements Pipeable.Pipeable {
|
||||
readonly [QueryClientCacheEntryTypeId]: QueryClientCacheEntryTypeId = QueryClientCacheEntryTypeId
|
||||
|
||||
constructor(
|
||||
readonly at: DateTime.DateTime,
|
||||
readonly ttl: Duration.DurationInput,
|
||||
readonly result: Result.Final<unknown, unknown, unknown>,
|
||||
readonly createdAt: DateTime.DateTime,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user