diff --git a/packages/effect-fc/src/QueryClient.ts b/packages/effect-fc/src/QueryClient.ts index 3191348..6e92e52 100644 --- a/packages/effect-fc/src/QueryClient.ts +++ b/packages/effect-fc/src/QueryClient.ts @@ -1,4 +1,4 @@ -import { type HashMap, Pipeable, Predicate, type Subscribable, type SubscriptionRef } from "effect" +import { Equal, Equivalence, Hash, type HashMap, Pipeable, Predicate, type Ref } from "effect" import type * as Query from "./Query.js" import type * as Result from "./Result.js" @@ -9,17 +9,35 @@ export type QueryClientTypeId = typeof QueryClientTypeId export interface QueryClient extends Pipeable.Pipeable { readonly [QueryClientTypeId]: QueryClientTypeId - readonly cache: Subscribable.Subscribable>> + readonly cache: Ref.Ref>> } export class QueryClientImpl extends Pipeable.Class() implements QueryClient { readonly [QueryClientTypeId]: QueryClientTypeId = QueryClientTypeId constructor( - readonly cache: SubscriptionRef.SubscriptionRef>> + readonly cache: Ref.Ref>> ) { super() } } export const isQueryClient = (u: unknown): u is QueryClient => Predicate.hasProperty(u, QueryClientTypeId) + + +const QueryClientCacheKeyTypeId: unique symbol = Symbol.for("@effect-fc/QueryClient/QueryClientCacheKey") +type QueryClientCacheKeyTypeId = typeof QueryClientCacheKeyTypeId + +class QueryClientCacheKey implements Equal.Equal { + readonly [QueryClientCacheKeyTypeId]: QueryClientCacheKeyTypeId = QueryClientCacheKeyTypeId + constructor(readonly key: Query.Query.AnyKey) {} + + [Equal.symbol](that: Equal.Equal) { + return isQueryClientKey(that) && Equivalence.array(Equal.equivalence())(this.key, that.key) + } + [Hash.symbol]() { + return 0 + } +} + +const isQueryClientKey = (u: unknown): u is QueryClientCacheKey => Predicate.hasProperty(u, QueryClientCacheKeyTypeId)