// import "./EffectOpt.ext.js" import "./builtIn.js" import "./date.js" import "./Lens.ext.js" import "./Ref.js" import "./refinements.js" import "./Schema.ext.js" import type { Option } from "@fp-ts/data/Option" export type _R> = [T] extends [ Effect ] ? R : never export type _E> = [T] extends [ Effect ] ? E : never /** * @tsplus fluent fp-ts/data/Option encaseInEffect */ export function encaseMaybeInEffect_( o: Option, onError: LazyArg ): Effect { return o.match(() => Effect.fail(onError()), Effect.succeed) } /** * @tsplus getter fp-ts/data/Either asEffect */ export const EitherasEffect = Effect.fromEither /** * @tsplus fluent fp-ts/data/Option encaseInEither */ export function encaseMaybeEither_( o: Option, onError: LazyArg ): Either { return o.match(() => Either.left(onError()), Either.right) } type Service = T extends Tag ? S : never type Values = T extends { [s: string]: infer S } ? Service : never type Services>> = { [key in keyof T]: Service } /** * @tsplus static effect/io/Effect.Ops servicesWith */ export function accessServices_>, A>( services: T, fn: (services: Services) => A ) { return (Effect.struct( services.$$.keys.reduce((prev, cur) => { prev[cur] = Effect.service(services[cur]!) return prev }, {} as any) ) as any as Effect, never, Services>).map(fn) } /** * @tsplus static effect/io/Effect.Ops servicesWithEffect */ export function accessServicesM_>, R, E, A>( services: T, fn: (services: Services) => Effect ) { return (Effect.struct( services.$$.keys.reduce((prev, cur) => { prev[cur] = Effect.service(services[cur]!) return prev }, {} as any) ) as any as Effect, never, Services>).flatMap(fn) } export function accessServices>>(services: T) { return (fn: (services: Services) => A) => (Effect.struct( services.$$.keys.reduce((prev, cur) => { prev[cur] = Effect.service(services[cur]!) return prev }, {} as any) ) as any as Effect, never, Services>).map(fn) } export function accessServicesM>>(services: T) { return (fn: (services: Services) => Effect) => (Effect.struct( services.$$.keys.reduce((prev, cur) => { prev[cur] = Effect.service(services[cur]!) return prev }, {} as any) ) as any as Effect, never, Services>).flatMap(fn) } /** * @tsplus getter effect/io/Effect toNullable */ export function toNullable( self: Effect> ) { return self.map(_ => _.getOrNull) } /** * @tsplus fluent effect/io/Effect scope */ export function scope( scopedEffect: Effect, effect: Effect ): Effect, E | E2, A2> { return scopedEffect.zipRight(effect).scoped } /** * @tsplus fluent effect/io/Effect flatMapScoped */ export function flatMapScoped( scopedEffect: Effect, effect: (a: A) => Effect ): Effect, E | E2, A2> { return scopedEffect.flatMap(effect).scoped } // /** // * @tsplus fluent effect/io/Effect withScoped // */ // export function withScoped( // effect: Effect, // scopedEffect: Effect // ): Effect, E | E2, A2> { // return scopedEffect.zipRight(effect).scoped // } // /** // * @tsplus fluent effect/io/Effect withScoped // */ // export function withScopedFlatMap( // effect: (a: A) => Effect, // scopedEffect: Effect // ): Effect, E | E2, A2> { // return scopedEffect.flatMap(effect).scoped // } /** * Recovers from all errors. * * @tsplus static effect/io/Effect.Aspects catchAllMap * @tsplus pipeable effect/io/Effect catchAllMap */ export function catchAllMap(f: (e: E) => A2) { return (self: Effect): Effect => self.catchAll(err => Effect(f(err))) } /** * Annotates each log in this effect with the specified log annotations. * @tsplus static effect/io/Effect.Ops logAnnotates */ export function logAnnotates(kvps: Record) { return (effect: Effect): Effect => FiberRef.currentLogAnnotations .get .flatMap(annotations => Effect.suspendSucceed(() => pipe( effect, FiberRef.locally(FiberRef.currentLogAnnotations)( HashMap.from([...annotations, ...kvps.$$.entries]) ) ) ) ) } /** * Annotates each log in this scope with the specified log annotation. * * @tsplus static effect/io/Effect.Ops logAnnotateScoped */ export function logAnnotateScoped(key: string, value: string) { return FiberRef.currentLogAnnotations .get .flatMap(annotations => Effect.suspendSucceed(() => annotations.set(key, value)["|>"]( FiberRef.locallyScoped(FiberRef.currentLogAnnotations) ) ) ) } /** * Annotates each log in this scope with the specified log annotations. * * @tsplus static effect/io/Effect.Ops logAnnotatesScoped */ export function logAnnotatesScoped(kvps: Record) { return FiberRef.currentLogAnnotations .get .flatMap(annotations => Effect.suspendSucceed(() => HashMap.from([...annotations, ...kvps.$$.entries])["|>"]( FiberRef.locallyScoped(FiberRef.currentLogAnnotations) ) ) ) } /** * @tsplus fluent function flow */ export function flow(f: (a: A) => B, g: (b: B) => C): (a: A) => C { return a => g(f(a)) }