Compare commits
17 Commits
form
...
e8f92c88b8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e8f92c88b8 | ||
|
|
6ae155de34 | ||
|
|
db783f174e | ||
|
|
2b48695e54 | ||
|
|
0fd3fe49a9 | ||
|
|
ab441fe982 | ||
|
|
eabcf9085b | ||
|
|
926482b154 | ||
|
|
110b0813f8 | ||
|
|
974af95a22 | ||
|
|
d6e1d445e8 | ||
|
|
d8d6e87a12 | ||
|
|
682e473bf7 | ||
|
|
31dd7b5fdb | ||
|
|
17686e68c3 | ||
|
|
49d4bd4d43 | ||
|
|
be88035936 |
24
bun.lock
24
bun.lock
@@ -46,21 +46,9 @@
|
|||||||
"vite": "^6.2.6",
|
"vite": "^6.2.6",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"packages/extension-form": {
|
|
||||||
"name": "@reffuse/extension-form",
|
|
||||||
"version": "0.1.0",
|
|
||||||
"devDependencies": {
|
|
||||||
"reffuse": "workspace:*",
|
|
||||||
},
|
|
||||||
"peerDependencies": {
|
|
||||||
"effect": "^3.13.0",
|
|
||||||
"react": "^19.0.0",
|
|
||||||
"reffuse": "^0.1.6",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"packages/extension-lazyref": {
|
"packages/extension-lazyref": {
|
||||||
"name": "@reffuse/extension-lazyref",
|
"name": "@reffuse/extension-lazyref",
|
||||||
"version": "0.1.2",
|
"version": "0.1.1",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"reffuse": "workspace:*",
|
"reffuse": "workspace:*",
|
||||||
},
|
},
|
||||||
@@ -69,12 +57,12 @@
|
|||||||
"@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.6",
|
"reffuse": "^0.1.4",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"packages/extension-query": {
|
"packages/extension-query": {
|
||||||
"name": "@reffuse/extension-query",
|
"name": "@reffuse/extension-query",
|
||||||
"version": "0.1.3",
|
"version": "0.1.2",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"reffuse": "workspace:*",
|
"reffuse": "workspace:*",
|
||||||
},
|
},
|
||||||
@@ -85,12 +73,12 @@
|
|||||||
"@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.6",
|
"reffuse": "^0.1.4",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"packages/reffuse": {
|
"packages/reffuse": {
|
||||||
"name": "reffuse",
|
"name": "reffuse",
|
||||||
"version": "0.1.6",
|
"version": "0.1.5",
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"@types/react": "^19.0.0",
|
"@types/react": "^19.0.0",
|
||||||
"effect": "^3.13.0",
|
"effect": "^3.13.0",
|
||||||
@@ -375,8 +363,6 @@
|
|||||||
|
|
||||||
"@reffuse/example": ["@reffuse/example@workspace:packages/example"],
|
"@reffuse/example": ["@reffuse/example@workspace:packages/example"],
|
||||||
|
|
||||||
"@reffuse/extension-form": ["@reffuse/extension-form@workspace:packages/extension-form"],
|
|
||||||
|
|
||||||
"@reffuse/extension-lazyref": ["@reffuse/extension-lazyref@workspace:packages/extension-lazyref"],
|
"@reffuse/extension-lazyref": ["@reffuse/extension-lazyref@workspace:packages/extension-lazyref"],
|
||||||
|
|
||||||
"@reffuse/extension-query": ["@reffuse/extension-query@workspace:packages/extension-query"],
|
"@reffuse/extension-query": ["@reffuse/extension-query@workspace:packages/extension-query"],
|
||||||
|
|||||||
@@ -10,6 +10,13 @@ export const Route = createFileRoute("/tests")({
|
|||||||
})
|
})
|
||||||
|
|
||||||
function RouteComponent() {
|
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(
|
// 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 +39,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 +50,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,9 +0,0 @@
|
|||||||
# LazyRef extension for Reffuse
|
|
||||||
|
|
||||||
Extension to integrate `@typed/lazy-ref` with Reffuse.
|
|
||||||
|
|
||||||
## Peer dependencies
|
|
||||||
- `@typed/lazy-ref`
|
|
||||||
- `reffuse` 0.1.3+
|
|
||||||
- `effect` 3.13+
|
|
||||||
- `react` & `@types/react` 19+
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "@reffuse/extension-form",
|
|
||||||
"version": "0.1.0",
|
|
||||||
"type": "module",
|
|
||||||
"files": [
|
|
||||||
"./README.md",
|
|
||||||
"./dist"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
|
||||||
"repository": {
|
|
||||||
"url": "git+https://github.com/Thiladev/reffuse.git"
|
|
||||||
},
|
|
||||||
"types": "./dist/index.d.ts",
|
|
||||||
"exports": {
|
|
||||||
".": {
|
|
||||||
"types": "./dist/index.d.ts",
|
|
||||||
"default": "./dist/index.js"
|
|
||||||
},
|
|
||||||
"./*": {
|
|
||||||
"types": "./dist/*.d.ts",
|
|
||||||
"default": "./dist/*.js"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"scripts": {
|
|
||||||
"build": "tsc",
|
|
||||||
"lint:tsc": "tsc --noEmit",
|
|
||||||
"pack": "npm pack",
|
|
||||||
"clean:cache": "rm -f tsconfig.tsbuildinfo",
|
|
||||||
"clean:dist": "rm -rf dist",
|
|
||||||
"clean:node": "rm -rf node_modules"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"reffuse": "workspace:*"
|
|
||||||
},
|
|
||||||
"peerDependencies": {
|
|
||||||
"effect": "^3.13.0",
|
|
||||||
"react": "^19.0.0",
|
|
||||||
"reffuse": "^0.1.7"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
import { ReffuseExtension, type ReffuseNamespace } from "reffuse"
|
|
||||||
|
|
||||||
|
|
||||||
export const FormExtension = ReffuseExtension.make(() => ({
|
|
||||||
useForm<A, E, R>(
|
|
||||||
this: ReffuseNamespace.ReffuseNamespace<R>,
|
|
||||||
) {
|
|
||||||
},
|
|
||||||
}))
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
export * from "./FormExtension.js"
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
import { Schema } from "effect"
|
|
||||||
|
|
||||||
|
|
||||||
export interface Form<A, I, R> {
|
|
||||||
readonly schema: Schema.Schema<A, I, R>
|
|
||||||
}
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
import type { Effect, Schema } from "effect"
|
|
||||||
import type * as Formify from "./Formify.js"
|
|
||||||
|
|
||||||
|
|
||||||
export interface FormField<S extends Schema.Schema.Any> {
|
|
||||||
readonly schema: S
|
|
||||||
}
|
|
||||||
|
|
||||||
export const makeFormField = <S extends Schema.Schema.Any>(
|
|
||||||
schema: S,
|
|
||||||
get: Effect.Effect<S["Type"]>,
|
|
||||||
set: (value: S["Type"]) => Effect.Effect<void>,
|
|
||||||
): FormField<S> => {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface UnionFormField<
|
|
||||||
S extends Schema.Union<Members>,
|
|
||||||
Members extends ReadonlyArray<Schema.Schema.All>,
|
|
||||||
> extends FormField<S> {
|
|
||||||
readonly member: Formify.Formify<Members[number]>
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface TupleFormField<
|
|
||||||
S extends Schema.TupleType<Elements, Rest>,
|
|
||||||
Elements extends Schema.TupleType.Elements,
|
|
||||||
Rest extends Schema.TupleType.Rest,
|
|
||||||
> extends FormField<S> {
|
|
||||||
readonly elements: [...{ readonly [K in keyof Elements]: Formify.Formify<Elements[K]> }]
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ArrayFormField<
|
|
||||||
S extends Schema.Array$<Value>,
|
|
||||||
Value extends Schema.Schema.Any,
|
|
||||||
> extends FormField<S> {
|
|
||||||
readonly elements: readonly Formify.Formify<Value>[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export type StructFormField<
|
|
||||||
S extends Schema.Struct<Fields>,
|
|
||||||
Fields extends Schema.Struct.Fields,
|
|
||||||
> = (
|
|
||||||
& FormField<S>
|
|
||||||
& { readonly fields: { readonly [K in keyof Fields]: Formify.Formify<Fields[K]> } }
|
|
||||||
& {
|
|
||||||
[K in keyof Fields as Fields[K] extends
|
|
||||||
Schema.tag<infer _> ? K : never
|
|
||||||
]: Fields[K] extends
|
|
||||||
Schema.tag<infer Tag> ? Tag : never
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
export interface GenericFormField<S extends Schema.Schema.Any> extends FormField<S> {
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export interface PropertySignatureFormField<
|
|
||||||
S extends Schema.PropertySignature<TypeToken, Type, Key, EncodedToken, Encoded, HasDefault, R>,
|
|
||||||
TypeToken extends Schema.PropertySignature.Token,
|
|
||||||
Type,
|
|
||||||
Key extends PropertyKey,
|
|
||||||
EncodedToken extends Schema.PropertySignature.Token,
|
|
||||||
Encoded,
|
|
||||||
HasDefault extends boolean = false,
|
|
||||||
R = never,
|
|
||||||
> {
|
|
||||||
readonly propertySignature: S
|
|
||||||
readonly value: Type
|
|
||||||
}
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
import { Schema } from "effect"
|
|
||||||
import type * as FormField from "./FormField.js"
|
|
||||||
|
|
||||||
|
|
||||||
export type Formify<S> = (
|
|
||||||
S extends Schema.Union<infer Members> ? FormField.UnionFormField<S, Members> :
|
|
||||||
S extends Schema.TupleType<infer Elements, infer Rest> ? FormField.TupleFormField<S, Elements, Rest> :
|
|
||||||
S extends Schema.Array$<infer Value> ? FormField.ArrayFormField<S, Value> :
|
|
||||||
S extends Schema.Struct<infer Fields> ? FormField.StructFormField<S, Fields> :
|
|
||||||
S extends Schema.Schema.Any ? FormField.GenericFormField<S> :
|
|
||||||
S extends Schema.PropertySignature<
|
|
||||||
infer TypeToken,
|
|
||||||
infer Type,
|
|
||||||
infer Key,
|
|
||||||
infer EncodedToken,
|
|
||||||
infer Encoded,
|
|
||||||
infer HasDefault,
|
|
||||||
infer R
|
|
||||||
> ? FormField.PropertySignatureFormField<S, TypeToken, Type, Key, EncodedToken, Encoded, HasDefault, R> :
|
|
||||||
never
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
const Login = Schema.Union(
|
|
||||||
Schema.Struct({
|
|
||||||
_tag: Schema.tag("ByEmail"),
|
|
||||||
email: Schema.String,
|
|
||||||
password: Schema.RedactedFromSelf(Schema.String),
|
|
||||||
}),
|
|
||||||
|
|
||||||
Schema.Struct({
|
|
||||||
_tag: Schema.tag("ByPhone"),
|
|
||||||
phone: Schema.String,
|
|
||||||
password: Schema.RedactedFromSelf(Schema.String),
|
|
||||||
}),
|
|
||||||
|
|
||||||
Schema.TaggedStruct("ByKey", {
|
|
||||||
id: Schema.String,
|
|
||||||
password: Schema.RedactedFromSelf(Schema.String),
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
type LoginForm = Formify<typeof Login>
|
|
||||||
declare const loginForm: LoginForm
|
|
||||||
|
|
||||||
switch (loginForm.member._tag) {
|
|
||||||
case "ByEmail":
|
|
||||||
loginForm.member
|
|
||||||
break
|
|
||||||
case "ByPhone":
|
|
||||||
break
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
import { Array, Predicate, Record, Schema, Tuple } from "effect"
|
|
||||||
|
|
||||||
|
|
||||||
export const isTupleSchema = (u: unknown): u is Schema.Tuple<any> => (
|
|
||||||
Schema.isSchema(u) &&
|
|
||||||
Predicate.hasProperty(u, "elements") && Array.isArray(u.elements) &&
|
|
||||||
Predicate.hasProperty(u, "rest") && Array.isArray(u.rest)
|
|
||||||
)
|
|
||||||
|
|
||||||
export const isArraySchema = (u: unknown): u is Schema.Array$<any> => (
|
|
||||||
Schema.isSchema(u) &&
|
|
||||||
Predicate.hasProperty(u, "elements") && Array.isArray(u.elements) && Array.isEmptyArray(u.elements) &&
|
|
||||||
Predicate.hasProperty(u, "rest") && Array.isArray(u.rest) && Tuple.isTupleOf(u.rest, 1) &&
|
|
||||||
Predicate.hasProperty(u, "value")
|
|
||||||
)
|
|
||||||
|
|
||||||
export const isStructSchema = (u: unknown): u is Schema.Struct<any> => (
|
|
||||||
Schema.isSchema(u) &&
|
|
||||||
Predicate.hasProperty(u, "fields") && Predicate.isObject(u.fields) &&
|
|
||||||
Predicate.hasProperty(u, "records") && Array.isArray(u.records) && Array.isEmptyArray(u.records)
|
|
||||||
)
|
|
||||||
|
|
||||||
export const isRecordSchema = (u: unknown): u is Schema.Record$<any, any> => (
|
|
||||||
Schema.isSchema(u) &&
|
|
||||||
Predicate.hasProperty(u, "fields") && Predicate.isObject(u.fields) && Record.isEmptyRecord(u.fields) &&
|
|
||||||
Predicate.hasProperty(u, "records") && Array.isArray(u.records) &&
|
|
||||||
Predicate.hasProperty(u, "key") &&
|
|
||||||
Predicate.hasProperty(u, "value")
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
const myTuple = Schema.Tuple(Schema.String)
|
|
||||||
const myArray = Schema.Array(Schema.String)
|
|
||||||
const myStruct = Schema.Struct({})
|
|
||||||
const myRecord = Schema.Record({ key: Schema.String, value: Schema.String })
|
|
||||||
|
|
||||||
console.log(isArraySchema(myTuple))
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
export * as Form from "./Form.js"
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
// Enable latest features
|
|
||||||
"lib": ["ESNext", "DOM"],
|
|
||||||
"target": "ESNext",
|
|
||||||
"module": "NodeNext",
|
|
||||||
"moduleDetection": "force",
|
|
||||||
"jsx": "react-jsx",
|
|
||||||
// "allowJs": true,
|
|
||||||
|
|
||||||
// Bundler mode
|
|
||||||
"moduleResolution": "NodeNext",
|
|
||||||
// "allowImportingTsExtensions": true,
|
|
||||||
"verbatimModuleSyntax": true,
|
|
||||||
// "noEmit": true,
|
|
||||||
|
|
||||||
// Best practices
|
|
||||||
"strict": true,
|
|
||||||
"skipLibCheck": true,
|
|
||||||
"noFallthroughCasesInSwitch": true,
|
|
||||||
|
|
||||||
// Some stricter flags (disabled by default)
|
|
||||||
"noUnusedLocals": false,
|
|
||||||
"noUnusedParameters": false,
|
|
||||||
"noPropertyAccessFromIndexSignature": false,
|
|
||||||
|
|
||||||
// Build
|
|
||||||
"outDir": "./dist",
|
|
||||||
"declaration": true
|
|
||||||
},
|
|
||||||
|
|
||||||
"include": ["./src"]
|
|
||||||
}
|
|
||||||
@@ -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.9",
|
||||||
"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, SubscriptionSubRef } from "./types/index.js"
|
||||||
|
|
||||||
|
|
||||||
export interface RenderOptions {
|
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<
|
useSubscribeRefs<
|
||||||
const Refs extends readonly SubscriptionRef.SubscriptionRef<any>[],
|
const Refs extends readonly SubscriptionRef.SubscriptionRef<any>[],
|
||||||
R,
|
R,
|
||||||
|
|||||||
@@ -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"
|
|
||||||
|
|||||||
89
packages/reffuse/src/types/SubscriptionSubRef.ts
Normal file
89
packages/reffuse/src/types/SubscriptionSubRef.ts
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
import { Effect, Effectable, Readable, Ref, Stream, Subscribable, SubscriptionRef, SynchronizedRef, type Types, type Unify } 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>
|
||||||
|
|
||||||
|
readonly [Unify.typeSymbol]?: unknown
|
||||||
|
readonly [Unify.unifySymbol]?: SubscriptionSubRefUnify<this>
|
||||||
|
readonly [Unify.ignoreSymbol]?: SubscriptionSubRefUnifyIgnore
|
||||||
|
}
|
||||||
|
|
||||||
|
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>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SubscriptionSubRefUnify<A extends { [Unify.typeSymbol]?: any }> extends SubscriptionRef.SubscriptionRefUnify<A> {
|
||||||
|
SubscriptionSubRef?: () => Extract<A[Unify.typeSymbol], SubscriptionSubRef<any, any>>
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SubscriptionSubRefUnifyIgnore extends SubscriptionRef.SubscriptionRefUnifyIgnore {
|
||||||
|
SubscriptionRef?: true
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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