0.1.11 #14

Merged
Thilawyn merged 318 commits from next into master 2025-05-19 14:01:41 +02:00
2 changed files with 16 additions and 16 deletions
Showing only changes of commit 110b0813f8 - Show all commits

View File

@@ -13,7 +13,7 @@ export const Route = createFileRoute("/tests")({
function RouteComponent() { function RouteComponent() {
const deepRef = R.useRef({ value: "poulet" }) const deepRef = R.useRef({ value: "poulet" })
const deepValueRef = useMemo(() => SubscriptionSubRef.make( const deepValueRef = useMemo(() => SubscriptionSubRef.makeFromGetSet(
deepRef, deepRef,
b => b.value, b => b.value,
(b, a) => ({ ...b, value: a }), (b, a) => ({ ...b, value: a }),

View File

@@ -2,7 +2,7 @@ import { Effect, Effectable, Readable, Ref, Stream, Subscribable, SubscriptionRe
export interface SubscriptionSubRef<in out A, in out B> extends SubscriptionRef.SubscriptionRef<A> { export interface SubscriptionSubRef<in out A, in out B> extends SubscriptionRef.SubscriptionRef<A> {
readonly ref: SubscriptionRef.SubscriptionRef<B> readonly parent: SubscriptionRef.SubscriptionRef<B>
} }
@@ -19,12 +19,12 @@ class SubscriptionSubRefImpl<in out A, in out B> extends Effectable.Class<A> imp
readonly get: Effect.Effect<A> readonly get: Effect.Effect<A>
constructor( constructor(
readonly ref: SubscriptionRef.SubscriptionRef<B>, readonly parent: SubscriptionRef.SubscriptionRef<B>,
readonly select: (value: B) => A, readonly getter: (parentValue: B) => A,
readonly setter: (value: B, subValue: A) => B, readonly setter: (parentValue: B, value: A) => B,
) { ) {
super() super()
this.get = Ref.get(this.ref).pipe(Effect.map(this.select)) this.get = Ref.get(this.parent).pipe(Effect.map(this.getter))
} }
commit() { commit() {
@@ -33,8 +33,8 @@ class SubscriptionSubRefImpl<in out A, in out B> extends Effectable.Class<A> imp
get changes(): Stream.Stream<A> { get changes(): Stream.Stream<A> {
return this.get.pipe( return this.get.pipe(
Effect.map(a => this.ref.changes.pipe( Effect.map(a => this.parent.changes.pipe(
Stream.map(this.select), Stream.map(this.getter),
s => Stream.concat(Stream.make(a), s), s => Stream.concat(Stream.make(a), s),
)), )),
Stream.unwrap, Stream.unwrap,
@@ -47,17 +47,17 @@ class SubscriptionSubRefImpl<in out A, in out B> extends Effectable.Class<A> imp
modifyEffect<C, E, R>(f: (a: A) => Effect.Effect<readonly [C, A], E, R>): Effect.Effect<C, E, R> { modifyEffect<C, E, R>(f: (a: A) => Effect.Effect<readonly [C, A], E, R>): Effect.Effect<C, E, R> {
return Effect.Do.pipe( return Effect.Do.pipe(
Effect.bind("b", () => Ref.get(this.ref)), Effect.bind("b", () => Ref.get(this.parent)),
Effect.bind("ca", ({ b }) => f(this.select(b))), Effect.bind("ca", ({ b }) => f(this.getter(b))),
Effect.tap(({ b, ca: [, a] }) => Ref.set(this.ref, this.setter(b, a))), Effect.tap(({ b, ca: [, a] }) => Ref.set(this.parent, this.setter(b, a))),
Effect.map(({ ca: [c] }) => c), Effect.map(({ ca: [c] }) => c),
) )
} }
} }
export const make = <A, B>( export const makeFromGetSet = <A, B>(
ref: SubscriptionRef.SubscriptionRef<B>, parent: SubscriptionRef.SubscriptionRef<B>,
select: (value: B) => A, getter: (parentValue: B) => A,
setter: (value: B, subValue: A) => B, setter: (parentValue: B, value: A) => B,
): SubscriptionSubRef<A, B> => new SubscriptionSubRefImpl(ref, select, setter) ): SubscriptionSubRef<A, B> => new SubscriptionSubRefImpl(parent, getter, setter)