import { Context, Effect, identity, Layer } from "effect" import type { Mutable } from "effect/Types" import * as QueryErrorHandler from "./QueryErrorHandler.js" export interface QueryClient { readonly errorHandler: QueryErrorHandler.QueryErrorHandler } export interface MakeProps { readonly errorHandler: QueryErrorHandler.QueryErrorHandler } export const make = ( { errorHandler }: MakeProps ): Effect.Effect> => Effect.Do.pipe( Effect.let("errorHandler", () => errorHandler) ) const id = "@reffuse/extension-query/QueryClient" export type TagClassShape = Context.TagClassShape> export type GenericTagClass = Context.TagClass< TagClassShape, typeof id, QueryClient > export const makeGenericTagClass = (): GenericTagClass => Context.Tag(id)() export interface ServiceProps { readonly errorHandler?: Effect.Effect, E, R> } export interface ServiceResult extends Context.TagClass< Self, typeof id, QueryClient > { readonly Default: Layer.Layer } export const Service = () => ( ( props?: ServiceProps ): ServiceResult => { const TagClass = Context.Tag(id)() as ServiceResult (TagClass as Mutable).Default = Layer.effect(TagClass, Effect.flatMap( props?.errorHandler ?? QueryErrorHandler.make()(identity), errorHandler => make({ errorHandler }), )) return TagClass } )