This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Effect, Pipeable, Predicate, Readable, Stream, Subscribable, SubscriptionRef } from "effect"
|
||||
import { Effect, Pipeable, Predicate, Readable, Stream, Subscribable, type SubscriptionRef } from "effect"
|
||||
import * as Writable from "./Writable.js"
|
||||
|
||||
|
||||
@@ -24,17 +24,23 @@ export const LensPrototype: LensPrototype = Object.freeze({
|
||||
|
||||
export const isLens = (u: unknown): u is Lens<unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, LensTypeId)
|
||||
|
||||
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>
|
||||
}): Lens<A, ER, RR, EW, RW> => Object.setPrototypeOf({
|
||||
export const make = <A, ER, RR, EW, RW>(
|
||||
options: {
|
||||
readonly get: Effect.Effect<A, ER, RR>
|
||||
readonly changes: Stream.Stream<A, ER, RR>
|
||||
} & (
|
||||
| { readonly modify: <B>(f: (a: A) => readonly [B, A]) => Effect.Effect<B, EW, RW> }
|
||||
| { readonly set: (value: A) => Effect.Effect<void, EW, RW> }
|
||||
)
|
||||
): Lens<A, ER, RR, EW, RW> => Object.setPrototypeOf({
|
||||
get: options.get,
|
||||
changes: options.changes,
|
||||
modify: <B>(f: (a: A) => readonly [B, A]) => Effect.flatMap(options.get, a => {
|
||||
const [b, next] = f(a)
|
||||
return Effect.map(options.set(next), () => b)
|
||||
}),
|
||||
modify: Predicate.hasProperty(options, "modify")
|
||||
? options.modify
|
||||
: <B>(f: (a: A) => readonly [B, A]) => Effect.flatMap(options.get, a => {
|
||||
const [b, next] = f(a)
|
||||
return Effect.map(options.set(next), () => b)
|
||||
}),
|
||||
}, LensPrototype)
|
||||
|
||||
/**
|
||||
@@ -45,7 +51,7 @@ export const fromSubscriptionRef = <A>(
|
||||
): Lens<A, never, never, never, never> => make({
|
||||
get: ref.get,
|
||||
changes: ref.changes,
|
||||
set: (v: A) => SubscriptionRef.set(ref, v),
|
||||
modify: ref.modify,
|
||||
})
|
||||
|
||||
export const unwrap = <A, ER, RR, EW, RW, E1, R1>(
|
||||
|
||||
Reference in New Issue
Block a user