@@ -1,5 +1,5 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Chunk, Context, Effect, Layer, Option, SubscriptionRef } from "effect"
|
||||
import { Chunk, Context, Effect, Option, SubscriptionRef } from "effect"
|
||||
import * as Lens from "./Lens.js"
|
||||
|
||||
|
||||
@@ -56,18 +56,18 @@ describe("Lens", () => {
|
||||
expect(result[1]).toEqual(Option.some(50)) // 100 / 2
|
||||
})
|
||||
|
||||
test("provide supplies a service to get and modify", async () => {
|
||||
test("provideContext supplies a service to get and modify", async () => {
|
||||
const result = await Effect.runPromise(
|
||||
Effect.flatMap(
|
||||
SubscriptionRef.make(10),
|
||||
parent => {
|
||||
const lens = Lens.provide(
|
||||
const lens = Lens.provideContext(
|
||||
Lens.mapEffect(
|
||||
Lens.fromSubscriptionRef(parent),
|
||||
n => Effect.map(Offset, ({ value }) => n + value),
|
||||
(_n, next) => Effect.map(Offset, ({ value }) => next - value),
|
||||
),
|
||||
Layer.succeed(Offset, { value: 5 }),
|
||||
Context.make(Offset, { value: 5 }),
|
||||
)
|
||||
|
||||
return Effect.flatMap(
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Array, Chunk, type Context, Effect, Function, identity, type Layer, type ManagedRuntime, Option, Pipeable, Predicate, Readable, type Runtime, Stream, type SubscriptionRef, type SynchronizedRef } from "effect"
|
||||
import { Array, Chunk, type Context, Effect, Function, identity, type ManagedRuntime, Option, Pipeable, Predicate, Readable, type Runtime, Stream, type SubscriptionRef, type SynchronizedRef } from "effect"
|
||||
import type { NoSuchElementException } from "effect/Cause"
|
||||
import * as Subscribable from "./Subscribable.js"
|
||||
|
||||
@@ -269,54 +269,41 @@ export const mapStream: {
|
||||
get modify() { return self.modify },
|
||||
}))
|
||||
|
||||
|
||||
/**
|
||||
* Provides the requirements of a `Lens`, removing them from both the read and write environments.
|
||||
*
|
||||
* Accepts the same provider forms as `Effect.provide`: one or more `Layer`s, a `Context`,
|
||||
* a `Runtime`, or a `ManagedRuntime`.
|
||||
*
|
||||
* Layer providers may add their construction errors to both the read and write error channels.
|
||||
* Provides a `Context` to a `Lens`, removing it from both the read and write environments.
|
||||
*/
|
||||
export const provide: {
|
||||
<const Layers extends readonly [Layer.Layer.Any, ...Array<Layer.Layer.Any>]>(
|
||||
layers: Layers,
|
||||
): <A, ER, EW, RR, RW>(self: Lens<A, ER, EW, RR, RW>) => Lens<
|
||||
A,
|
||||
ER | { [k in keyof Layers]: Layer.Layer.Error<Layers[k]> }[number],
|
||||
EW | { [k in keyof Layers]: Layer.Layer.Error<Layers[k]> }[number],
|
||||
{ [k in keyof Layers]: Layer.Layer.Context<Layers[k]> }[number] | Exclude<RR, { [k in keyof Layers]: Layer.Layer.Success<Layers[k]> }[number]>,
|
||||
{ [k in keyof Layers]: Layer.Layer.Context<Layers[k]> }[number] | Exclude<RW, { [k in keyof Layers]: Layer.Layer.Success<Layers[k]> }[number]>
|
||||
>
|
||||
<ROut, E2, RIn>(
|
||||
layer: Layer.Layer<ROut, E2, RIn>,
|
||||
): <A, ER, EW, RR, RW>(self: Lens<A, ER, EW, RR, RW>) => Lens<A, ER | E2, EW | E2, RIn | Exclude<RR, ROut>, RIn | Exclude<RW, ROut>>
|
||||
export const provideContext: {
|
||||
<A, ER, EW, RR, RW, R2>(
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
context: Context.Context<R2>,
|
||||
): Lens<A, ER, EW, Exclude<RR, R2>, Exclude<RW, R2>>
|
||||
<R2>(
|
||||
context: Context.Context<R2>,
|
||||
): <A, ER, EW, RR, RW>(self: Lens<A, ER, EW, RR, RW>) => Lens<A, ER, EW, Exclude<RR, R2>, Exclude<RW, R2>>
|
||||
} = Function.dual(2, <A, ER, EW, RR, RW, R2>(
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
context: Context.Context<R2>,
|
||||
): Lens<A, ER, EW, Exclude<RR, R2>, Exclude<RW, R2>> => make({
|
||||
get get() { return Effect.provide(self.get, context) },
|
||||
get changes() { return Stream.provideSomeContext(self.changes, context) },
|
||||
modify: <B, E1 = never, R1 = never>(
|
||||
f: (a: A) => Effect.Effect<readonly [B, A], E1, R1>
|
||||
) => Effect.provide(self.modify(f), context),
|
||||
}))
|
||||
|
||||
/**
|
||||
* Provides a `Runtime` or `ManagedRuntime` to a `Lens`, removing it from both the read and write environments.
|
||||
*
|
||||
* `ManagedRuntime` may add its construction errors to both the read and write error channels.
|
||||
*/
|
||||
export const provideRuntime: {
|
||||
<R2>(
|
||||
runtime: Runtime.Runtime<R2>,
|
||||
): <A, ER, EW, RR, RW>(self: Lens<A, ER, EW, RR, RW>) => Lens<A, ER, EW, Exclude<RR, R2>, Exclude<RW, R2>>
|
||||
<E2, R2>(
|
||||
managedRuntime: ManagedRuntime.ManagedRuntime<R2, E2>,
|
||||
): <A, ER, EW, RR, RW>(self: Lens<A, ER, EW, RR, RW>) => Lens<A, ER | E2, EW | E2, Exclude<RR, R2>, Exclude<RW, R2>>
|
||||
<A, ER, EW, RR, RW, const Layers extends readonly [Layer.Layer.Any, ...Array<Layer.Layer.Any>]>(
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
layers: Layers,
|
||||
): Lens<
|
||||
A,
|
||||
ER | { [k in keyof Layers]: Layer.Layer.Error<Layers[k]> }[number],
|
||||
EW | { [k in keyof Layers]: Layer.Layer.Error<Layers[k]> }[number],
|
||||
{ [k in keyof Layers]: Layer.Layer.Context<Layers[k]> }[number] | Exclude<RR, { [k in keyof Layers]: Layer.Layer.Success<Layers[k]> }[number]>,
|
||||
{ [k in keyof Layers]: Layer.Layer.Context<Layers[k]> }[number] | Exclude<RW, { [k in keyof Layers]: Layer.Layer.Success<Layers[k]> }[number]>
|
||||
>
|
||||
<A, ER, EW, RR, RW, ROut, E2, RIn>(
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
layer: Layer.Layer<ROut, E2, RIn>,
|
||||
): Lens<A, ER | E2, EW | E2, RIn | Exclude<RR, ROut>, RIn | Exclude<RW, ROut>>
|
||||
<A, ER, EW, RR, RW, R2>(
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
context: Context.Context<R2>,
|
||||
): Lens<A, ER, EW, Exclude<RR, R2>, Exclude<RW, R2>>
|
||||
<A, ER, EW, RR, RW, R2>(
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
runtime: Runtime.Runtime<R2>,
|
||||
@@ -325,22 +312,20 @@ export const provide: {
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
runtime: ManagedRuntime.ManagedRuntime<R2, E2>,
|
||||
): Lens<A, ER | E2, EW | E2, Exclude<RR, R2>, Exclude<RW, R2>>
|
||||
} = Function.dual(2, <A, ER, EW, RR, RW, ROut, E2, RIn>(
|
||||
} = Function.dual(2, <A, ER, EW, RR, RW, E2, R2>(
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
layer: Layer.Layer<ROut, E2, RIn>,
|
||||
): Lens<A, ER | E2, EW | E2, RIn | Exclude<RR, ROut>, RIn | Exclude<RW, ROut>> => make({
|
||||
get get() { return Effect.provide(self.get, layer) },
|
||||
runtime: Runtime.Runtime<R2>,
|
||||
) => make<A, ER | E2, EW | E2, Exclude<RR, R2>, Exclude<RW, R2>>({
|
||||
get get() { return Effect.provide(self.get, runtime) },
|
||||
get changes() {
|
||||
return Stream.unwrapScoped(
|
||||
Effect.map(
|
||||
Effect.provide(Effect.context<RR>(), layer),
|
||||
return Stream.unwrap(Effect.map(
|
||||
Effect.provide(Effect.context<RR>(), runtime),
|
||||
context => Stream.provideContext(self.changes, context),
|
||||
),
|
||||
)
|
||||
))
|
||||
},
|
||||
modify: <B, E1 = never, R1 = never>(
|
||||
f: (a: A) => Effect.Effect<readonly [B, A], E1, R1>
|
||||
) => Effect.provide(self.modify(f), layer),
|
||||
) => Effect.provide(self.modify(f), runtime),
|
||||
}))
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user