From 107ff1e79477d899b7cfdb3dcf3b9205451270eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Fri, 25 Apr 2025 08:12:34 +0200 Subject: [PATCH] SubscriptionSubRef.makeFromPath --- packages/reffuse/src/types/SubscriptionSubRef.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/reffuse/src/types/SubscriptionSubRef.ts b/packages/reffuse/src/types/SubscriptionSubRef.ts index b9cb31f..6567c55 100644 --- a/packages/reffuse/src/types/SubscriptionSubRef.ts +++ b/packages/reffuse/src/types/SubscriptionSubRef.ts @@ -1,4 +1,5 @@ -import { Effect, Effectable, Readable, Ref, Stream, Subscribable, SubscriptionRef, SynchronizedRef, type Types, type Unify } from "effect" +import { Effect, Effectable, Option, Readable, Ref, Stream, Subscribable, SubscriptionRef, SynchronizedRef, type Types, type Unify } from "effect" +import * as PropertyPath from "./PropertyPath.js" export const SubscriptionSubRefTypeId: unique symbol = Symbol.for("reffuse/types/SubscriptionSubRef") @@ -87,3 +88,12 @@ export const makeFromGetSet = ( getter: (parentValue: B) => A, setter: (parentValue: B, value: A) => B, ): SubscriptionSubRef => new SubscriptionSubRefImpl(parent, getter, setter) + +export const makeFromPath = >( + parent: SubscriptionRef.SubscriptionRef, + path: P, +): SubscriptionSubRef, B> => new SubscriptionSubRefImpl( + parent, + parentValue => PropertyPath.unsafeGet(parentValue, path), + (parentValue, value) => Option.getOrThrow(PropertyPath.immutableSet(parentValue, path, value)), +)