0.1.8 (#11)
Co-authored-by: Julien Valverdé <julien.valverde@mailo.com> Reviewed-on: https://gitea:3000/Thilawyn/reffuse/pulls/11
This commit was merged in pull request #11.
This commit is contained in:
@@ -10,6 +10,13 @@ export const Route = createFileRoute("/tests")({
|
||||
})
|
||||
|
||||
function RouteComponent() {
|
||||
const deepRef = R.useRef({ value: "poulet" })
|
||||
const deepValueRef = R.useSubRefFromGetSet(
|
||||
deepRef,
|
||||
b => b.value,
|
||||
(b, a) => ({ ...b, value: a }),
|
||||
)
|
||||
|
||||
// const value = R.useMemoScoped(Effect.addFinalizer(() => Console.log("cleanup")).pipe(
|
||||
// Effect.andThen(makeUuid4),
|
||||
// Effect.provide(GetRandomValues.CryptoRandom),
|
||||
@@ -32,7 +39,8 @@ function RouteComponent() {
|
||||
|
||||
const generateUuid = R.useCallbackSync(() => makeUuid4.pipe(
|
||||
Effect.provide(GetRandomValues.CryptoRandom),
|
||||
Effect.flatMap(v => Ref.set(uuidRef, v)),
|
||||
Effect.tap(v => Ref.set(uuidRef, v)),
|
||||
Effect.tap(v => Ref.set(deepValueRef, v)),
|
||||
), [])
|
||||
|
||||
|
||||
@@ -42,6 +50,10 @@ function RouteComponent() {
|
||||
{(uuid, anotherRef) => <Text>{uuid} / {anotherRef}</Text>}
|
||||
</R.SubscribeRefs>
|
||||
|
||||
<R.SubscribeRefs refs={[deepRef, deepValueRef]}>
|
||||
{(deep, deepValue) => <Text>{JSON.stringify(deep)} / {deepValue}</Text>}
|
||||
</R.SubscribeRefs>
|
||||
|
||||
<Button onClick={() => logValue("test")}>Log value</Button>
|
||||
<Button onClick={() => generateUuid()}>Generate UUID</Button>
|
||||
</Flex>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@reffuse/extension-lazyref",
|
||||
"version": "0.1.3",
|
||||
"version": "0.1.4",
|
||||
"type": "module",
|
||||
"files": [
|
||||
"./README.md",
|
||||
@@ -37,6 +37,6 @@
|
||||
"@types/react": "^19.0.0",
|
||||
"effect": "^3.13.0",
|
||||
"react": "^19.0.0",
|
||||
"reffuse": "^0.1.7"
|
||||
"reffuse": "^0.1.8"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import * as LazyRef from "@typed/lazy-ref"
|
||||
import { Effect, pipe, Stream } from "effect"
|
||||
import * as React from "react"
|
||||
import { ReffuseExtension, type ReffuseNamespace, SetStateAction } from "reffuse"
|
||||
import { ReffuseExtension, type ReffuseNamespace } from "reffuse"
|
||||
import { SetStateAction } from "reffuse/types"
|
||||
|
||||
|
||||
export const LazyRefExtension = ReffuseExtension.make(() => ({
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "reffuse",
|
||||
"version": "0.1.7",
|
||||
"version": "0.1.8",
|
||||
"type": "module",
|
||||
"files": [
|
||||
"./README.md",
|
||||
@@ -16,6 +16,10 @@
|
||||
"types": "./dist/index.d.ts",
|
||||
"default": "./dist/index.js"
|
||||
},
|
||||
"./types": {
|
||||
"types": "./dist/types/index.d.ts",
|
||||
"default": "./dist/types/index.js"
|
||||
},
|
||||
"./*": {
|
||||
"types": "./dist/*.d.ts",
|
||||
"default": "./dist/*.js"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type * as ReffuseContext from "./ReffuseContext.js"
|
||||
import type * as ReffuseExtension from "./ReffuseExtension.js"
|
||||
import * as ReffuseNamespace from "./ReffuseNamespace.js"
|
||||
import type { Merge, StaticType } from "./types.js"
|
||||
import type { Merge, StaticType } from "./utils.js"
|
||||
|
||||
|
||||
export class Reffuse extends ReffuseNamespace.makeClass() {}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { type Context, Effect, ExecutionStrategy, Exit, type Fiber, type Layer,
|
||||
import * as React from "react"
|
||||
import * as ReffuseContext from "./ReffuseContext.js"
|
||||
import * as ReffuseRuntime from "./ReffuseRuntime.js"
|
||||
import * as SetStateAction from "./SetStateAction.js"
|
||||
import { SetStateAction, SubscriptionSubRef } from "./types/index.js"
|
||||
|
||||
|
||||
export interface RenderOptions {
|
||||
@@ -384,6 +384,18 @@ 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],
|
||||
)
|
||||
}
|
||||
|
||||
useSubscribeRefs<
|
||||
const Refs extends readonly SubscriptionRef.SubscriptionRef<any>[],
|
||||
R,
|
||||
|
||||
@@ -3,4 +3,3 @@ export * as ReffuseContext from "./ReffuseContext.js"
|
||||
export * as ReffuseExtension from "./ReffuseExtension.js"
|
||||
export * as ReffuseNamespace from "./ReffuseNamespace.js"
|
||||
export * as ReffuseRuntime from "./ReffuseRuntime.js"
|
||||
export * as SetStateAction from "./SetStateAction.js"
|
||||
|
||||
77
packages/reffuse/src/types/SubscriptionSubRef.ts
Normal file
77
packages/reffuse/src/types/SubscriptionSubRef.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { Effect, Effectable, Readable, Ref, Stream, Subscribable, SubscriptionRef, SynchronizedRef, type Types } from "effect"
|
||||
|
||||
|
||||
export const SubscriptionSubRefTypeId: unique symbol = Symbol.for("reffuse/types/SubscriptionSubRef")
|
||||
|
||||
export interface SubscriptionSubRef<in out A, in out B> extends SubscriptionSubRef.Variance<A, B>, SubscriptionRef.SubscriptionRef<A> {
|
||||
readonly parent: SubscriptionRef.SubscriptionRef<B>
|
||||
}
|
||||
|
||||
export declare namespace SubscriptionSubRef {
|
||||
export interface Variance<in out A, in out B> {
|
||||
readonly [SubscriptionSubRefTypeId]: {
|
||||
readonly _A: Types.Invariant<A>
|
||||
readonly _B: Types.Invariant<B>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const refVariance = { _A: (_: any) => _ }
|
||||
const synchronizedRefVariance = { _A: (_: any) => _ }
|
||||
const subscriptionRefVariance = { _A: (_: any) => _ }
|
||||
const subscriptionSubRefVariance = { _A: (_: any) => _, _B: (_: any) => _ }
|
||||
|
||||
class SubscriptionSubRefImpl<in out A, in out B> extends Effectable.Class<A> implements SubscriptionSubRef<A, B> {
|
||||
readonly [Readable.TypeId]: Readable.TypeId = Readable.TypeId
|
||||
readonly [Subscribable.TypeId]: Subscribable.TypeId = Subscribable.TypeId
|
||||
readonly [Ref.RefTypeId] = refVariance
|
||||
readonly [SynchronizedRef.SynchronizedRefTypeId] = synchronizedRefVariance
|
||||
readonly [SubscriptionRef.SubscriptionRefTypeId] = subscriptionRefVariance
|
||||
readonly [SubscriptionSubRefTypeId] = subscriptionSubRefVariance
|
||||
|
||||
readonly get: Effect.Effect<A>
|
||||
|
||||
constructor(
|
||||
readonly parent: SubscriptionRef.SubscriptionRef<B>,
|
||||
readonly getter: (parentValue: B) => A,
|
||||
readonly setter: (parentValue: B, value: A) => B,
|
||||
) {
|
||||
super()
|
||||
this.get = Ref.get(this.parent).pipe(Effect.map(this.getter))
|
||||
}
|
||||
|
||||
commit() {
|
||||
return this.get
|
||||
}
|
||||
|
||||
get changes(): Stream.Stream<A> {
|
||||
return this.get.pipe(
|
||||
Effect.map(a => this.parent.changes.pipe(
|
||||
Stream.map(this.getter),
|
||||
s => Stream.concat(Stream.make(a), s),
|
||||
)),
|
||||
Stream.unwrap,
|
||||
)
|
||||
}
|
||||
|
||||
modify<C>(f: (a: A) => readonly [C, A]): Effect.Effect<C> {
|
||||
return this.modifyEffect(a => Effect.succeed(f(a)))
|
||||
}
|
||||
|
||||
modifyEffect<C, E, R>(f: (a: A) => Effect.Effect<readonly [C, A], E, R>): Effect.Effect<C, E, R> {
|
||||
return Effect.Do.pipe(
|
||||
Effect.bind("b", () => Ref.get(this.parent)),
|
||||
Effect.bind("ca", ({ b }) => f(this.getter(b))),
|
||||
Effect.tap(({ b, ca: [, a] }) => Ref.set(this.parent, this.setter(b, a))),
|
||||
Effect.map(({ ca: [c] }) => c),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const makeFromGetSet = <A, B>(
|
||||
parent: SubscriptionRef.SubscriptionRef<B>,
|
||||
getter: (parentValue: B) => A,
|
||||
setter: (parentValue: B, value: A) => B,
|
||||
): SubscriptionSubRef<A, B> => new SubscriptionSubRefImpl(parent, getter, setter)
|
||||
2
packages/reffuse/src/types/index.ts
Normal file
2
packages/reffuse/src/types/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * as SetStateAction from "./SetStateAction.js"
|
||||
export * as SubscriptionSubRef from "./SubscriptionSubRef.js"
|
||||
Reference in New Issue
Block a user