From 7b5eaa3838c79bf4bdccf91b94d61bbc38a2ec95 Mon Sep 17 00:00:00 2001 From: Matheus Michalski Puel Date: Mon, 1 Jan 2024 05:58:49 -0300 Subject: [PATCH] make serviceFunctions accept an Effect (#1797) --- .changeset/four-zoos-happen.md | 5 +++ packages/effect/src/Effect.ts | 36 +++++++++-------- packages/effect/src/internal/core-effect.ts | 44 +++++++++++---------- 3 files changed, 47 insertions(+), 38 deletions(-) create mode 100644 .changeset/four-zoos-happen.md diff --git a/.changeset/four-zoos-happen.md b/.changeset/four-zoos-happen.md new file mode 100644 index 00000000000..12df37f0e3e --- /dev/null +++ b/.changeset/four-zoos-happen.md @@ -0,0 +1,5 @@ +--- +"effect": patch +--- + +make serviceFunctions and similar accept an Effect as the service diff --git a/packages/effect/src/Effect.ts b/packages/effect/src/Effect.ts index b44ff6edf5f..b8870d5c129 100644 --- a/packages/effect/src/Effect.ts +++ b/packages/effect/src/Effect.ts @@ -3154,29 +3154,30 @@ export const provideServiceEffect: { * @since 2.0.0 * @category context */ -export const serviceFunction: , Args extends Array, A>( - service: T, - f: (_: Context.Tag.Service) => (...args: Args) => A -) => (...args: Args) => Effect, never, A> = effect.serviceFunction +export const serviceFunction: , Args extends Array, A>( + getService: T, + f: (_: Effect.Success) => (...args: Args) => A +) => (...args: Args) => Effect, Effect.Error, A> = effect.serviceFunction /** * @since 2.0.0 * @category context */ -export const serviceFunctionEffect: , Args extends Array, R, E, A>( - service: T, - f: (_: Context.Tag.Service) => (...args: Args) => Effect -) => (...args: Args) => Effect, E, A> = effect.serviceFunctionEffect +export const serviceFunctionEffect: , Args extends Array, R, E, A>( + getService: T, + f: (_: Effect.Success) => (...args: Args) => Effect +) => (...args: Args) => Effect, E | Effect.Error, A> = effect.serviceFunctionEffect /** * @since 2.0.0 * @category context */ -export const serviceFunctions: ( - tag: Context.Tag +export const serviceFunctions: ( + getService: Effect ) => { [k in { [k in keyof S]: S[k] extends (...args: Array) => Effect ? k : never }[keyof S]]: - S[k] extends (...args: infer Args) => Effect ? (...args: Args) => Effect : + S[k] extends (...args: infer Args) => Effect ? + (...args: Args) => Effect : never } = effect.serviceFunctions @@ -3184,26 +3185,27 @@ export const serviceFunctions: ( * @since 2.0.0 * @category context */ -export const serviceConstants: ( - tag: Context.Tag +export const serviceConstants: ( + getService: Effect ) => { [k in { [k in keyof S]: S[k] extends Effect ? k : never }[keyof S]]: S[k] extends - Effect ? Effect : never + Effect ? Effect : never } = effect.serviceConstants /** * @since 2.0.0 * @category context */ -export const serviceMembers: (tag: Context.Tag) => { +export const serviceMembers: (getService: Effect) => { functions: { [k in { [k in keyof S]: S[k] extends (...args: Array) => Effect ? k : never }[keyof S]]: - S[k] extends (...args: infer Args) => Effect ? (...args: Args) => Effect : + S[k] extends (...args: infer Args) => Effect ? + (...args: Args) => Effect : never } constants: { [k in { [k in keyof S]: S[k] extends Effect ? k : never }[keyof S]]: S[k] extends - Effect ? Effect : never + Effect ? Effect : never } } = effect.serviceMembers diff --git a/packages/effect/src/internal/core-effect.ts b/packages/effect/src/internal/core-effect.ts index 70fcd4fa456..dd9db103b2c 100644 --- a/packages/effect/src/internal/core-effect.ts +++ b/packages/effect/src/internal/core-effect.ts @@ -1832,62 +1832,64 @@ export const withMetric = dual< >(2, (self, metric) => metric(self)) /** @internal */ -export const serviceFunctionEffect = , Args extends Array, R, E, A>( - service: T, - f: (_: Context.Tag.Service) => (...args: Args) => Effect.Effect +export const serviceFunctionEffect = , Args extends Array, R, E, A>( + getService: T, + f: (_: Effect.Effect.Success) => (...args: Args) => Effect.Effect ) => -(...args: Args): Effect.Effect, E, A> => core.flatMap(service, (a) => f(a)(...args)) +(...args: Args): Effect.Effect, E | Effect.Effect.Error, A> => + core.flatMap(getService, (a) => f(a)(...args)) /** @internal */ -export const serviceFunction = , Args extends Array, A>( - service: T, - f: (_: Context.Tag.Service) => (...args: Args) => A +export const serviceFunction = , Args extends Array, A>( + getService: T, + f: (_: Effect.Effect.Success) => (...args: Args) => A ) => -(...args: Args): Effect.Effect, never, A> => core.map(service, (a) => f(a)(...args)) +(...args: Args): Effect.Effect, Effect.Effect.Error, A> => + core.map(getService, (a) => f(a)(...args)) /** @internal */ -export const serviceFunctions = ( - tag: Context.Tag +export const serviceFunctions = ( + getService: Effect.Effect ): { [k in { [k in keyof S]: S[k] extends (...args: Array) => Effect.Effect ? k : never }[keyof S]]: S[k] extends (...args: infer Args) => Effect.Effect - ? (...args: Args) => Effect.Effect + ? (...args: Args) => Effect.Effect : never } => new Proxy({} as any, { get(_target: any, prop: any, _receiver) { - return (...args: Array) => core.flatMap(tag, (s: any) => s[prop](...args)) + return (...args: Array) => core.flatMap(getService, (s: any) => s[prop](...args)) } }) /** @internal */ -export const serviceConstants = ( - tag: Context.Tag +export const serviceConstants = ( + getService: Effect.Effect ): { [k in { [k in keyof S]: S[k] extends Effect.Effect ? k : never }[keyof S]]: S[k] extends - Effect.Effect ? Effect.Effect : never + Effect.Effect ? Effect.Effect : never } => new Proxy({} as any, { get(_target: any, prop: any, _receiver) { - return core.flatMap(tag, (s: any) => s[prop]) + return core.flatMap(getService, (s: any) => s[prop]) } }) /** @internal */ -export const serviceMembers = (tag: Context.Tag): { +export const serviceMembers = (getService: Effect.Effect): { functions: { [k in { [k in keyof S]: S[k] extends (...args: Array) => Effect.Effect ? k : never }[keyof S]]: S[k] extends (...args: infer Args) => Effect.Effect - ? (...args: Args) => Effect.Effect + ? (...args: Args) => Effect.Effect : never } constants: { [k in { [k in keyof S]: S[k] extends Effect.Effect ? k : never }[keyof S]]: S[k] extends - Effect.Effect ? Effect.Effect : never + Effect.Effect ? Effect.Effect : never } } => ({ - functions: serviceFunctions(tag), - constants: serviceConstants(tag) + functions: serviceFunctions(getService), + constants: serviceConstants(getService) }) /** @internal */