Compare commits
5 Commits
ea374d7e0f
...
extension-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
076007ec67 | ||
|
|
dd524e1aa5 | ||
|
|
1c7cef703b | ||
|
|
fa0f8c6b24 | ||
|
|
357e5aa56b |
@@ -11,7 +11,7 @@ export const Route = createFileRoute("/tests")({
|
|||||||
|
|
||||||
function RouteComponent() {
|
function RouteComponent() {
|
||||||
const deepRef = R.useRef({ value: "poulet" })
|
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(
|
// const value = R.useMemoScoped(Effect.addFinalizer(() => Console.log("cleanup")).pipe(
|
||||||
// Effect.andThen(makeUuid4),
|
// Effect.andThen(makeUuid4),
|
||||||
|
|||||||
@@ -14,11 +14,16 @@ export interface ScopeOptions {
|
|||||||
readonly finalizerExecutionStrategy?: ExecutionStrategy.ExecutionStrategy
|
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> {
|
export abstract class ReffuseNamespace<R> {
|
||||||
declare ["constructor"]: ReffuseNamespaceClass<R>
|
declare ["constructor"]: ReffuseNamespaceClass<R>
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
this.SubRef = this.SubRef.bind(this as any) as any
|
||||||
this.SubscribeRefs = this.SubscribeRefs.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.RefState = this.RefState.bind(this as any) as any
|
||||||
this.SubscribeStream = this.SubscribeStream.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>(
|
useSubRef<B, const P extends PropertyPath.Paths<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>(
|
|
||||||
this: ReffuseNamespace<R>,
|
this: ReffuseNamespace<R>,
|
||||||
parent: SubscriptionRef.SubscriptionRef<B>,
|
parent: SubscriptionRef.SubscriptionRef<B>,
|
||||||
path: P,
|
path: P,
|
||||||
@@ -413,18 +406,18 @@ export abstract class ReffuseNamespace<R> {
|
|||||||
>(
|
>(
|
||||||
this: ReffuseNamespace<R>,
|
this: ReffuseNamespace<R>,
|
||||||
...refs: Refs
|
...refs: Refs
|
||||||
): [...{ [K in keyof Refs]: Effect.Effect.Success<Refs[K]> }] {
|
): RefsA<Refs> {
|
||||||
const [reactStateValue, setReactStateValue] = React.useState(this.useMemo(
|
const [reactStateValue, setReactStateValue] = React.useState(this.useMemo(
|
||||||
() => Effect.all(refs as readonly SubscriptionRef.SubscriptionRef<any>[]),
|
() => Effect.all(refs as readonly SubscriptionRef.SubscriptionRef<any>[]),
|
||||||
[],
|
[],
|
||||||
{ doNotReExecuteOnRuntimeOrContextChange: true },
|
{ doNotReExecuteOnRuntimeOrContextChange: true },
|
||||||
) as [...{ [K in keyof Refs]: Effect.Effect.Success<Refs[K]> }])
|
) as RefsA<Refs>)
|
||||||
|
|
||||||
this.useFork(() => pipe(
|
this.useFork(() => pipe(
|
||||||
refs.map(ref => Stream.changesWith(ref.changes, (x, y) => x === y)),
|
refs.map(ref => Stream.changesWith(ref.changes, (x, y) => x === y)),
|
||||||
streams => Stream.zipLatestAll(...streams),
|
streams => Stream.zipLatestAll(...streams),
|
||||||
Stream.runForEach(v =>
|
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)
|
), 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<
|
SubscribeRefs<
|
||||||
const Refs extends readonly SubscriptionRef.SubscriptionRef<any>[],
|
const Refs extends readonly SubscriptionRef.SubscriptionRef<any>[],
|
||||||
R,
|
R,
|
||||||
@@ -498,7 +502,7 @@ export abstract class ReffuseNamespace<R> {
|
|||||||
this: ReffuseNamespace<R>,
|
this: ReffuseNamespace<R>,
|
||||||
props: {
|
props: {
|
||||||
readonly refs: Refs
|
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 {
|
): React.ReactNode {
|
||||||
return props.children(...this.useSubscribeRefs(...props.refs))
|
return props.children(...this.useSubscribeRefs(...props.refs))
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Array, Option, Predicate } from "effect"
|
import { Array, Function, Option, Predicate } from "effect"
|
||||||
|
|
||||||
|
|
||||||
export type Paths<T> = [] | (
|
export type Paths<T> = [] | (
|
||||||
@@ -37,57 +37,58 @@ export type AnyKey = string | number | symbol
|
|||||||
export type AnyPath = readonly AnyKey[]
|
export type AnyPath = readonly AnyKey[]
|
||||||
|
|
||||||
|
|
||||||
export const unsafeGet = <T, const P extends Paths<T>>(
|
export const unsafeGet: {
|
||||||
parent: T,
|
<T, const P extends Paths<T>>(path: P): (self: T) => ValueFromPath<T, P>
|
||||||
path: P,
|
<T, const P extends Paths<T>>(self: T, path: P): ValueFromPath<T, 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], parent)
|
path.reduce((acc: any, key: any) => acc?.[key], self)
|
||||||
)
|
)
|
||||||
|
|
||||||
export const get = <T, const P extends Paths<T>>(
|
export const get: {
|
||||||
parent: T,
|
<T, const P extends Paths<T>>(path: P): (self: T) => Option.Option<ValueFromPath<T, P>>
|
||||||
path: P,
|
<T, const P extends Paths<T>>(self: T, path: P): Option.Option<ValueFromPath<T, P>>
|
||||||
): Option.Option<ValueFromPath<T, P>> => path.reduce(
|
} = 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)
|
(acc: Option.Option<any>, key: any): Option.Option<any> => Option.isSome(acc)
|
||||||
? Predicate.hasProperty(acc.value, key)
|
? Predicate.hasProperty(acc.value, key)
|
||||||
? Option.some(acc.value[key])
|
? Option.some(acc.value[key])
|
||||||
: Option.none()
|
: Option.none()
|
||||||
: acc,
|
: acc,
|
||||||
|
|
||||||
Option.some(parent),
|
Option.some(self),
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
export const immutableSet = <T, const P extends Paths<T>>(
|
export const immutableSet: {
|
||||||
parent: T,
|
<T, const P extends Paths<T>>(path: P, value: ValueFromPath<T, P>): (self: T) => ValueFromPath<T, P>
|
||||||
path: P,
|
<T, const P extends Paths<T>>(self: T, path: P, value: ValueFromPath<T, P>): Option.Option<T>
|
||||||
value: ValueFromPath<T, P>,
|
} = Function.dual(3, <T, const P extends Paths<T>>(self: T, path: P, value: ValueFromPath<T, P>): Option.Option<T> => {
|
||||||
): Option.Option<T> => {
|
|
||||||
const key = Array.head(path as AnyPath)
|
const key = Array.head(path as AnyPath)
|
||||||
if (Option.isNone(key))
|
if (Option.isNone(key))
|
||||||
return Option.some(value as T)
|
return Option.some(value as T)
|
||||||
if (!Predicate.hasProperty(parent, key.value))
|
if (!Predicate.hasProperty(self, key.value))
|
||||||
return Option.none()
|
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))
|
if (Option.isNone(child))
|
||||||
return child
|
return child
|
||||||
|
|
||||||
if (Array.isArray(parent))
|
if (Array.isArray(self))
|
||||||
return typeof key.value === "number"
|
return typeof key.value === "number"
|
||||||
? Option.some([
|
? Option.some([
|
||||||
...parent.slice(0, key.value),
|
...self.slice(0, key.value),
|
||||||
child.value,
|
child.value,
|
||||||
...parent.slice(key.value + 1),
|
...self.slice(key.value + 1),
|
||||||
] as T)
|
] as T)
|
||||||
: Option.none()
|
: Option.none()
|
||||||
|
|
||||||
if (typeof parent === "object")
|
if (typeof self === "object")
|
||||||
return Option.some(
|
return Option.some(
|
||||||
Object.assign(
|
Object.assign(
|
||||||
Object.create(Object.getPrototypeOf(parent)),
|
Object.create(Object.getPrototypeOf(self)),
|
||||||
{ ...parent, [key.value]: child.value },
|
{ ...self, [key.value]: child.value },
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
return Option.none()
|
return Option.none()
|
||||||
}
|
})
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import * as PropertyPath from "./PropertyPath.js"
|
|||||||
|
|
||||||
|
|
||||||
export const SubscriptionSubRefTypeId: unique symbol = Symbol.for("reffuse/types/SubscriptionSubRef")
|
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> {
|
export interface SubscriptionSubRef<in out A, in out B> extends SubscriptionSubRef.Variance<A, B>, SubscriptionRef.SubscriptionRef<A> {
|
||||||
readonly parent: SubscriptionRef.SubscriptionRef<B>
|
readonly parent: SubscriptionRef.SubscriptionRef<B>
|
||||||
|
|||||||
Reference in New Issue
Block a user