0.2.5 #43
@@ -5,34 +5,38 @@ export const LensTypeId: unique symbol = Symbol.for("@effect-fc/Lens/Lens")
|
|||||||
export type LensTypeId = typeof LensTypeId
|
export type LensTypeId = typeof LensTypeId
|
||||||
|
|
||||||
export interface Lens<in out A, in out ER = never, in out RR = never, in out EW = never, in out RW = never>
|
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> {
|
extends Subscribable.Subscribable<A, ER, RR> {
|
||||||
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 [LensTypeId]: LensTypeId
|
||||||
|
|
||||||
|
readonly set: (a: A) => Effect.Effect<void, EW, RW>
|
||||||
readonly modify: <B, E1 = never, R1 = never>(
|
readonly modify: <B, E1 = never, R1 = never>(
|
||||||
f: (a: A) => Effect.Effect<readonly [B, A], E1, R1>
|
f: (a: A) => Effect.Effect<readonly [B, A], E1, R1>
|
||||||
) => Effect.Effect<B, ER | EW | E1, RR | RW | R1>
|
) => Effect.Effect<B, ER | EW | E1, RR | RW | R1>
|
||||||
}
|
}
|
||||||
|
|
||||||
export const LensPrototype: LensPrototype<any, any, any, any, any> = Object.freeze({
|
export class LensImpl<in out A, in out ER = never, in out RR = never, in out EW = never, in out RW = never>
|
||||||
...Pipeable.Prototype,
|
extends Pipeable.Class() implements Lens<A, ER, RR, EW, RW> {
|
||||||
[Readable.TypeId]: Readable.TypeId,
|
readonly [Readable.TypeId]: Readable.TypeId = Readable.TypeId
|
||||||
[Subscribable.TypeId]: Subscribable.TypeId,
|
readonly [Subscribable.TypeId]: Subscribable.TypeId = Subscribable.TypeId
|
||||||
[LensTypeId]: LensTypeId,
|
readonly [LensTypeId]: LensTypeId = LensTypeId
|
||||||
|
|
||||||
modify<A, ER, RR, EW, RW, B, E1 = never, R1 = never>(
|
constructor(
|
||||||
this: Lens<A, ER, RR, EW, RW>,
|
readonly get: Effect.Effect<A, ER, RR>,
|
||||||
f: (a: A) => Effect.Effect<readonly [B, A], E1, R1>,
|
readonly changes: Stream.Stream<A, ER, RR>,
|
||||||
|
readonly set: (a: A) => Effect.Effect<void, EW, RW>,
|
||||||
|
) {
|
||||||
|
super()
|
||||||
|
}
|
||||||
|
|
||||||
|
modify<B, E1 = never, R1 = never>(
|
||||||
|
f: (a: A) => Effect.Effect<readonly [B, A], E1, R1>
|
||||||
) {
|
) {
|
||||||
return Effect.flatMap(
|
return Effect.flatMap(
|
||||||
this.get,
|
this.get,
|
||||||
a => Effect.flatMap(f(a), ([b, next]) => Effect.as(this.set(next), b)
|
a => Effect.flatMap(f(a), ([b, next]) => Effect.as(this.set(next), b)
|
||||||
))
|
))
|
||||||
},
|
}
|
||||||
} as const)
|
}
|
||||||
|
|
||||||
|
|
||||||
export const isLens = (u: unknown): u is Lens<unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, LensTypeId)
|
export const isLens = (u: unknown): u is Lens<unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, LensTypeId)
|
||||||
@@ -43,7 +47,7 @@ export const make = <A, ER, RR, EW, RW>(
|
|||||||
readonly changes: Stream.Stream<A, ER, RR>
|
readonly changes: Stream.Stream<A, ER, RR>
|
||||||
readonly set: (a: A) => Effect.Effect<void, EW, RW>
|
readonly set: (a: A) => Effect.Effect<void, EW, RW>
|
||||||
}
|
}
|
||||||
): Lens<A, ER, RR, EW, RW> => Object.setPrototypeOf({ ...options }, LensPrototype)
|
): Lens<A, ER, RR, EW, RW> => new LensImpl(options.get, options.changes, options.set)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a `Lens` that directly proxies a `SubscriptionRef`.
|
* Creates a `Lens` that directly proxies a `SubscriptionRef`.
|
||||||
|
|||||||
Reference in New Issue
Block a user