@@ -175,6 +175,44 @@ export const derive: {
|
||||
): Lens<A, ER2, EW2, RR2, RW2> => new DerivedLensImpl(asLensImpl(self), source))
|
||||
|
||||
|
||||
export class RefLensImpl<in out A>
|
||||
extends LensImpl<A, never, never, never, never> {
|
||||
constructor(
|
||||
readonly ref: Ref.Ref<A>,
|
||||
readonly semaphore: Effect.Semaphore,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
get resolve(): Effect.Effect<LensImpl.Frame<A>, never, never> {
|
||||
return Effect.map(
|
||||
Ref.get(this.ref),
|
||||
value => ({
|
||||
value,
|
||||
commit: next => Effect.flatMap(
|
||||
next,
|
||||
value => Ref.set(this.ref, value),
|
||||
),
|
||||
}),
|
||||
)
|
||||
}
|
||||
get changes() { return Stream.unwrap(Effect.map(Ref.get(this.ref), Stream.make)) }
|
||||
get withLock() { return this.semaphore.withPermits(1) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a `Lens` that proxies a `Ref`.
|
||||
*
|
||||
* Note: since `Ref` does not provide any kind of reactivity mechanism, the produced `Lens` will be non-reactive.
|
||||
* This means its `changes` stream will only emit the current value once when evaluated and nothing else.
|
||||
*/
|
||||
export const fromRef = Effect.fnUntraced(function* <A>(
|
||||
ref: Ref.Ref<A>
|
||||
): Effect.fn.Return<Lens<A, never, never, never, never>, never, never> {
|
||||
return new RefLensImpl(ref, yield* Effect.makeSemaphore(1))
|
||||
})
|
||||
|
||||
|
||||
export declare namespace SynchronizedRefLensImpl {
|
||||
export interface SynchronizedRefWithInternals<in out A>
|
||||
extends SynchronizedRef.SynchronizedRef<A> {
|
||||
|
||||
Reference in New Issue
Block a user