5 Commits

Author SHA1 Message Date
Julien Valverdé
076007ec67 Refactoring
All checks were successful
Lint / lint (push) Successful in 13s
2025-04-27 18:52:08 +02:00
Julien Valverdé
dd524e1aa5 Refactoring
All checks were successful
Lint / lint (push) Successful in 14s
2025-04-27 18:46:33 +02:00
Julien Valverdé
1c7cef703b SubRef
All checks were successful
Lint / lint (push) Successful in 17s
2025-04-25 13:50:54 +02:00
Julien Valverdé
fa0f8c6b24 Refactoring
All checks were successful
Lint / lint (push) Successful in 14s
2025-04-25 13:38:42 +02:00
Julien Valverdé
357e5aa56b useSubRef
All checks were successful
Lint / lint (push) Successful in 13s
2025-04-25 10:16:04 +02:00
4 changed files with 54 additions and 48 deletions

View File

@@ -11,7 +11,7 @@ export const Route = createFileRoute("/tests")({
function RouteComponent() {
const deepRef = R.useRef({ value: "poulet" })
const deepValueRef = R.useSubRefFromPath(deepRef, ["value"])
const deepValueRef = R.useSubRef(deepRef, ["value"])
// const value = R.useMemoScoped(Effect.addFinalizer(() => Console.log("cleanup")).pipe(
// Effect.andThen(makeUuid4),

View File

@@ -14,11 +14,16 @@ export interface ScopeOptions {
readonly finalizerExecutionStrategy?: ExecutionStrategy.ExecutionStrategy
}
export type RefsA<T extends readonly SubscriptionRef.SubscriptionRef<any>[]> = {
[K in keyof T]: Effect.Effect.Success<T[K]>
}
export abstract class ReffuseNamespace<R> {
declare ["constructor"]: ReffuseNamespaceClass<R>
constructor() {
this.SubRef = this.SubRef.bind(this as any) as any
this.SubscribeRefs = this.SubscribeRefs.bind(this as any) as any
this.RefState = this.RefState.bind(this as any) as any
this.SubscribeStream = this.SubscribeStream.bind(this as any) as any
@@ -384,19 +389,7 @@ export abstract class ReffuseNamespace<R> {
)
}
useSubRefFromGetSet<A, B, R>(
this: ReffuseNamespace<R>,
parent: SubscriptionRef.SubscriptionRef<B>,
getter: (parentValue: B) => A,
setter: (parentValue: B, value: A) => B,
): SubscriptionSubRef.SubscriptionSubRef<A, B> {
return React.useMemo(
() => SubscriptionSubRef.makeFromGetSet(parent, getter, setter),
[parent],
)
}
useSubRefFromPath<B, const P extends PropertyPath.Paths<B>, R>(
useSubRef<B, const P extends PropertyPath.Paths<B>, R>(
this: ReffuseNamespace<R>,
parent: SubscriptionRef.SubscriptionRef<B>,
path: P,
@@ -413,18 +406,18 @@ export abstract class ReffuseNamespace<R> {
>(
this: ReffuseNamespace<R>,
...refs: Refs
): [...{ [K in keyof Refs]: Effect.Effect.Success<Refs[K]> }] {
): RefsA<Refs> {
const [reactStateValue, setReactStateValue] = React.useState(this.useMemo(
() => Effect.all(refs as readonly SubscriptionRef.SubscriptionRef<any>[]),
[],
{ doNotReExecuteOnRuntimeOrContextChange: true },
) as [...{ [K in keyof Refs]: Effect.Effect.Success<Refs[K]> }])
) as RefsA<Refs>)
this.useFork(() => pipe(
refs.map(ref => Stream.changesWith(ref.changes, (x, y) => x === y)),
streams => Stream.zipLatestAll(...streams),
Stream.runForEach(v =>
Effect.sync(() => setReactStateValue(v as [...{ [K in keyof Refs]: Effect.Effect.Success<Refs[K]> }]))
Effect.sync(() => setReactStateValue(v as RefsA<Refs>))
),
), refs)
@@ -491,6 +484,17 @@ export abstract class ReffuseNamespace<R> {
}
SubRef<B, const P extends PropertyPath.Paths<B>, R>(
this: ReffuseNamespace<R>,
props: {
readonly parent: SubscriptionRef.SubscriptionRef<B>,
readonly path: P,
readonly children: (subRef: SubscriptionSubRef.SubscriptionSubRef<PropertyPath.ValueFromPath<B, P>, B>) => React.ReactNode
},
): React.ReactNode {
return props.children(this.useSubRef(props.parent, props.path))
}
SubscribeRefs<
const Refs extends readonly SubscriptionRef.SubscriptionRef<any>[],
R,
@@ -498,7 +502,7 @@ export abstract class ReffuseNamespace<R> {
this: ReffuseNamespace<R>,
props: {
readonly refs: Refs
readonly children: (...args: [...{ [K in keyof Refs]: Effect.Effect.Success<Refs[K]> }]) => React.ReactNode
readonly children: (...args: RefsA<Refs>) => React.ReactNode
},
): React.ReactNode {
return props.children(...this.useSubscribeRefs(...props.refs))

View File

@@ -1,4 +1,4 @@
import { Array, Option, Predicate } from "effect"
import { Array, Function, Option, Predicate } from "effect"
export type Paths<T> = [] | (
@@ -37,57 +37,58 @@ export type AnyKey = string | number | symbol
export type AnyPath = readonly AnyKey[]
export const unsafeGet = <T, const P extends Paths<T>>(
parent: T,
path: P,
): ValueFromPath<T, P> => (
path.reduce((acc: any, key: any) => acc?.[key], parent)
export const unsafeGet: {
<T, const P extends Paths<T>>(path: P): (self: T) => ValueFromPath<T, P>
<T, const P extends Paths<T>>(self: T, path: P): ValueFromPath<T, P>
} = Function.dual(2, <T, const P extends Paths<T>>(self: T, path: P): ValueFromPath<T, P> =>
path.reduce((acc: any, key: any) => acc?.[key], self)
)
export const get = <T, const P extends Paths<T>>(
parent: T,
path: P,
): Option.Option<ValueFromPath<T, P>> => path.reduce(
(acc: Option.Option<any>, key: any): Option.Option<any> => Option.isSome(acc)
? Predicate.hasProperty(acc.value, key)
? Option.some(acc.value[key])
: Option.none()
: acc,
export const get: {
<T, const P extends Paths<T>>(path: P): (self: T) => Option.Option<ValueFromPath<T, P>>
<T, const P extends Paths<T>>(self: T, path: P): Option.Option<ValueFromPath<T, P>>
} = Function.dual(2, <T, const P extends Paths<T>>(self: T, path: P): Option.Option<ValueFromPath<T, P>> =>
path.reduce(
(acc: Option.Option<any>, key: any): Option.Option<any> => Option.isSome(acc)
? Predicate.hasProperty(acc.value, key)
? Option.some(acc.value[key])
: Option.none()
: acc,
Option.some(parent),
Option.some(self),
)
)
export const immutableSet = <T, const P extends Paths<T>>(
parent: T,
path: P,
value: ValueFromPath<T, P>,
): Option.Option<T> => {
export const immutableSet: {
<T, const P extends Paths<T>>(path: P, value: ValueFromPath<T, P>): (self: T) => ValueFromPath<T, P>
<T, const P extends Paths<T>>(self: T, path: P, value: ValueFromPath<T, P>): Option.Option<T>
} = Function.dual(3, <T, const P extends Paths<T>>(self: T, path: P, value: ValueFromPath<T, P>): Option.Option<T> => {
const key = Array.head(path as AnyPath)
if (Option.isNone(key))
return Option.some(value as T)
if (!Predicate.hasProperty(parent, key.value))
if (!Predicate.hasProperty(self, key.value))
return Option.none()
const child = immutableSet<any, any>(parent[key.value], Option.getOrThrow(Array.tail(path as AnyPath)), value)
const child = immutableSet<any, any>(self[key.value], Option.getOrThrow(Array.tail(path as AnyPath)), value)
if (Option.isNone(child))
return child
if (Array.isArray(parent))
if (Array.isArray(self))
return typeof key.value === "number"
? Option.some([
...parent.slice(0, key.value),
...self.slice(0, key.value),
child.value,
...parent.slice(key.value + 1),
...self.slice(key.value + 1),
] as T)
: Option.none()
if (typeof parent === "object")
if (typeof self === "object")
return Option.some(
Object.assign(
Object.create(Object.getPrototypeOf(parent)),
{ ...parent, [key.value]: child.value },
Object.create(Object.getPrototypeOf(self)),
{ ...self, [key.value]: child.value },
)
)
return Option.none()
}
})

View File

@@ -3,6 +3,7 @@ import * as PropertyPath from "./PropertyPath.js"
export const SubscriptionSubRefTypeId: unique symbol = Symbol.for("reffuse/types/SubscriptionSubRef")
export type SubscriptionSubRefTypeId = typeof SubscriptionSubRefTypeId
export interface SubscriptionSubRef<in out A, in out B> extends SubscriptionSubRef.Variance<A, B>, SubscriptionRef.SubscriptionRef<A> {
readonly parent: SubscriptionRef.SubscriptionRef<B>