From ecb8aeeb7c09aaa8dfbf50996bb3e84f43c19824 Mon Sep 17 00:00:00 2001 From: Michael Arnaldi Date: Sat, 7 Nov 2020 12:13:40 +0000 Subject: [PATCH] add methods --- packages/effect/src/Managed/core.ts | 7 ++++ packages/effect/src/Managed/methods/api.ts | 49 +++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/packages/effect/src/Managed/core.ts b/packages/effect/src/Managed/core.ts index 2c52c8197e..9bffb9f767 100644 --- a/packages/effect/src/Managed/core.ts +++ b/packages/effect/src/Managed/core.ts @@ -583,6 +583,13 @@ export function provideSome_( ) } +/** + * Like provideSome for effect but for Managed + */ +export function provideSome(f: (r0: R0) => R) { + return (self: Managed) => provideSome_(self, f) +} + /** * A `Reservation< R, E, A>` encapsulates resource acquisition and disposal * without specifying when or how that resource might be used. diff --git a/packages/effect/src/Managed/methods/api.ts b/packages/effect/src/Managed/methods/api.ts index 0f94f19387..6569650ad0 100644 --- a/packages/effect/src/Managed/methods/api.ts +++ b/packages/effect/src/Managed/methods/api.ts @@ -4,13 +4,14 @@ import * as E from "../../Either" import { flow, identity, pipe, tuple } from "../../Function" import * as O from "../../Option" import * as P from "../../Promise" -import { chain_, fail, foldCauseM_, map_, mapM_ } from "../core" +import { chain_, fail, foldCauseM_, map_, mapM_, provideSome_ } from "../core" import { fromEffect } from "../fromEffect" import type { UIO } from "../managed" import { Managed } from "../managed" import { succeed } from "../succeed" import { absolve } from "./absolve" import { foldM_ } from "./foldM_" +import { gen } from "./gen" import { halt } from "./halt" import { releaseMap } from "./releaseMap" import { suspend } from "./suspend" @@ -365,3 +366,49 @@ export function collect_( export function collect(e: E1, pf: (a: A) => O.Option) { return (self: Managed) => collect_(self, e, pf) } + +/** + * Provides the `Managed` effect with its required environment, which eliminates + * its dependency on `R`. + */ +export function provideAll(r: R) { + return (self: Managed) => provideAll_(self, r) +} + +/** + * Provides the `Managed` effect with its required environment, which eliminates + * its dependency on `R`. + */ +export function provideAll_(self: Managed, r: R) { + return provideSome_(self, () => r) +} + +/** + * Accesses the whole environment of the effect. + */ +export function environment() { + return fromEffect(T.environment()) +} + +/** + * Executes the second effect and then provides its output as an environment to this effect + */ +export function compose(that: Managed) { + return (self: Managed) => + gen(function* (_) { + const r1 = yield* _(environment()) + const r = yield* _(provideAll(r1)(self)) + + return yield* _(provideAll(r)(that)) + }) +} + +/** + * Returns an effect whose failure and success have been lifted into an + * `Either`. The resulting effect cannot fail + */ +export function either( + self: Managed +): Managed> { + return fold_(self, E.left, E.right) +}