Compare commits
8 Commits
form
...
974af95a22
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
974af95a22 | ||
|
|
d6e1d445e8 | ||
|
|
d8d6e87a12 | ||
|
|
682e473bf7 | ||
|
|
31dd7b5fdb | ||
|
|
17686e68c3 | ||
|
|
49d4bd4d43 | ||
|
|
be88035936 |
@@ -3,6 +3,8 @@ import { Button, Flex, Text } from "@radix-ui/themes"
|
|||||||
import { createFileRoute } from "@tanstack/react-router"
|
import { createFileRoute } from "@tanstack/react-router"
|
||||||
import { GetRandomValues, makeUuid4 } from "@typed/id"
|
import { GetRandomValues, makeUuid4 } from "@typed/id"
|
||||||
import { Console, Effect, Ref } from "effect"
|
import { Console, Effect, Ref } from "effect"
|
||||||
|
import { useMemo } from "react"
|
||||||
|
import { SubscriptionSubRef } from "reffuse/types"
|
||||||
|
|
||||||
|
|
||||||
export const Route = createFileRoute("/tests")({
|
export const Route = createFileRoute("/tests")({
|
||||||
@@ -10,6 +12,13 @@ export const Route = createFileRoute("/tests")({
|
|||||||
})
|
})
|
||||||
|
|
||||||
function RouteComponent() {
|
function RouteComponent() {
|
||||||
|
const deepRef = R.useRef({ value: "poulet" })
|
||||||
|
const deepValueRef = useMemo(() => SubscriptionSubRef.make(
|
||||||
|
deepRef,
|
||||||
|
b => b.value,
|
||||||
|
(b, a) => ({ ...b, value: a }),
|
||||||
|
), [deepRef])
|
||||||
|
|
||||||
// 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),
|
||||||
// Effect.provide(GetRandomValues.CryptoRandom),
|
// Effect.provide(GetRandomValues.CryptoRandom),
|
||||||
@@ -32,7 +41,8 @@ function RouteComponent() {
|
|||||||
|
|
||||||
const generateUuid = R.useCallbackSync(() => makeUuid4.pipe(
|
const generateUuid = R.useCallbackSync(() => makeUuid4.pipe(
|
||||||
Effect.provide(GetRandomValues.CryptoRandom),
|
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 +52,10 @@ function RouteComponent() {
|
|||||||
{(uuid, anotherRef) => <Text>{uuid} / {anotherRef}</Text>}
|
{(uuid, anotherRef) => <Text>{uuid} / {anotherRef}</Text>}
|
||||||
</R.SubscribeRefs>
|
</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={() => logValue("test")}>Log value</Button>
|
||||||
<Button onClick={() => generateUuid()}>Generate UUID</Button>
|
<Button onClick={() => generateUuid()}>Generate UUID</Button>
|
||||||
</Flex>
|
</Flex>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@reffuse/extension-lazyref",
|
"name": "@reffuse/extension-lazyref",
|
||||||
"version": "0.1.3",
|
"version": "0.1.4",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"files": [
|
"files": [
|
||||||
"./README.md",
|
"./README.md",
|
||||||
@@ -37,6 +37,6 @@
|
|||||||
"@types/react": "^19.0.0",
|
"@types/react": "^19.0.0",
|
||||||
"effect": "^3.13.0",
|
"effect": "^3.13.0",
|
||||||
"react": "^19.0.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 * as LazyRef from "@typed/lazy-ref"
|
||||||
import { Effect, pipe, Stream } from "effect"
|
import { Effect, pipe, Stream } from "effect"
|
||||||
import * as React from "react"
|
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(() => ({
|
export const LazyRefExtension = ReffuseExtension.make(() => ({
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "reffuse",
|
"name": "reffuse",
|
||||||
"version": "0.1.7",
|
"version": "0.1.8",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"files": [
|
"files": [
|
||||||
"./README.md",
|
"./README.md",
|
||||||
@@ -16,6 +16,10 @@
|
|||||||
"types": "./dist/index.d.ts",
|
"types": "./dist/index.d.ts",
|
||||||
"default": "./dist/index.js"
|
"default": "./dist/index.js"
|
||||||
},
|
},
|
||||||
|
"./types": {
|
||||||
|
"types": "./dist/types/index.d.ts",
|
||||||
|
"default": "./dist/types/index.js"
|
||||||
|
},
|
||||||
"./*": {
|
"./*": {
|
||||||
"types": "./dist/*.d.ts",
|
"types": "./dist/*.d.ts",
|
||||||
"default": "./dist/*.js"
|
"default": "./dist/*.js"
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import type * as ReffuseContext from "./ReffuseContext.js"
|
import type * as ReffuseContext from "./ReffuseContext.js"
|
||||||
import type * as ReffuseExtension from "./ReffuseExtension.js"
|
import type * as ReffuseExtension from "./ReffuseExtension.js"
|
||||||
import * as ReffuseNamespace from "./ReffuseNamespace.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() {}
|
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 React from "react"
|
||||||
import * as ReffuseContext from "./ReffuseContext.js"
|
import * as ReffuseContext from "./ReffuseContext.js"
|
||||||
import * as ReffuseRuntime from "./ReffuseRuntime.js"
|
import * as ReffuseRuntime from "./ReffuseRuntime.js"
|
||||||
import * as SetStateAction from "./SetStateAction.js"
|
import { SetStateAction } from "./types/index.js"
|
||||||
|
|
||||||
|
|
||||||
export interface RenderOptions {
|
export interface RenderOptions {
|
||||||
|
|||||||
@@ -3,4 +3,3 @@ export * as ReffuseContext from "./ReffuseContext.js"
|
|||||||
export * as ReffuseExtension from "./ReffuseExtension.js"
|
export * as ReffuseExtension from "./ReffuseExtension.js"
|
||||||
export * as ReffuseNamespace from "./ReffuseNamespace.js"
|
export * as ReffuseNamespace from "./ReffuseNamespace.js"
|
||||||
export * as ReffuseRuntime from "./ReffuseRuntime.js"
|
export * as ReffuseRuntime from "./ReffuseRuntime.js"
|
||||||
export * as SetStateAction from "./SetStateAction.js"
|
|
||||||
|
|||||||
63
packages/reffuse/src/types/SubscriptionSubRef.ts
Normal file
63
packages/reffuse/src/types/SubscriptionSubRef.ts
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
import { Effect, Effectable, Readable, Ref, Stream, Subscribable, SubscriptionRef, SynchronizedRef } from "effect"
|
||||||
|
|
||||||
|
|
||||||
|
export interface SubscriptionSubRef<in out A, in out B> extends SubscriptionRef.SubscriptionRef<A> {
|
||||||
|
readonly ref: SubscriptionRef.SubscriptionRef<B>
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const synchronizedRefVariance = { _A: (_: any) => _ }
|
||||||
|
const subscriptionRefVariance = { _A: (_: 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]: Ref.Ref.Variance<A>[Ref.RefTypeId] = { _A: (_: any) => _ }
|
||||||
|
readonly [SynchronizedRef.SynchronizedRefTypeId]: SynchronizedRef.SynchronizedRef.Variance<A>[SynchronizedRef.SynchronizedRefTypeId] = synchronizedRefVariance
|
||||||
|
readonly [SubscriptionRef.SubscriptionRefTypeId]: SubscriptionRef.SubscriptionRef.Variance<A>[SubscriptionRef.SubscriptionRefTypeId] = subscriptionRefVariance
|
||||||
|
|
||||||
|
readonly get: Effect.Effect<A>
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
readonly ref: SubscriptionRef.SubscriptionRef<B>,
|
||||||
|
readonly select: (value: B) => A,
|
||||||
|
readonly setter: (value: B, subValue: A) => B,
|
||||||
|
) {
|
||||||
|
super()
|
||||||
|
this.get = Ref.get(this.ref).pipe(Effect.map(this.select))
|
||||||
|
}
|
||||||
|
|
||||||
|
commit() {
|
||||||
|
return this.get
|
||||||
|
}
|
||||||
|
|
||||||
|
get changes(): Stream.Stream<A> {
|
||||||
|
return this.get.pipe(
|
||||||
|
Effect.map(a => this.ref.changes.pipe(
|
||||||
|
Stream.map(this.select),
|
||||||
|
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.ref)),
|
||||||
|
Effect.bind("ca", ({ b }) => f(this.select(b))),
|
||||||
|
Effect.tap(({ b, ca: [, a] }) => Ref.set(this.ref, this.setter(b, a))),
|
||||||
|
Effect.map(({ ca: [c] }) => c),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export const make = <A, B>(
|
||||||
|
ref: SubscriptionRef.SubscriptionRef<B>,
|
||||||
|
select: (value: B) => A,
|
||||||
|
setter: (value: B, subValue: A) => B,
|
||||||
|
): SubscriptionSubRef<A, B> => new SubscriptionSubRefImpl(ref, select, 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