/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/ban-types */ /** * We're doing the long way around here with assignTag, TagBase & TagBaseTagged, * because there's a typescript compiler issue where it will complain about Equal.symbol, and Hash.symbol not being accessible. * https://github.com/microsoft/TypeScript/issues/52644 */ import type { TagTypeId as TagTypeIdOriginal } from "effect/Context" export const ServiceTag = Symbol() export type ServiceTag = typeof ServiceTag export abstract class PhantomTypeParameter { protected abstract readonly [ServiceTag]: { readonly [NameP in Identifier]: (_: InstantiatedType) => InstantiatedType } } /** * @tsplus type ServiceTagged */ export abstract class ServiceTagged extends PhantomTypeParameter {} /** * @tsplus static ServiceTagged make */ export function makeService>(_: Omit) { return _ as T } /** * @tsplus fluent effect/data/Context/Tag make */ export function make, I = T>(_: Tag, t: Omit) { return t as T } export const TagTypeId: TagTypeIdOriginal = Symbol.for("effect/Context/Tag") as unknown as TagTypeIdOriginal export type TagTypeId = typeof TagTypeId let i = 0 const randomId = () => "unknown-service-" + i++ export function assignTag(key?: string) { return (cls: S): S & Tag => { const tag = GenericTag(key ?? randomId()) const t = Object.assign(cls, Object.getPrototypeOf(tag), tag) const limit = Error.stackTraceLimit Error.stackTraceLimit = 4 // TODO const creationError = new Error() Error.stackTraceLimit = limit // the stack is used to get the location of the tag definition, if a service is not found in the registry Object.defineProperty(t, "stack", { get() { // remove one line as assignTag is generally used inside a class constructor function return creationError.stack?.split("\n").slice(1).join("\n") } }) return t } } export function TagClass(key?: string) { const c: { new(service: ServiceImpl): Readonly } = class { constructor(service: ServiceImpl) { Object.assign(this, service) } // static readonly Id: Id } as any return assignTag(key)(c) } export const TagClassMake = () => ( make: Effect, key?: string ) => { const c: { new(service: ServiceImpl): Readonly toLayer: () => Layer toLayerScoped: () => Layer> } = class { constructor(service: ServiceImpl) { Object.assign(this, service) } // works around an issue where defining layer on the class messes up and causes the Tag to infer to `any, any` :/ static toLayer() { return make.toLayer(this as any) } static toLayerScoped() { return make.toLayerScoped(this as any) } // eslint-disable-next-line @typescript-eslint/no-explicit-any } as any return assignTag(key)(c) } export function TagClassLegacy(key?: string) { abstract class TagClassLegacy {} return assignTag(key)(TagClassLegacy) } /** @deprecated use `Id` of TagClass for unique id */ export function ServiceTaggedClass() { return (_: Key) => { abstract class ServiceTaggedClassC { static make = (t: Omit) => { return t as Service } } return assignTag()(ServiceTaggedClassC) } }