@@ -1,26 +1,37 @@
|
||||
import { Effect, Pipeable, Predicate, Readable, Stream, Subscribable, type SubscriptionRef } from "effect"
|
||||
import { Effect, Pipeable, Predicate, Readable, Stream, Subscribable, SubscriptionRef } from "effect"
|
||||
|
||||
|
||||
export const LensTypeId: unique symbol = Symbol.for("@effect-fc/Lens/Lens")
|
||||
export type LensTypeId = typeof LensTypeId
|
||||
|
||||
export interface Lens<in out A, ER = never, RR = never, EW = never, RW = never>
|
||||
extends Subscribable.Subscribable<A, ER, RR>, LensPrototype {
|
||||
export interface Lens<in out A, in out ER = never, in out RR = never, in out EW = never, in out RW = never>
|
||||
extends Subscribable.Subscribable<A, ER, RR>, LensPrototype<A, ER, RR, EW, RW> {
|
||||
readonly set: (a: A) => Effect.Effect<void, EW, RW>
|
||||
}
|
||||
|
||||
|
||||
export interface LensPrototype<in out A, in out ER = never, in out RR = never, in out EW = never, in out RW = never> extends Pipeable.Pipeable {
|
||||
readonly [LensTypeId]: LensTypeId
|
||||
readonly modify: <B, E1 = never, R1 = never>(
|
||||
f: (a: A) => Effect.Effect<readonly [B, A], E1, R1>
|
||||
) => Effect.Effect<B, ER | EW | E1, RR | RW | R1>
|
||||
}
|
||||
|
||||
|
||||
export interface LensPrototype extends Pipeable.Pipeable {
|
||||
readonly [LensTypeId]: LensTypeId
|
||||
}
|
||||
|
||||
export const LensPrototype: LensPrototype = Object.freeze({
|
||||
export const LensPrototype: LensPrototype<any, any, any, any, any> = Object.freeze({
|
||||
...Pipeable.Prototype,
|
||||
[Readable.TypeId]: Readable.TypeId,
|
||||
[Subscribable.TypeId]: Subscribable.TypeId,
|
||||
[LensTypeId]: LensTypeId,
|
||||
|
||||
modify<A, ER, RR, EW, RW, B, E1 = never, R1 = never>(
|
||||
this: Lens<A, ER, RR, EW, RW>,
|
||||
f: (a: A) => Effect.Effect<readonly [B, A], E1, R1>,
|
||||
) {
|
||||
return Effect.flatMap(
|
||||
this.get,
|
||||
a => Effect.flatMap(f(a), ([b, next]) => Effect.as(this.set(next), b)
|
||||
))
|
||||
},
|
||||
} as const)
|
||||
|
||||
|
||||
@@ -30,21 +41,9 @@ export const make = <A, ER, RR, EW, RW>(
|
||||
options: {
|
||||
readonly get: Effect.Effect<A, ER, RR>
|
||||
readonly changes: Stream.Stream<A, ER, RR>
|
||||
} & (
|
||||
| { readonly set: (value: A) => Effect.Effect<void, EW, RW> }
|
||||
| { readonly modify: <B, E1 = never, R1 = never>(f: (a: A) => Effect.Effect<readonly [B, A], E1, R1>) => Effect.Effect<B, ER | EW | E1, RR | RW | R1> }
|
||||
)
|
||||
): Lens<A, ER, RR, EW, RW> => Object.setPrototypeOf({
|
||||
get: options.get,
|
||||
changes: options.changes,
|
||||
modify: Predicate.hasProperty(options, "modify")
|
||||
? options.modify
|
||||
: <B, E1 = never, R1 = never>(f: (a: A) => Effect.Effect<readonly [B, A], E1, R1>) =>
|
||||
Effect.flatMap(options.get, a =>
|
||||
Effect.flatMap(f(a), ([b, next]) => Effect.as(options.set(next), b)
|
||||
)
|
||||
),
|
||||
}, LensPrototype)
|
||||
readonly set: (a: A) => Effect.Effect<void, EW, RW>
|
||||
}
|
||||
): Lens<A, ER, RR, EW, RW> => Object.setPrototypeOf({ ...options }, LensPrototype)
|
||||
|
||||
/**
|
||||
* Creates a `Lens` that directly proxies a `SubscriptionRef`.
|
||||
@@ -53,8 +52,8 @@ export const fromSubscriptionRef = <A>(
|
||||
ref: SubscriptionRef.SubscriptionRef<A>
|
||||
): Lens<A, never, never, never, never> => make({
|
||||
get: ref.get,
|
||||
changes: ref.changes,
|
||||
modify: ref.modifyEffect,
|
||||
get changes() { return ref.changes },
|
||||
set: a => SubscriptionRef.set(ref, a),
|
||||
})
|
||||
|
||||
export const unwrap = <A, ER, RR, EW, RW, E1, R1>(
|
||||
@@ -62,7 +61,5 @@ export const unwrap = <A, ER, RR, EW, RW, E1, R1>(
|
||||
): Lens<A, ER | E1, RR | R1, EW | E1, RW | R1> => make({
|
||||
get: Effect.flatMap(effect, l => l.get),
|
||||
changes: Stream.unwrap(Effect.map(effect, l => l.changes)),
|
||||
modify: <B, E2 = never, R2 = never>(
|
||||
f: (a: A) => Effect.Effect<readonly [B, A], E2, R2>
|
||||
) => Effect.flatMap(effect, l => l.modify(f)),
|
||||
set: a => Effect.flatMap(effect, l => l.set(a)),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user