64 Commits

Author SHA1 Message Date
Julien Valverdé
16a7dec3fd Merge branch 'next' of git.valverde.cloud:Thilawyn/reffuse into next
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-19 14:03:52 +02:00
Julien Valverdé
2c467dc6ec 0.1.11 (#14)
All checks were successful
Publish / publish (push) Successful in 22s
Lint / lint (push) Successful in 13s
Co-authored-by: Julien Valverdé <julien.valverde@mailo.com>
Reviewed-on: https://gitea:3000/Thilawyn/reffuse/pulls/14
2025-05-19 14:01:41 +02:00
Julien Valverdé
ffba43c259 useScope refactoring
All checks were successful
Lint / lint (push) Successful in 13s
Test build / test-build (pull_request) Successful in 16s
2025-05-19 13:58:12 +02:00
Julien Valverdé
45ab641262 Fix
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-19 13:03:14 +02:00
Julien Valverdé
c7c2f8de62 Example fix
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-19 12:58:00 +02:00
Julien Valverdé
dda868d444 Scope refactoring
All checks were successful
Lint / lint (push) Successful in 14s
2025-05-19 12:49:37 +02:00
Julien Valverdé
b9e787f42b Todos refactoring
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-18 15:45:31 +02:00
Julien Valverdé
1af2a14b52 Fix
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-18 13:57:59 +02:00
Julien Valverdé
861e462ebd Todos example work
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-18 13:52:39 +02:00
Julien Valverdé
b6a127c8a7 SubRefFromGetSet
All checks were successful
Lint / lint (push) Successful in 14s
2025-05-18 12:35:44 +02:00
Julien Valverdé
497e9a34f2 SubRefFromPath
All checks were successful
Lint / lint (push) Successful in 14s
2025-05-18 12:16:25 +02:00
Julien Valverdé
9d0daaa87f Todos example refactoring
All checks were successful
Lint / lint (push) Successful in 14s
2025-05-18 10:52:39 +02:00
Julien Valverdé
557c4a1b97 Update
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-14 06:29:37 +02:00
Julien Valverdé
b395644798 Dependencies upgrade
Some checks failed
Lint / lint (push) Failing after 14s
2025-05-14 06:19:39 +02:00
Julien Valverdé
099a28ca0d Fix
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-11 19:44:46 +02:00
Julien Valverdé
27ca5e643a Merge branch 'next' of git.valverde.cloud:Thilawyn/reffuse into next
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-11 19:22:39 +02:00
Julien Valverdé
64943deaab 0.1.10 (#13)
All checks were successful
Publish / publish (push) Successful in 22s
Lint / lint (push) Successful in 13s
Co-authored-by: Julien Valverdé <julien.valverde@mailo.com>
Reviewed-on: https://gitea:3000/Thilawyn/reffuse/pulls/13
2025-05-11 19:21:06 +02:00
Julien Valverdé
a616e84079 Version bump
All checks were successful
Lint / lint (push) Successful in 13s
Test build / test-build (pull_request) Successful in 16s
2025-05-11 19:19:02 +02:00
Julien Valverdé
c832c3f79a Fix
All checks were successful
Lint / lint (push) Successful in 18s
2025-05-11 19:13:30 +02:00
Julien Valverdé
6f65574ebd Cleanup
All checks were successful
Lint / lint (push) Successful in 14s
2025-05-11 08:11:03 +02:00
Julien Valverdé
9ccabbb627 Tests
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-11 07:10:38 +02:00
Julien Valverdé
5f4087aa40 Cleanup
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-11 07:04:14 +02:00
Julien Valverdé
85b41bda9f Tests
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-11 06:48:58 +02:00
Julien Valverdé
e5a7fe8ad6 useSubscribeStream
All checks were successful
Lint / lint (push) Successful in 14s
2025-05-11 03:17:35 +02:00
Julien Valverdé
59b7115d19 Fix
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-10 05:33:14 +02:00
Julien Valverdé
08af31f0b9 Fix
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-10 05:30:13 +02:00
Julien Valverdé
44fc6bbbc4 Stream tests
All checks were successful
Lint / lint (push) Successful in 15s
2025-05-10 05:26:15 +02:00
Julien Valverdé
904b725753 Dependencies upgrade
All checks were successful
Lint / lint (push) Successful in 14s
2025-05-08 21:09:55 +02:00
Julien Valverdé
73dd7bc160 Cleanup
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-08 19:22:50 +02:00
Julien Valverdé
6bc07d5b2a Tests
All checks were successful
Lint / lint (push) Successful in 12s
2025-05-08 05:59:36 +02:00
Julien Valverdé
70e9b9218d Queue -> PubSub
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-08 04:51:41 +02:00
Julien Valverdé
31b07f842b Fix
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-08 04:42:24 +02:00
Julien Valverdé
10f23d4cb4 Fix
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-08 01:28:41 +02:00
Julien Valverdé
39765102db useStreamFromReactiveValues
All checks were successful
Lint / lint (push) Successful in 14s
2025-05-08 01:15:35 +02:00
Julien Valverdé
04e78e1ea3 Tests
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-07 04:53:40 +02:00
Julien Valverdé
606dd2c00f useStreamFromReactiveValues
All checks were successful
Lint / lint (push) Successful in 12s
2025-05-07 02:15:46 +02:00
Julien Valverdé
c13a8d549f useStreamFromReactiveValues
All checks were successful
Lint / lint (push) Successful in 15s
2025-05-07 02:07:50 +02:00
Julien Valverdé
4b9bfd0637 Stream PubSub
All checks were successful
Lint / lint (push) Successful in 14s
2025-05-05 21:52:55 +02:00
Julien Valverdé
53fc1ef505 Fix
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-05 20:38:39 +02:00
Julien Valverdé
c8b675d93e Fix
All checks were successful
Lint / lint (push) Successful in 14s
2025-05-05 03:33:50 +02:00
Julien Valverdé
882ec9591c Fix
All checks were successful
Lint / lint (push) Successful in 14s
2025-05-05 03:09:17 +02:00
Julien Valverdé
5b3637afd8 useSubscribeStream work
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-05 02:39:12 +02:00
Julien Valverdé
d6256a7cfd Stream hooks work
All checks were successful
Lint / lint (push) Successful in 14s
2025-05-04 01:18:29 +02:00
Julien Valverdé
cf6c84ff8e useScope fix
All checks were successful
Lint / lint (push) Successful in 14s
2025-05-02 21:56:46 +02:00
Julien Valverdé
198a7cee03 Fix
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-01 21:59:51 +02:00
Julien Valverdé
032f283ac8 Pull example
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-01 21:56:58 +02:00
Julien Valverdé
c34629e20d usePullStream
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-01 21:08:52 +02:00
Julien Valverdé
284a080f19 usePullStream
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-01 20:27:11 +02:00
Julien Valverdé
87d27dd48d useScope work
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-01 19:18:54 +02:00
Julien Valverdé
24853561f1 Working useScope
All checks were successful
Lint / lint (push) Successful in 15s
2025-05-01 17:10:45 +02:00
Julien Valverdé
1902ad373f useScope tests
All checks were successful
Lint / lint (push) Successful in 18s
2025-05-01 16:48:37 +02:00
Julien Valverdé
aa6c4a8008 Fix
All checks were successful
Lint / lint (push) Successful in 13s
2025-05-01 16:19:42 +02:00
Julien Valverdé
d5ac84b2cc Fix
All checks were successful
Lint / lint (push) Successful in 14s
2025-05-01 16:17:01 +02:00
Julien Valverdé
3c604abcef useScope
Some checks failed
Lint / lint (push) Failing after 11s
2025-05-01 03:01:18 +02:00
Julien Valverdé
ba99309877 useSubscribePullStream
Some checks failed
Lint / lint (push) Failing after 12s
2025-04-30 23:41:03 +02:00
Julien Valverdé
db3cd05851 Fix
All checks were successful
Lint / lint (push) Successful in 14s
2025-04-30 23:00:37 +02:00
Julien Valverdé
dce81be269 Fix
All checks were successful
Lint / lint (push) Successful in 17s
2025-04-30 22:52:38 +02:00
Julien Valverdé
3980c10747 Fix
All checks were successful
Lint / lint (push) Successful in 16s
2025-04-30 21:41:58 +02:00
Julien Valverdé
43a3793dbf Fix
All checks were successful
Lint / lint (push) Successful in 14s
2025-04-30 16:27:08 +02:00
Julien Valverdé
da7044ee9f useRefFromValue
All checks were successful
Lint / lint (push) Successful in 13s
2025-04-30 13:33:21 +02:00
Julien Valverdé
ff5503cfd1 Fix
All checks were successful
Lint / lint (push) Successful in 15s
2025-04-29 19:47:09 +02:00
Julien Valverdé
dc2cfb35e0 Merge branch 'next' of git.valverde.cloud:Thilawyn/reffuse into next
All checks were successful
Lint / lint (push) Successful in 13s
2025-04-27 19:13:26 +02:00
Julien Valverdé
bc8c96635c 0.1.9 (#12)
All checks were successful
Publish / publish (push) Successful in 25s
Lint / lint (push) Successful in 13s
Co-authored-by: Julien Valverdé <julien.valverde@mailo.com>
Reviewed-on: https://gitea:3000/Thilawyn/reffuse/pulls/12
2025-04-27 19:12:09 +02:00
Julien Valverdé
1228c51694 Dependencies upgrade
All checks were successful
Lint / lint (push) Successful in 13s
Test build / test-build (pull_request) Successful in 43s
2025-04-27 19:09:30 +02:00
25 changed files with 726 additions and 463 deletions

544
bun.lock

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
{ {
"name": "@reffuse/monorepo", "name": "@reffuse/monorepo",
"packageManager": "bun@1.2.9", "packageManager": "bun@1.2.13",
"private": true, "private": true,
"workspaces": [ "workspaces": [
"./packages/*" "./packages/*"
@@ -15,9 +15,9 @@
"clean:node": "rm -rf node_modules" "clean:node": "rm -rf node_modules"
}, },
"devDependencies": { "devDependencies": {
"npm-check-updates": "^17.1.18", "npm-check-updates": "^18.0.1",
"npm-sort": "^0.0.4", "npm-sort": "^0.0.4",
"turbo": "^2.5.0", "turbo": "^2.5.3",
"typescript": "^5.8.3" "typescript": "^5.8.3"
} }
} }

View File

@@ -11,41 +11,41 @@
"preview": "vite preview" "preview": "vite preview"
}, },
"devDependencies": { "devDependencies": {
"@eslint/js": "^9.24.0", "@eslint/js": "^9.26.0",
"@tanstack/react-router": "^1.115.3", "@tanstack/react-router": "^1.120.3",
"@tanstack/react-router-devtools": "^1.115.3", "@tanstack/react-router-devtools": "^1.120.3",
"@tanstack/router-plugin": "^1.115.3", "@tanstack/router-plugin": "^1.120.3",
"@thilawyn/thilaschema": "^0.1.4", "@thilawyn/thilaschema": "^0.1.4",
"@types/react": "^19.1.1", "@types/react": "^19.1.4",
"@types/react-dom": "^19.1.2", "@types/react-dom": "^19.1.5",
"@vitejs/plugin-react": "^4.3.4", "@vitejs/plugin-react": "^4.4.1",
"eslint": "^9.24.0", "eslint": "^9.26.0",
"eslint-plugin-react-hooks": "^5.2.0", "eslint-plugin-react-hooks": "^5.2.0",
"eslint-plugin-react-refresh": "^0.4.19", "eslint-plugin-react-refresh": "^0.4.20",
"globals": "^16.0.0", "globals": "^16.1.0",
"react": "^19.1.0", "react": "^19.1.0",
"react-dom": "^19.1.0", "react-dom": "^19.1.0",
"typescript-eslint": "^8.29.1", "typescript-eslint": "^8.32.1",
"vite": "^6.2.6" "vite": "^6.3.5"
}, },
"dependencies": { "dependencies": {
"@effect/platform": "^0.80.8", "@effect/platform": "^0.82.1",
"@effect/platform-browser": "^0.59.8", "@effect/platform-browser": "^0.62.1",
"@radix-ui/themes": "^3.2.1", "@radix-ui/themes": "^3.2.1",
"@reffuse/extension-lazyref": "workspace:*", "@reffuse/extension-lazyref": "workspace:*",
"@reffuse/extension-query": "workspace:*", "@reffuse/extension-query": "workspace:*",
"@typed/async-data": "^0.13.1", "@typed/async-data": "^0.13.1",
"@typed/id": "^0.17.2", "@typed/id": "^0.17.2",
"@typed/lazy-ref": "^0.3.3", "@typed/lazy-ref": "^0.3.3",
"effect": "^3.14.8", "effect": "^3.15.1",
"lucide-react": "^0.487.0", "lucide-react": "^0.510.0",
"mobx": "^6.13.7", "mobx": "^6.13.7",
"reffuse": "workspace:*" "reffuse": "workspace:*"
}, },
"overrides": { "overrides": {
"effect": "^3.14.8", "effect": "^3.15.1",
"@effect/platform": "^0.80.8", "@effect/platform": "^0.82.1",
"@effect/platform-browser": "^0.59.8", "@effect/platform-browser": "^0.62.1",
"@typed/lazy-ref": "^0.3.3", "@typed/lazy-ref": "^0.3.3",
"@typed/async-data": "^0.13.1" "@typed/async-data": "^0.13.1"
} }

View File

@@ -1,7 +1,7 @@
import { AlertDialog, Button, Flex, Text } from "@radix-ui/themes" import { AlertDialog, Button, Flex, Text } from "@radix-ui/themes"
import { Cause, Console, Effect, Either, flow, Match, Option, Stream } from "effect" import { Cause, Console, Effect, Either, flow, Match, Option, Stream } from "effect"
import { useState } from "react" import { useState } from "react"
import { AppQueryErrorHandler } from "./query" import { AppQueryClient } from "./query"
import { R } from "./reffuse" import { R } from "./reffuse"
@@ -9,8 +9,8 @@ export function VQueryErrorHandler() {
const [open, setOpen] = useState(false) const [open, setOpen] = useState(false)
const error = R.useSubscribeStream( const error = R.useSubscribeStream(
R.useMemo(() => AppQueryErrorHandler.pipe( R.useMemo(() => AppQueryClient.pipe(
Effect.map(handler => handler.errors.pipe( Effect.map(client => client.errorHandler.errors.pipe(
Stream.changes, Stream.changes,
Stream.tap(Console.error), Stream.tap(Console.error),
Stream.tap(() => Effect.sync(() => setOpen(true))), Stream.tap(() => Effect.sync(() => setOpen(true))),

View File

@@ -1,6 +1,5 @@
import { ThSchema } from "@thilawyn/thilaschema" import { ThSchema } from "@thilawyn/thilaschema"
import { GetRandomValues, makeUuid4 } from "@typed/id" import { Schema } from "effect"
import { Effect, Schema } from "effect"
export class Todo extends Schema.Class<Todo>("Todo")({ export class Todo extends Schema.Class<Todo>("Todo")({
@@ -18,9 +17,4 @@ export const TodoFromJsonStruct = Schema.Struct({
ThSchema.assertEncodedJsonifiable ThSchema.assertEncodedJsonifiable
) )
export const TodoFromJson = TodoFromJsonStruct.pipe(Schema.compose(Todo)) export const TodoFromJson = Schema.compose(TodoFromJsonStruct, Todo)
export const generateUniqueID = makeUuid4.pipe(
Effect.provide(GetRandomValues.CryptoRandom)
)

View File

@@ -9,11 +9,7 @@ export class AppQueryErrorHandler extends QueryErrorHandler.Service<AppQueryErro
"AppQueryErrorHandler", "AppQueryErrorHandler",
(self, failure, defect) => self.pipe( (self, failure, defect) => self.pipe(
Effect.catchTags({ Effect.catchTag("RequestError", "ResponseError", failure),
RequestError: failure,
ResponseError: failure,
}),
Effect.catchAllDefect(defect), Effect.catchAllDefect(defect),
), ),
) {} ) {}

View File

@@ -3,12 +3,11 @@ import { Clipboard, Geolocation, Permissions } from "@effect/platform-browser"
import { LazyRefExtension } from "@reffuse/extension-lazyref" import { LazyRefExtension } from "@reffuse/extension-lazyref"
import { QueryExtension } from "@reffuse/extension-query" import { QueryExtension } from "@reffuse/extension-query"
import { Reffuse, ReffuseContext } from "reffuse" import { Reffuse, ReffuseContext } from "reffuse"
import { AppQueryClient, AppQueryErrorHandler } from "./query" import { AppQueryClient } from "./query"
export const RootContext = ReffuseContext.make< export const RootContext = ReffuseContext.make<
| AppQueryClient | AppQueryClient
| AppQueryErrorHandler
| Clipboard.Clipboard | Clipboard.Clipboard
| Geolocation.Geolocation | Geolocation.Geolocation
| Permissions.Permissions | Permissions.Permissions

View File

@@ -19,6 +19,7 @@ import { Route as LazyrefImport } from './routes/lazyref'
import { Route as CountImport } from './routes/count' import { Route as CountImport } from './routes/count'
import { Route as BlankImport } from './routes/blank' import { Route as BlankImport } from './routes/blank'
import { Route as IndexImport } from './routes/index' import { Route as IndexImport } from './routes/index'
import { Route as StreamsPullImport } from './routes/streams/pull'
import { Route as QueryUsequeryImport } from './routes/query/usequery' import { Route as QueryUsequeryImport } from './routes/query/usequery'
import { Route as QueryUsemutationImport } from './routes/query/usemutation' import { Route as QueryUsemutationImport } from './routes/query/usemutation'
import { Route as QueryServiceImport } from './routes/query/service' import { Route as QueryServiceImport } from './routes/query/service'
@@ -73,6 +74,12 @@ const IndexRoute = IndexImport.update({
getParentRoute: () => rootRoute, getParentRoute: () => rootRoute,
} as any) } as any)
const StreamsPullRoute = StreamsPullImport.update({
id: '/streams/pull',
path: '/streams/pull',
getParentRoute: () => rootRoute,
} as any)
const QueryUsequeryRoute = QueryUsequeryImport.update({ const QueryUsequeryRoute = QueryUsequeryImport.update({
id: '/query/usequery', id: '/query/usequery',
path: '/query/usequery', path: '/query/usequery',
@@ -172,6 +179,13 @@ declare module '@tanstack/react-router' {
preLoaderRoute: typeof QueryUsequeryImport preLoaderRoute: typeof QueryUsequeryImport
parentRoute: typeof rootRoute parentRoute: typeof rootRoute
} }
'/streams/pull': {
id: '/streams/pull'
path: '/streams/pull'
fullPath: '/streams/pull'
preLoaderRoute: typeof StreamsPullImport
parentRoute: typeof rootRoute
}
} }
} }
@@ -189,6 +203,7 @@ export interface FileRoutesByFullPath {
'/query/service': typeof QueryServiceRoute '/query/service': typeof QueryServiceRoute
'/query/usemutation': typeof QueryUsemutationRoute '/query/usemutation': typeof QueryUsemutationRoute
'/query/usequery': typeof QueryUsequeryRoute '/query/usequery': typeof QueryUsequeryRoute
'/streams/pull': typeof StreamsPullRoute
} }
export interface FileRoutesByTo { export interface FileRoutesByTo {
@@ -203,6 +218,7 @@ export interface FileRoutesByTo {
'/query/service': typeof QueryServiceRoute '/query/service': typeof QueryServiceRoute
'/query/usemutation': typeof QueryUsemutationRoute '/query/usemutation': typeof QueryUsemutationRoute
'/query/usequery': typeof QueryUsequeryRoute '/query/usequery': typeof QueryUsequeryRoute
'/streams/pull': typeof StreamsPullRoute
} }
export interface FileRoutesById { export interface FileRoutesById {
@@ -218,6 +234,7 @@ export interface FileRoutesById {
'/query/service': typeof QueryServiceRoute '/query/service': typeof QueryServiceRoute
'/query/usemutation': typeof QueryUsemutationRoute '/query/usemutation': typeof QueryUsemutationRoute
'/query/usequery': typeof QueryUsequeryRoute '/query/usequery': typeof QueryUsequeryRoute
'/streams/pull': typeof StreamsPullRoute
} }
export interface FileRouteTypes { export interface FileRouteTypes {
@@ -234,6 +251,7 @@ export interface FileRouteTypes {
| '/query/service' | '/query/service'
| '/query/usemutation' | '/query/usemutation'
| '/query/usequery' | '/query/usequery'
| '/streams/pull'
fileRoutesByTo: FileRoutesByTo fileRoutesByTo: FileRoutesByTo
to: to:
| '/' | '/'
@@ -247,6 +265,7 @@ export interface FileRouteTypes {
| '/query/service' | '/query/service'
| '/query/usemutation' | '/query/usemutation'
| '/query/usequery' | '/query/usequery'
| '/streams/pull'
id: id:
| '__root__' | '__root__'
| '/' | '/'
@@ -260,6 +279,7 @@ export interface FileRouteTypes {
| '/query/service' | '/query/service'
| '/query/usemutation' | '/query/usemutation'
| '/query/usequery' | '/query/usequery'
| '/streams/pull'
fileRoutesById: FileRoutesById fileRoutesById: FileRoutesById
} }
@@ -275,6 +295,7 @@ export interface RootRouteChildren {
QueryServiceRoute: typeof QueryServiceRoute QueryServiceRoute: typeof QueryServiceRoute
QueryUsemutationRoute: typeof QueryUsemutationRoute QueryUsemutationRoute: typeof QueryUsemutationRoute
QueryUsequeryRoute: typeof QueryUsequeryRoute QueryUsequeryRoute: typeof QueryUsequeryRoute
StreamsPullRoute: typeof StreamsPullRoute
} }
const rootRouteChildren: RootRouteChildren = { const rootRouteChildren: RootRouteChildren = {
@@ -289,6 +310,7 @@ const rootRouteChildren: RootRouteChildren = {
QueryServiceRoute: QueryServiceRoute, QueryServiceRoute: QueryServiceRoute,
QueryUsemutationRoute: QueryUsemutationRoute, QueryUsemutationRoute: QueryUsemutationRoute,
QueryUsequeryRoute: QueryUsequeryRoute, QueryUsequeryRoute: QueryUsequeryRoute,
StreamsPullRoute: StreamsPullRoute,
} }
export const routeTree = rootRoute export const routeTree = rootRoute
@@ -311,7 +333,8 @@ export const routeTree = rootRoute
"/todos", "/todos",
"/query/service", "/query/service",
"/query/usemutation", "/query/usemutation",
"/query/usequery" "/query/usequery",
"/streams/pull"
] ]
}, },
"/": { "/": {
@@ -346,6 +369,9 @@ export const routeTree = rootRoute
}, },
"/query/usequery": { "/query/usequery": {
"filePath": "query/usequery.tsx" "filePath": "query/usequery.tsx"
},
"/streams/pull": {
"filePath": "streams/pull.tsx"
} }
} }
} }

View File

@@ -1,6 +1,6 @@
import { R } from "@/reffuse" import { R } from "@/reffuse"
import { createFileRoute } from "@tanstack/react-router" import { createFileRoute } from "@tanstack/react-router"
import { Ref } from "effect" import { Effect, Ref } from "effect"
export const Route = createFileRoute("/count")({ export const Route = createFileRoute("/count")({
@@ -11,14 +11,13 @@ function Count() {
const runSync = R.useRunSync() const runSync = R.useRunSync()
const countRef = R.useRef(0) const countRef = R.useRef(() => Effect.succeed(0))
const [count] = R.useRefState(countRef) const [count] = R.useSubscribeRefs(countRef)
return ( return (
<div className="container mx-auto"> <div className="container mx-auto">
{/* <button onClick={() => setCount((count) => count + 1)}> */} <button onClick={() => runSync(Ref.update(countRef, count => count + 1))}>
<button onClick={() => Ref.update(countRef, count => count + 1).pipe(runSync)}>
count is {count} count is {count}
</button> </button>
</div> </div>

View File

@@ -14,7 +14,7 @@ export const Route = createFileRoute("/query/service")({
function RouteComponent() { function RouteComponent() {
const query = R.useQuery({ const query = R.useQuery({
key: R.useStreamFromValues(["uuid4", 10 as number]), key: R.useStreamFromReactiveValues(["uuid4", 10 as number]),
query: ([, count]) => Console.log(`Querying ${ count } IDs...`).pipe( query: ([, count]) => Console.log(`Querying ${ count } IDs...`).pipe(
Effect.andThen(Effect.sleep("500 millis")), Effect.andThen(Effect.sleep("500 millis")),
Effect.andThen(HttpClient.get(`https://www.uuidtools.com/api/generate/v4/count/${ count }`)), Effect.andThen(HttpClient.get(`https://www.uuidtools.com/api/generate/v4/count/${ count }`)),

View File

@@ -20,7 +20,7 @@ function RouteComponent() {
const [count, setCount] = useState(1) const [count, setCount] = useState(1)
const query = R.useQuery({ const query = R.useQuery({
key: R.useStreamFromValues(["uuid4", count]), key: R.useStreamFromReactiveValues(["uuid4", count]),
query: ([, count]) => Console.log(`Querying ${ count } IDs...`).pipe( query: ([, count]) => Console.log(`Querying ${ count } IDs...`).pipe(
Effect.andThen(Effect.sleep("500 millis")), Effect.andThen(Effect.sleep("500 millis")),
Effect.andThen(HttpClient.get(`https://www.uuidtools.com/api/generate/v4/count/${ count }`)), Effect.andThen(HttpClient.get(`https://www.uuidtools.com/api/generate/v4/count/${ count }`)),

View File

@@ -0,0 +1,34 @@
import { R } from "@/reffuse"
import { Button, Flex, Text } from "@radix-ui/themes"
import { createFileRoute } from "@tanstack/react-router"
import { Chunk, Effect, Exit, Option, Queue, Random, Scope, Stream } from "effect"
import { useMemo, useState } from "react"
export const Route = createFileRoute("/streams/pull")({
component: RouteComponent
})
function RouteComponent() {
const stream = useMemo(() => Stream.repeatEffect(Random.nextInt), [])
const streamScope = R.useScope([stream], { finalizerExecutionMode: "fork" })
const queue = R.useMemo(() => Effect.provideService(Stream.toQueueOfElements(stream), Scope.Scope, streamScope), [streamScope])
const [value, setValue] = useState(Option.none<number>())
const pullLatest = R.useCallbackSync(() => Queue.takeAll(queue).pipe(
Effect.flatMap(Chunk.last),
Effect.flatMap(Exit.matchEffect({
onSuccess: Effect.succeed,
onFailure: Effect.fail,
})),
Effect.tap(v => Effect.sync(() => setValue(Option.some(v)))),
), [queue])
return (
<Flex direction="column" align="center" gap="2">
{Option.isSome(value) && <Text>{value.value}</Text>}
<Button onClick={pullLatest}>Pull latest</Button>
</Flex>
)
}

View File

@@ -2,7 +2,11 @@ import { R } from "@/reffuse"
import { Button, Flex, Text } from "@radix-ui/themes" 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, Option } from "effect"
import { useEffect, useState } from "react"
const makeUuid = Effect.provide(makeUuid4, GetRandomValues.CryptoRandom)
export const Route = createFileRoute("/tests")({ export const Route = createFileRoute("/tests")({
@@ -10,48 +14,34 @@ export const Route = createFileRoute("/tests")({
}) })
function RouteComponent() { function RouteComponent() {
const deepRef = R.useRef({ value: "poulet" }) const runSync = R.useRunSync()
const deepValueRef = R.useSubRef(deepRef, ["value"])
// const value = R.useMemoScoped(Effect.addFinalizer(() => Console.log("cleanup")).pipe( const [uuid, setUuid] = useState(R.useMemo(() => makeUuid, []))
// Effect.andThen(makeUuid4), const generateUuid = R.useCallbackSync(() => makeUuid.pipe(
// Effect.provide(GetRandomValues.CryptoRandom), Effect.tap(v => Effect.sync(() => setUuid(v)))
// ), [])
// console.log(value)
R.useFork(() => Effect.addFinalizer(() => Console.log("cleanup")).pipe(
Effect.andThen(Console.log("ouient")),
Effect.delay("1 second"),
), []) ), [])
const uuidStream = R.useStreamFromReactiveValues([uuid])
const uuidStreamLatestValue = R.useSubscribeStream(uuidStream)
const uuidRef = R.useRef("none") const [, scopeLayer] = R.useScope([uuid])
const anotherRef = R.useRef(69)
const logValue = R.useCallbackSync(Effect.fn(function*(value: string) {
yield* Effect.log(value)
}), [])
const generateUuid = R.useCallbackSync(() => makeUuid4.pipe(
Effect.provide(GetRandomValues.CryptoRandom),
Effect.tap(v => Ref.set(uuidRef, v)),
Effect.tap(v => Ref.set(deepValueRef, v)),
), [])
useEffect(() => Effect.addFinalizer(() => Console.log("Scope cleanup!")).pipe(
Effect.andThen(Console.log("Scope changed")),
Effect.provide(scopeLayer),
runSync,
), [scopeLayer, runSync])
return ( return (
<Flex direction="row" justify="center" align="center" gap="2"> <Flex direction="column" justify="center" align="center" gap="2">
<R.SubscribeRefs refs={[uuidRef, anotherRef]}> <Text>{uuid}</Text>
{(uuid, anotherRef) => <Text>{uuid} / {anotherRef}</Text>} <Button onClick={generateUuid}>Generate UUID</Button>
</R.SubscribeRefs> <Text>
{Option.match(uuidStreamLatestValue, {
<R.SubscribeRefs refs={[deepRef, deepValueRef]}> onSome: ([v]) => v,
{(deep, deepValue) => <Text>{JSON.stringify(deep)} / {deepValue}</Text>} onNone: () => <></>,
</R.SubscribeRefs> })}
</Text>
<Button onClick={() => logValue("test")}>Log value</Button>
<Button onClick={() => generateUuid()}>Generate UUID</Button>
</Flex> </Flex>
) )
} }

View File

@@ -26,7 +26,7 @@ function Todos() {
return ( return (
<Container> <Container>
<TodosContext.Provider layer={todosLayer}> <TodosContext.Provider layer={todosLayer} finalizerExecutionMode="fork">
<VTodos /> <VTodos />
</TodosContext.Provider> </TodosContext.Provider>
</Container> </Container>

View File

@@ -2,68 +2,43 @@ import { Todo } from "@/domain"
import { KeyValueStore } from "@effect/platform" import { KeyValueStore } from "@effect/platform"
import { BrowserKeyValueStore } from "@effect/platform-browser" import { BrowserKeyValueStore } from "@effect/platform-browser"
import { PlatformError } from "@effect/platform/Error" import { PlatformError } from "@effect/platform/Error"
import { Chunk, Context, Effect, identity, Layer, ParseResult, Ref, Schema, SubscriptionRef } from "effect" import { Chunk, Context, Effect, identity, Layer, ParseResult, Ref, Schema, Stream, SubscriptionRef } from "effect"
export class TodosState extends Context.Tag("TodosState")<TodosState, { export class TodosState extends Context.Tag("TodosState")<TodosState, {
readonly todos: SubscriptionRef.SubscriptionRef<Chunk.Chunk<Todo.Todo>> readonly todos: SubscriptionRef.SubscriptionRef<Chunk.Chunk<Todo.Todo>>
readonly load: Effect.Effect<void, PlatformError | ParseResult.ParseError>
readonly readFromLocalStorage: Effect.Effect<void, PlatformError | ParseResult.ParseError> readonly save: Effect.Effect<void, PlatformError | ParseResult.ParseError>
readonly saveToLocalStorage: Effect.Effect<void, PlatformError | ParseResult.ParseError>
readonly prepend: (todo: Todo.Todo) => Effect.Effect<void>
readonly replace: (index: number, todo: Todo.Todo) => Effect.Effect<void>
readonly remove: (index: number) => Effect.Effect<void>
// readonly moveUp: (index: number) => Effect.Effect<void, Cause.NoSuchElementException>
// readonly moveDown: (index: number) => Effect.Effect<void, Cause.NoSuchElementException>
}>() {} }>() {}
export const make = (key: string) => Layer.effect(TodosState, Effect.gen(function*() { export const make = (key: string) => Layer.effect(TodosState, Effect.gen(function*() {
const todos = yield* SubscriptionRef.make(Chunk.empty<Todo.Todo>())
const readFromLocalStorage = KeyValueStore.KeyValueStore.pipe( const readFromLocalStorage = KeyValueStore.KeyValueStore.pipe(
Effect.flatMap(kv => kv.get(key)), Effect.flatMap(kv => kv.get(key)),
Effect.flatMap(identity), Effect.flatMap(identity),
Effect.flatMap(Schema.parseJson().pipe( Effect.flatMap(Schema.decode(
Schema.compose(Schema.Chunk(Todo.TodoFromJson)), Schema.compose(Schema.parseJson(), Schema.Chunk(Todo.TodoFromJson))
Schema.decode,
)), )),
Effect.flatMap(v => Ref.set(todos, v)), Effect.catchTag("NoSuchElementException", () => Effect.succeed(Chunk.empty<Todo.Todo>())),
Effect.catchTag("NoSuchElementException", () => Ref.set(todos, Chunk.empty())),
Effect.provide(BrowserKeyValueStore.layerLocalStorage), Effect.provide(BrowserKeyValueStore.layerLocalStorage),
) )
const saveToLocalStorage = Effect.all([KeyValueStore.KeyValueStore, todos]).pipe( const writeToLocalStorage = (values: Chunk.Chunk<Todo.Todo>) => KeyValueStore.KeyValueStore.pipe(
Effect.flatMap(([kv, values]) => values.pipe( Effect.flatMap(kv => values.pipe(
Schema.parseJson().pipe( Schema.encode(
Schema.compose(Schema.Chunk(Todo.TodoFromJson)), Schema.compose(Schema.parseJson(), Schema.Chunk(Todo.TodoFromJson))
Schema.encode,
), ),
Effect.flatMap(v => kv.set(key, v)), Effect.flatMap(v => kv.set(key, v)),
)), )),
Effect.provide(BrowserKeyValueStore.layerLocalStorage), Effect.provide(BrowserKeyValueStore.layerLocalStorage),
) )
const prepend = (todo: Todo.Todo) => Ref.update(todos, Chunk.prepend(todo)) const todos = yield* SubscriptionRef.make(yield* readFromLocalStorage)
const replace = (index: number, todo: Todo.Todo) => Ref.update(todos, Chunk.replace(index, todo)) const load = Effect.flatMap(readFromLocalStorage, v => Ref.set(todos, v))
const remove = (index: number) => Ref.update(todos, Chunk.remove(index)) const save = Effect.flatMap(todos, writeToLocalStorage)
// const moveUp = (index: number) => Effect.gen(function*() { // Sync changes with local storage
yield* Effect.forkScoped(Stream.runForEach(todos.changes, writeToLocalStorage))
// }) return { todos, load, save }
yield* readFromLocalStorage
return {
todos,
readFromLocalStorage,
saveToLocalStorage,
prepend,
replace,
remove,
}
})) }))

View File

@@ -1,25 +1,27 @@
import { Todo } from "@/domain" import { Todo } from "@/domain"
import { Box, Button, Card, Flex, TextArea } from "@radix-ui/themes" import { Box, Button, Card, Flex, TextArea } from "@radix-ui/themes"
import { Effect, Option, SubscriptionRef } from "effect" import { GetRandomValues, makeUuid4 } from "@typed/id"
import { Chunk, Effect, Option, Ref } from "effect"
import { R } from "../reffuse" import { R } from "../reffuse"
import { TodosState } from "../services" import { TodosState } from "../services"
const createEmptyTodo = Todo.generateUniqueID.pipe( const createEmptyTodo = makeUuid4.pipe(
Effect.map(id => Todo.Todo.make({ Effect.map(id => Todo.Todo.make({ id, content: "", completedAt: Option.none()}, true)),
id, Effect.provide(GetRandomValues.CryptoRandom),
content: "",
completedAt: Option.none(),
}, true))
) )
export function VNewTodo() { export function VNewTodo() {
const runSync = R.useRunSync() const todoRef = R.useRef(() => createEmptyTodo)
const [content, setContent] = R.useRefState(R.useSubRefFromPath(todoRef, ["content"]))
const todoRef = R.useMemo(() => createEmptyTodo.pipe(Effect.flatMap(SubscriptionRef.make)), []) const add = R.useCallbackSync(() => Effect.all([TodosState.TodosState, todoRef]).pipe(
const [todo, setTodo] = R.useRefState(todoRef) Effect.flatMap(([state, todo]) => Ref.update(state.todos, Chunk.prepend(todo))),
Effect.andThen(createEmptyTodo),
Effect.flatMap(v => Ref.set(todoRef, v)),
), [todoRef])
return ( return (
@@ -27,23 +29,12 @@ export function VNewTodo() {
<Card> <Card>
<Flex direction="column" align="stretch" gap="2"> <Flex direction="column" align="stretch" gap="2">
<TextArea <TextArea
value={todo.content} value={content}
onChange={e => setTodo(prev => onChange={e => setContent(e.target.value)}
Todo.Todo.make({ ...prev, content: e.target.value }, true)
)}
/> />
<Flex direction="row" justify="center" align="center"> <Flex direction="row" justify="center" align="center">
<Button <Button onClick={add}>Add</Button>
onClick={() => TodosState.TodosState.pipe(
Effect.flatMap(state => state.prepend(todo)),
Effect.andThen(createEmptyTodo),
Effect.map(setTodo),
runSync,
)}
>
Add
</Button>
</Flex> </Flex>
</Flex> </Flex>
</Card> </Card>

View File

@@ -1,20 +1,28 @@
import { Todo } from "@/domain" import { Todo } from "@/domain"
import { Box, Card, Flex, IconButton, TextArea } from "@radix-ui/themes" import { Box, Card, Flex, IconButton, TextArea } from "@radix-ui/themes"
import { Effect } from "effect" import { Effect, Ref, Stream, SubscriptionRef } from "effect"
import { Delete } from "lucide-react" import { Delete } from "lucide-react"
import { useState } from "react" import { useState } from "react"
import { R } from "../reffuse" import { R } from "../reffuse"
import { TodosState } from "../services"
export interface VTodoProps { export interface VTodoProps {
readonly index: number readonly todoRef: SubscriptionRef.SubscriptionRef<Todo.Todo>
readonly todo: Todo.Todo readonly remove: Effect.Effect<void>
} }
export function VTodo({ index, todo }: VTodoProps) { export function VTodo({ todoRef, remove }: VTodoProps) {
const runSync = R.useRunSync() const runSync = R.useRunSync()
const localTodoRef = R.useRef(() => todoRef)
const [content, setContent] = R.useRefState(R.useSubRefFromPath(localTodoRef, ["content"]))
R.useFork(() => localTodoRef.changes.pipe(
Stream.debounce("250 millis"),
Stream.runForEach(v => Ref.set(todoRef, v)),
), [localTodoRef])
const editorMode = useState(false) const editorMode = useState(false)
@@ -23,14 +31,8 @@ export function VTodo({ index, todo }: VTodoProps) {
<Card> <Card>
<Flex direction="column" align="stretch" gap="1"> <Flex direction="column" align="stretch" gap="1">
<TextArea <TextArea
value={todo.content} value={content}
onChange={e => TodosState.TodosState.pipe( onChange={e => setContent(e.target.value)}
Effect.flatMap(state => state.replace(
index,
Todo.Todo.make({ ...todo, content: e.target.value }, true),
)),
runSync,
)}
disabled={!editorMode} disabled={!editorMode}
/> />
@@ -38,12 +40,7 @@ export function VTodo({ index, todo }: VTodoProps) {
<Box></Box> <Box></Box>
<Flex direction="row" align="center" gap="1"> <Flex direction="row" align="center" gap="1">
<IconButton <IconButton onClick={() => runSync(remove)}>
onClick={() => TodosState.TodosState.pipe(
Effect.flatMap(state => state.remove(index)),
runSync,
)}
>
<Delete /> <Delete />
</IconButton> </IconButton>
</Flex> </Flex>

View File

@@ -1,5 +1,5 @@
import { Box, Flex } from "@radix-ui/themes" import { Box, Flex } from "@radix-ui/themes"
import { Chunk, Effect, Stream } from "effect" import { Chunk, Effect, Ref } from "effect"
import { R } from "../reffuse" import { R } from "../reffuse"
import { TodosState } from "../services" import { TodosState } from "../services"
import { VNewTodo } from "./VNewTodo" import { VNewTodo } from "./VNewTodo"
@@ -8,15 +8,8 @@ import { VTodo } from "./VTodo"
export function VTodos() { export function VTodos() {
// Sync changes to the todos with the local storage const todosRef = R.useMemo(() => Effect.map(TodosState.TodosState, state => state.todos), [])
R.useFork(() => TodosState.TodosState.pipe( const [todos] = R.useSubscribeRefs(todosRef)
Effect.flatMap(state =>
Stream.runForEach(state.todos.changes, () => state.saveToLocalStorage)
)
), [])
const todosRef = R.useMemo(() => TodosState.TodosState.pipe(Effect.map(state => state.todos)), [])
const [todos] = R.useRefState(todosRef)
return ( return (
@@ -27,7 +20,16 @@ export function VTodos() {
{Chunk.map(todos, (todo, index) => ( {Chunk.map(todos, (todo, index) => (
<Box key={todo.id} width="500px"> <Box key={todo.id} width="500px">
<VTodo index={index} todo={todo} /> <R.SubRefFromGetSet
parent={todosRef}
getter={parentValue => Chunk.unsafeGet(parentValue, index)}
setter={(parentValue, value) => Chunk.replace(parentValue, index, value)}
>
{ref => <VTodo
todoRef={ref}
remove={Ref.update(todosRef, Chunk.remove(index))}
/>}
</R.SubRefFromGetSet>
</Box> </Box>
))} ))}
</Flex> </Flex>

View File

@@ -35,7 +35,7 @@
"peerDependencies": { "peerDependencies": {
"@typed/lazy-ref": "^0.3.0", "@typed/lazy-ref": "^0.3.0",
"@types/react": "^19.0.0", "@types/react": "^19.0.0",
"effect": "^3.13.0", "effect": "^3.15.0",
"react": "^19.0.0", "react": "^19.0.0",
"reffuse": "^0.1.8" "reffuse": "^0.1.8"
} }

View File

@@ -37,7 +37,7 @@
"@effect/platform-browser": "^0.56.0", "@effect/platform-browser": "^0.56.0",
"@typed/async-data": "^0.13.0", "@typed/async-data": "^0.13.0",
"@types/react": "^19.0.0", "@types/react": "^19.0.0",
"effect": "^3.13.0", "effect": "^3.15.0",
"react": "^19.0.0", "react": "^19.0.0",
"reffuse": "^0.1.6" "reffuse": "^0.1.6"
} }

View File

@@ -1,4 +1,4 @@
import { Cause, Context, Effect, identity, Layer, Queue, Stream } from "effect" import { Cause, Context, Effect, identity, Layer, PubSub, Stream } from "effect"
import type { Mutable } from "effect/Types" import type { Mutable } from "effect/Types"
@@ -36,17 +36,17 @@ export const Service = <Self, HandledE = never>() => (
const TagClass = Context.Tag(id)() as ServiceResult<Self, Id, FallbackA, HandledE> const TagClass = Context.Tag(id)() as ServiceResult<Self, Id, FallbackA, HandledE>
(TagClass as Mutable<typeof TagClass>).Live = Layer.effect(TagClass, Effect.gen(function*() { (TagClass as Mutable<typeof TagClass>).Live = Layer.effect(TagClass, Effect.gen(function*() {
const queue = yield* Queue.unbounded<Cause.Cause<HandledE>>() const pubsub = yield* PubSub.unbounded<Cause.Cause<HandledE>>()
const errors = Stream.fromQueue(queue) const errors = Stream.fromPubSub(pubsub)
const handle = <A, E, R>( const handle = <A, E, R>(
self: Effect.Effect<A, E, R> self: Effect.Effect<A, E, R>
): Effect.Effect<A | FallbackA, Exclude<E, HandledE>, R> => f( ): Effect.Effect<A | FallbackA, Exclude<E, HandledE>, R> => f(
self as unknown as Effect.Effect<never, HandledE, never>, self as unknown as Effect.Effect<never, HandledE, never>,
(failure: HandledE) => Queue.offer(queue, Cause.fail(failure)).pipe( (failure: HandledE) => PubSub.publish(pubsub, Cause.fail(failure)).pipe(
Effect.andThen(Effect.failCause(Cause.empty)) Effect.andThen(Effect.failCause(Cause.empty))
), ),
(defect: unknown) => Queue.offer(queue, Cause.die(defect)).pipe( (defect: unknown) => PubSub.publish(pubsub, Cause.die(defect)).pipe(
Effect.andThen(Effect.failCause(Cause.empty)) Effect.andThen(Effect.failCause(Cause.empty))
), ),
) )

View File

@@ -1,6 +1,6 @@
{ {
"name": "reffuse", "name": "reffuse",
"version": "0.1.9", "version": "0.1.11",
"type": "module", "type": "module",
"files": [ "files": [
"./README.md", "./README.md",
@@ -35,7 +35,7 @@
}, },
"peerDependencies": { "peerDependencies": {
"@types/react": "^19.0.0", "@types/react": "^19.0.0",
"effect": "^3.13.0", "effect": "^3.15.0",
"react": "^19.0.0" "react": "^19.0.0"
} }
} }

View File

@@ -1,4 +1,4 @@
import { Array, Context, Effect, ExecutionStrategy, Exit, Layer, Ref, Runtime, Scope } from "effect" import { Array, Context, Effect, ExecutionStrategy, Exit, Layer, Match, Ref, Runtime, Scope } from "effect"
import * as React from "react" import * as React from "react"
import * as ReffuseRuntime from "./ReffuseRuntime.js" import * as ReffuseRuntime from "./ReffuseRuntime.js"
@@ -25,6 +25,8 @@ export type R<T> = T extends ReffuseContext<infer R> ? R : never
export type ReactProvider<R> = React.FC<{ export type ReactProvider<R> = React.FC<{
readonly layer: Layer.Layer<R, unknown, Scope.Scope> readonly layer: Layer.Layer<R, unknown, Scope.Scope>
readonly scope?: Scope.Scope readonly scope?: Scope.Scope
readonly finalizerExecutionStrategy?: ExecutionStrategy.ExecutionStrategy
readonly finalizerExecutionMode?: "sync" | "fork"
readonly children?: React.ReactNode readonly children?: React.ReactNode
}> }>
@@ -32,16 +34,25 @@ const makeProvider = <R>(Context: React.Context<Context.Context<R>>): ReactProvi
return function ReffuseContextReactProvider(props) { return function ReffuseContextReactProvider(props) {
const runtime = ReffuseRuntime.useRuntime() const runtime = ReffuseRuntime.useRuntime()
const runSync = React.useMemo(() => Runtime.runSync(runtime), [runtime]) const runSync = React.useMemo(() => Runtime.runSync(runtime), [runtime])
const runFork = React.useMemo(() => Runtime.runFork(runtime), [runtime])
const makeScope = React.useMemo(() => props.scope const makeScope = React.useMemo(() => props.scope
? Scope.fork(props.scope, ExecutionStrategy.sequential) ? Scope.fork(props.scope, props.finalizerExecutionStrategy ?? ExecutionStrategy.sequential)
: Scope.make(), : Scope.make(props.finalizerExecutionStrategy ?? ExecutionStrategy.sequential),
[props.scope]) [props.scope])
const makeContext = React.useCallback((scope: Scope.CloseableScope) => Effect.context<R>().pipe( const makeContext = (scope: Scope.CloseableScope) => Effect.context<R>().pipe(
Effect.provide(props.layer), Effect.provide(props.layer),
Effect.provideService(Scope.Scope, scope), Effect.provideService(Scope.Scope, scope),
), [props.layer]) )
const closeScope = (scope: Scope.CloseableScope) => Scope.close(scope, Exit.void).pipe(
effect => Match.value(props.finalizerExecutionMode ?? "sync").pipe(
Match.when("sync", () => { runSync(effect) }),
Match.when("fork", () => { runFork(effect) }),
Match.exhaustive,
)
)
const [isInitialRun, initialScope, initialValue] = React.useMemo(() => Effect.Do.pipe( const [isInitialRun, initialScope, initialValue] = React.useMemo(() => Effect.Do.pipe(
Effect.bind("isInitialRun", () => Ref.make(true)), Effect.bind("isInitialRun", () => Ref.make(true)),
@@ -57,7 +68,7 @@ const makeProvider = <R>(Context: React.Context<Context.Context<R>>): ReactProvi
Effect.if({ Effect.if({
onTrue: () => Ref.set(isInitialRun, false).pipe( onTrue: () => Ref.set(isInitialRun, false).pipe(
Effect.map(() => Effect.map(() =>
() => runSync(Scope.close(initialScope, Exit.void)) () => closeScope(initialScope)
) )
), ),
@@ -68,13 +79,13 @@ const makeProvider = <R>(Context: React.Context<Context.Context<R>>): ReactProvi
Effect.sync(() => setValue(context)) Effect.sync(() => setValue(context))
), ),
Effect.map(({ scope }) => Effect.map(({ scope }) =>
() => runSync(Scope.close(scope, Exit.void)) () => closeScope(scope)
), ),
), ),
}), }),
runSync, runSync,
), [makeScope, makeContext, runSync]) ), [makeScope, runSync, runFork])
return React.createElement(Context, { ...props, value }) return React.createElement(Context, { ...props, value })
} }
@@ -84,6 +95,7 @@ export type AsyncReactProvider<R> = React.FC<{
readonly layer: Layer.Layer<R, unknown, Scope.Scope> readonly layer: Layer.Layer<R, unknown, Scope.Scope>
readonly scope?: Scope.Scope readonly scope?: Scope.Scope
readonly finalizerExecutionStrategy?: ExecutionStrategy.ExecutionStrategy readonly finalizerExecutionStrategy?: ExecutionStrategy.ExecutionStrategy
readonly finalizerExecutionMode?: "sync" | "fork"
readonly fallback?: React.ReactNode readonly fallback?: React.ReactNode
readonly children?: React.ReactNode readonly children?: React.ReactNode
}> }>
@@ -112,7 +124,7 @@ const makeAsyncProvider = <R>(Context: React.Context<Context.Context<R>>): Async
const scope = runSync(props.scope const scope = runSync(props.scope
? Scope.fork(props.scope, props.finalizerExecutionStrategy ?? ExecutionStrategy.sequential) ? Scope.fork(props.scope, props.finalizerExecutionStrategy ?? ExecutionStrategy.sequential)
: Scope.make(props.finalizerExecutionStrategy) : Scope.make(props.finalizerExecutionStrategy ?? ExecutionStrategy.sequential)
) )
Effect.context<R>().pipe( Effect.context<R>().pipe(
@@ -126,7 +138,13 @@ const makeAsyncProvider = <R>(Context: React.Context<Context.Context<R>>): Async
effect => runFork(effect, { ...props, scope }), effect => runFork(effect, { ...props, scope }),
) )
return () => { runFork(Scope.close(scope, Exit.void)) } return () => Scope.close(scope, Exit.void).pipe(
effect => Match.value(props.finalizerExecutionMode ?? "sync").pipe(
Match.when("sync", () => { runSync(effect) }),
Match.when("fork", () => { runFork(effect) }),
Match.exhaustive,
)
)
}, [props.layer, runSync, runFork]) }, [props.layer, runSync, runFork])
return React.createElement(React.Suspense, { return React.createElement(React.Suspense, {

View File

@@ -1,4 +1,4 @@
import { type Context, Effect, ExecutionStrategy, Exit, type Fiber, type Layer, Option, pipe, Pipeable, Queue, Ref, Runtime, Scope, Stream, SubscriptionRef } from "effect" import { type Context, Effect, ExecutionStrategy, Exit, type Fiber, Layer, Match, Option, pipe, Pipeable, PubSub, Ref, Runtime, Scope, Stream, SubscriptionRef } from "effect"
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"
@@ -14,6 +14,11 @@ export interface ScopeOptions {
readonly finalizerExecutionStrategy?: ExecutionStrategy.ExecutionStrategy readonly finalizerExecutionStrategy?: ExecutionStrategy.ExecutionStrategy
} }
export interface UseScopeOptions extends RenderOptions, ScopeOptions {
readonly scope?: Scope.Scope
readonly finalizerExecutionMode?: "sync" | "fork"
}
export type RefsA<T extends readonly SubscriptionRef.SubscriptionRef<any>[]> = { export type RefsA<T extends readonly SubscriptionRef.SubscriptionRef<any>[]> = {
[K in keyof T]: Effect.Effect.Success<T[K]> [K in keyof T]: Effect.Effect.Success<T[K]>
} }
@@ -23,7 +28,8 @@ 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.SubRefFromGetSet = this.SubRefFromGetSet.bind(this as any) as any
this.SubRefFromPath = this.SubRefFromPath.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
@@ -88,6 +94,56 @@ export abstract class ReffuseNamespace<R> {
), [runtime, context]) ), [runtime, context])
} }
useScope<R>(
this: ReffuseNamespace<R>,
deps: React.DependencyList = [],
options?: UseScopeOptions,
): readonly [scope: Scope.Scope, layer: Layer.Layer<Scope.Scope>] {
const runSync = this.useRunSync()
const runFork = this.useRunFork()
const makeScope = React.useMemo(() => options?.scope
? Scope.fork(options.scope, options.finalizerExecutionStrategy ?? ExecutionStrategy.sequential)
: Scope.make(options?.finalizerExecutionStrategy ?? ExecutionStrategy.sequential),
[options?.scope])
const closeScope = (scope: Scope.CloseableScope) => Scope.close(scope, Exit.void).pipe(
effect => Match.value(options?.finalizerExecutionMode ?? "sync").pipe(
Match.when("sync", () => { runSync(effect) }),
Match.when("fork", () => { runFork(effect) }),
Match.exhaustive,
)
)
const [isInitialRun, initialScope] = React.useMemo(() => runSync(
Effect.all([Ref.make(true), makeScope])
), [makeScope])
const [scope, setScope] = React.useState(initialScope)
React.useEffect(() => isInitialRun.pipe(
Effect.if({
onTrue: () => Effect.as(
Ref.set(isInitialRun, false),
() => closeScope(initialScope),
),
onFalse: () => makeScope.pipe(
Effect.tap(v => Effect.sync(() => setScope(v))),
Effect.map(v => () => closeScope(v)),
),
}),
runSync,
), [
makeScope,
...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync, runFork],
...deps,
])
return React.useMemo(() => [scope, Layer.succeed(Scope.Scope, scope)] as const, [scope])
}
/** /**
* Reffuse equivalent to `React.useMemo`. * Reffuse equivalent to `React.useMemo`.
* *
@@ -111,53 +167,6 @@ export abstract class ReffuseNamespace<R> {
]) ])
} }
useMemoScoped<A, E, R>(
this: ReffuseNamespace<R>,
effect: () => Effect.Effect<A, E, R | Scope.Scope>,
deps: React.DependencyList,
options?: RenderOptions & ScopeOptions,
): A {
const runSync = this.useRunSync()
const [isInitialRun, initialScope, initialValue] = React.useMemo(() => Effect.Do.pipe(
Effect.bind("isInitialRun", () => Ref.make(true)),
Effect.bind("scope", () => Scope.make(options?.finalizerExecutionStrategy)),
Effect.bind("value", ({ scope }) => Effect.provideService(effect(), Scope.Scope, scope)),
Effect.map(({ isInitialRun, scope, value }) => [isInitialRun, scope, value] as const),
runSync,
), [])
const [value, setValue] = React.useState(initialValue)
React.useEffect(() => isInitialRun.pipe(
Effect.if({
onTrue: () => Ref.set(isInitialRun, false).pipe(
Effect.map(() =>
() => runSync(Scope.close(initialScope, Exit.void))
)
),
onFalse: () => Effect.Do.pipe(
Effect.bind("scope", () => Scope.make(options?.finalizerExecutionStrategy)),
Effect.bind("value", ({ scope }) => Effect.provideService(effect(), Scope.Scope, scope)),
Effect.tap(({ value }) =>
Effect.sync(() => setValue(value))
),
Effect.map(({ scope }) =>
() => runSync(Scope.close(scope, Exit.void))
),
),
}),
runSync,
), [
...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync],
...deps,
])
return value
}
/** /**
* Reffuse equivalent to `React.useEffect`. * Reffuse equivalent to `React.useEffect`.
* *
@@ -378,18 +387,39 @@ export abstract class ReffuseNamespace<R> {
]) ])
} }
useRef<A, R>( useRef<A, E, R>(
this: ReffuseNamespace<R>, this: ReffuseNamespace<R>,
value: A, initialValue: () => Effect.Effect<A, E, R>,
): SubscriptionRef.SubscriptionRef<A> { ): SubscriptionRef.SubscriptionRef<A> {
return this.useMemo( return this.useMemo(
() => SubscriptionRef.make(value), () => Effect.flatMap(initialValue(), SubscriptionRef.make),
[], [],
{ doNotReExecuteOnRuntimeOrContextChange: true }, // Do not recreate the ref when the context changes { doNotReExecuteOnRuntimeOrContextChange: true }, // Do not recreate the ref when the context changes
) )
} }
useSubRef<B, const P extends PropertyPath.Paths<B>, R>( useRefFromReactiveValue<A, R>(
this: ReffuseNamespace<R>,
value: A,
): SubscriptionRef.SubscriptionRef<A> {
const ref = this.useRef(() => Effect.succeed(value))
this.useEffect(() => Ref.set(ref, value), [value], { doNotReExecuteOnRuntimeOrContextChange: true })
return ref
}
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>(
this: ReffuseNamespace<R>, this: ReffuseNamespace<R>,
parent: SubscriptionRef.SubscriptionRef<B>, parent: SubscriptionRef.SubscriptionRef<B>,
path: P, path: P,
@@ -455,36 +485,77 @@ export abstract class ReffuseNamespace<R> {
return [reactStateValue, setValue] return [reactStateValue, setValue]
} }
useStreamFromValues<const A extends React.DependencyList, R>( useStreamFromReactiveValues<const A extends React.DependencyList, R>(
this: ReffuseNamespace<R>, this: ReffuseNamespace<R>,
values: A, values: A,
): Stream.Stream<A> { ): Stream.Stream<A> {
const [queue, stream] = this.useMemo(() => Queue.unbounded<A>().pipe( const [, scopeLayer] = this.useScope([], { finalizerExecutionMode: "fork" })
Effect.map(queue => [queue, Stream.fromQueue(queue)] as const)
), [])
this.useEffect(() => Queue.offer(queue, values), values) const { latest, pubsub, stream } = this.useMemo(() => Effect.Do.pipe(
Effect.bind("latest", () => Ref.make(values)),
Effect.bind("pubsub", () => Effect.acquireRelease(PubSub.unbounded<A>(), PubSub.shutdown)),
Effect.let("stream", ({ latest, pubsub }) => Ref.get(latest).pipe(
Effect.flatMap(a => Effect.map(
Stream.fromPubSub(pubsub, { scoped: true }),
s => Stream.concat(Stream.make(a), s),
)),
Stream.unwrapScoped,
)),
Effect.provide(scopeLayer),
), [scopeLayer], { doNotReExecuteOnRuntimeOrContextChange: true })
this.useEffect(() => Ref.set(latest, values).pipe(
Effect.andThen(PubSub.publish(pubsub, values)),
Effect.unlessEffect(PubSub.isShutdown(pubsub)),
), values, { doNotReExecuteOnRuntimeOrContextChange: true })
return stream return stream
} }
useSubscribeStream<A, InitialA extends A | undefined, E, R>( useSubscribeStream<A, E, R>(
this: ReffuseNamespace<R>, this: ReffuseNamespace<R>,
stream: Stream.Stream<A, E, R>, stream: Stream.Stream<A, E, R>,
initialValue?: InitialA, ): Option.Option<A>
): InitialA extends A ? Option.Some<A> : Option.Option<A> { useSubscribeStream<A, E, IE, R>(
const [reactStateValue, setReactStateValue] = React.useState<Option.Option<A>>(Option.fromNullable(initialValue)) this: ReffuseNamespace<R>,
stream: Stream.Stream<A, E, R>,
initialValue: () => Effect.Effect<A, IE, R>,
): Option.Some<A>
useSubscribeStream<A, E, IE, R>(
this: ReffuseNamespace<R>,
stream: Stream.Stream<A, E, R>,
initialValue?: () => Effect.Effect<A, IE, R>,
): Option.Option<A> {
const [reactStateValue, setReactStateValue] = React.useState(this.useMemo(
() => initialValue
? Effect.map(initialValue(), Option.some)
: Effect.succeed(Option.none()),
[],
{ doNotReExecuteOnRuntimeOrContextChange: true },
))
this.useFork(() => Stream.runForEach( this.useFork(() => Stream.runForEach(
Stream.changesWith(stream, (x, y) => x === y), Stream.changesWith(stream, (x, y) => x === y),
v => Effect.sync(() => setReactStateValue(Option.some(v))), v => Effect.sync(() => setReactStateValue(Option.some(v))),
), [stream]) ), [stream])
return reactStateValue as InitialA extends A ? Option.Some<A> : Option.Option<A> return reactStateValue
} }
SubRef<B, const P extends PropertyPath.Paths<B>, R>( SubRefFromGetSet<A, B, R>(
this: ReffuseNamespace<R>,
props: {
readonly parent: SubscriptionRef.SubscriptionRef<B>,
readonly getter: (parentValue: B) => A,
readonly setter: (parentValue: B, value: A) => B,
readonly children: (subRef: SubscriptionSubRef.SubscriptionSubRef<A, B>) => React.ReactNode
},
): React.ReactNode {
return props.children(this.useSubRefFromGetSet(props.parent, props.getter, props.setter))
}
SubRefFromPath<B, const P extends PropertyPath.Paths<B>, R>(
this: ReffuseNamespace<R>, this: ReffuseNamespace<R>,
props: { props: {
readonly parent: SubscriptionRef.SubscriptionRef<B>, readonly parent: SubscriptionRef.SubscriptionRef<B>,
@@ -492,7 +563,7 @@ export abstract class ReffuseNamespace<R> {
readonly children: (subRef: SubscriptionSubRef.SubscriptionSubRef<PropertyPath.ValueFromPath<B, P>, B>) => React.ReactNode readonly children: (subRef: SubscriptionSubRef.SubscriptionSubRef<PropertyPath.ValueFromPath<B, P>, B>) => React.ReactNode
}, },
): React.ReactNode { ): React.ReactNode {
return props.children(this.useSubRef(props.parent, props.path)) return props.children(this.useSubRefFromPath(props.parent, props.path))
} }
SubscribeRefs< SubscribeRefs<
@@ -518,15 +589,30 @@ export abstract class ReffuseNamespace<R> {
return props.children(this.useRefState(props.ref)) return props.children(this.useRefState(props.ref))
} }
SubscribeStream<A, InitialA extends A | undefined, E, R>( SubscribeStream<A, E, R>(
this: ReffuseNamespace<R>, this: ReffuseNamespace<R>,
props: { props: {
readonly stream: Stream.Stream<A, E, R> readonly stream: Stream.Stream<A, E, R>
readonly initialValue?: InitialA readonly children: (latestValue: Option.Option<A>) => React.ReactNode
readonly children: (latestValue: InitialA extends A ? Option.Some<A> : Option.Option<A>) => React.ReactNode },
): React.ReactNode
SubscribeStream<A, E, IE, R>(
this: ReffuseNamespace<R>,
props: {
readonly stream: Stream.Stream<A, E, R>
readonly initialValue: () => Effect.Effect<A, IE, R>
readonly children: (latestValue: Option.Some<A>) => React.ReactNode
},
): React.ReactNode
SubscribeStream<A, E, IE, R>(
this: ReffuseNamespace<R>,
props: {
readonly stream: Stream.Stream<A, E, R>
readonly initialValue?: () => Effect.Effect<A, IE, R>
readonly children: (latestValue: Option.Some<A>) => React.ReactNode
}, },
): React.ReactNode { ): React.ReactNode {
return props.children(this.useSubscribeStream(props.stream, props.initialValue)) return props.children(this.useSubscribeStream(props.stream, props.initialValue as () => Effect.Effect<A, IE, R>))
} }
} }

View File

@@ -52,7 +52,7 @@ class SubscriptionSubRefImpl<in out A, in out B> extends Effectable.Class<A> imp
readonly setter: (parentValue: B, value: A) => B, readonly setter: (parentValue: B, value: A) => B,
) { ) {
super() super()
this.get = Ref.get(this.parent).pipe(Effect.map(this.getter)) this.get = Effect.map(Ref.get(this.parent), this.getter)
} }
commit() { commit() {