@tilia/react 1.6.0 → 2.0.0-beta.20250713T165812
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -165
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +9 -7
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +3 -3
- package/package.json +12 -6
- package/rescript.json +2 -2
- package/src/TiliaReact.res +27 -0
- package/src/TiliaReact.resi +13 -0
- package/src/Tilia.res +0 -20
- package/src/Tilia.resi +0 -8
package/README.md
CHANGED
|
@@ -1,189 +1,69 @@
|
|
|
1
|
-
#
|
|
1
|
+
# React hooks for tilia
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Check the [**website**](https://tiliajs.com) for documentation and examples.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
_ReScript_, you can find the examples for the latter at the end.
|
|
5
|
+
## API (in case the website is not available)
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
(TypeScript version below)
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
npm install @tilia/react
|
|
12
|
-
```
|
|
13
|
-
|
|
14
|
-
## Usage
|
|
15
|
-
|
|
16
|
-
```tsx
|
|
17
|
-
import { tilia, observe, useTilia } from "@tilia/react";
|
|
18
|
-
|
|
19
|
-
// Create a tracked object or array:
|
|
20
|
-
const state: MyState = tilia({
|
|
21
|
-
flowers: "are beautiful",
|
|
22
|
-
clouds: { morning: "can be pink", evening: "can be orange" },
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
// Observe 'evening' and update 'morning' with a calculation.
|
|
26
|
-
observe(state, () => {
|
|
27
|
-
state.clouds.morning = state.clouds.evening + ", maybe";
|
|
28
|
-
});
|
|
9
|
+
### ReScript
|
|
29
10
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
// observers.
|
|
36
|
-
clouds.evening = e.target.value;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// Will register this component as watching the "evening"
|
|
40
|
-
// property inside clouds.
|
|
41
|
-
return (
|
|
42
|
-
<div>
|
|
43
|
-
<input value={clouds.evening} onChange={onChange} />
|
|
44
|
-
</div>
|
|
45
|
-
);
|
|
11
|
+
```res
|
|
12
|
+
open Tilia
|
|
13
|
+
type tilia_react = {
|
|
14
|
+
useTilia: unit => unit,
|
|
15
|
+
useComputed: 'a. (unit => 'a) => signal<'a>,
|
|
46
16
|
}
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
Note that you can re-insert a tracked object inside the same tree and share
|
|
50
|
-
state and tracking.
|
|
51
17
|
|
|
52
|
-
|
|
18
|
+
let useTilia: unit => unit
|
|
19
|
+
let useComputed: (unit => 'a) => signal<'a>
|
|
53
20
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
```ts
|
|
59
|
-
const groceries = tilia({
|
|
60
|
-
fruits: {
|
|
61
|
-
bananas: 5,
|
|
62
|
-
apples: 10,
|
|
63
|
-
},
|
|
64
|
-
veggies: {
|
|
65
|
-
salad: 2,
|
|
66
|
-
},
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
observe(groceries, () => {
|
|
70
|
-
console.log("⚠️⚠️", "Banana count is now", groceries.fruits.bananas);
|
|
71
|
-
});
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
What happens if someone does this ?
|
|
75
|
-
|
|
76
|
-
```ts
|
|
77
|
-
// A
|
|
78
|
-
groceries.fruits.apples = 12;
|
|
21
|
+
/**
|
|
22
|
+
* Create api from a tilia context.
|
|
23
|
+
*/
|
|
24
|
+
let make: tilia => tilia_react
|
|
79
25
|
```
|
|
80
26
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
What about this ?
|
|
27
|
+
### TypeScript
|
|
84
28
|
|
|
85
29
|
```ts
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
```
|
|
30
|
+
import type { Tilia } from "tilia";
|
|
31
|
+
export interface TiliaReact {
|
|
32
|
+
useTilia: () => void;
|
|
33
|
+
useComputed: <T>(fn: () => T) => signal<A>;
|
|
34
|
+
}
|
|
92
35
|
|
|
93
|
-
|
|
36
|
+
export function useTilia(): void;
|
|
37
|
+
export function useComputed<T>(fn: () => T): signal<T>;
|
|
94
38
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Create api from a tilia context.
|
|
41
|
+
*/
|
|
42
|
+
export function make(tilia: Tilia): TiliaReact;
|
|
98
43
|
```
|
|
99
44
|
|
|
100
|
-
|
|
101
|
-
only the value for the "apples" key is changed.
|
|
45
|
+
## Example
|
|
102
46
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
## "Index observer"
|
|
108
|
-
|
|
109
|
-
What about this observer ?
|
|
47
|
+
```tsx
|
|
48
|
+
import { tilia } from "tilia";
|
|
49
|
+
import { useTilia, useComputed } from "@tilia/react";
|
|
110
50
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
console.log("I like these fruits:", Object.keys(groceries.fruits).join(", "));
|
|
51
|
+
const alice = tilia({
|
|
52
|
+
name: "Alice",
|
|
53
|
+
age: 10,
|
|
115
54
|
});
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
When is this triggered ?
|
|
119
|
-
|
|
120
|
-
```ts
|
|
121
|
-
// A
|
|
122
|
-
groceries.fruits.bananas = 15;
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
Or this ?
|
|
126
|
-
|
|
127
|
-
```ts
|
|
128
|
-
// B
|
|
129
|
-
groceries.fruits.oranges = 2;
|
|
130
|
-
```
|
|
131
55
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
56
|
+
// If alice.name or alice.age changes, this will re-rendered.
|
|
57
|
+
function ShowPerson({ person }: { person: Person }) {
|
|
58
|
+
useTilia();
|
|
135
59
|
|
|
136
|
-
|
|
60
|
+
const selected = useComputed(() => app.selected.id === person.id);
|
|
137
61
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
mutable morning: string,
|
|
145
|
-
mutable evening: string,
|
|
146
|
-
}
|
|
147
|
-
type state = {
|
|
148
|
-
mutable flowers: string,
|
|
149
|
-
mutable clouds: clouds,
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// Create a tracked object or array:
|
|
153
|
-
let tree = Tilia.make({
|
|
154
|
-
flowers: "are beautiful",
|
|
155
|
-
clouds: { morning: "can be pink", evening: "can be orange" },
|
|
156
|
-
})
|
|
157
|
-
|
|
158
|
-
// Observe 'evening' and update 'morning' with a calculation.
|
|
159
|
-
Tilia.observe(state, () => {
|
|
160
|
-
state.clouds.morning = state.clouds.evening ++ ", maybe"
|
|
161
|
-
})
|
|
162
|
-
|
|
163
|
-
@react.component
|
|
164
|
-
let make(~clouds: clouds) {
|
|
165
|
-
// Start observing the clouds props (must be a tilia object).
|
|
166
|
-
let clouds = Tilia.use(clouds)
|
|
167
|
-
let onChange = (e) => {
|
|
168
|
-
// Write to a tilia object and trigger re-draw in
|
|
169
|
-
// observers.
|
|
170
|
-
clouds.evening = target(e)["value"]
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// Will register this component as watching the "evening"
|
|
174
|
-
// property inside clouds.
|
|
175
|
-
<div>
|
|
176
|
-
<input value={clouds.evening} onChange />
|
|
177
|
-
</div>
|
|
62
|
+
return (
|
|
63
|
+
<div className={selected.value ? "selected" : ""}>
|
|
64
|
+
<p>Name: {person.name}</p>
|
|
65
|
+
<p>Age: {person.age}</p>
|
|
66
|
+
</div>
|
|
67
|
+
);
|
|
178
68
|
}
|
|
179
69
|
```
|
|
180
|
-
|
|
181
|
-
Please check the documentation for [@tilia/core](../core/README.md) for technical details on how tracking is done.
|
|
182
|
-
|
|
183
|
-
## Little sandbox examples
|
|
184
|
-
|
|
185
|
-
- [An input field](https://codesandbox.io/p/sandbox/react-ts)
|
|
186
|
-
|
|
187
|
-
### Changelog
|
|
188
|
-
|
|
189
|
-
See [core/changelog](https://github.com/tiliajs/tilia)
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var c=Object.defineProperty;var d=Object.getOwnPropertyDescriptor;var l=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var C=(t,e)=>{for(var r in e)c(t,r,{get:e[r],enumerable:!0})},_=(t,e,r,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of l(e))!x.call(t,o)&&o!==r&&c(t,o,{get:()=>e[o],enumerable:!(s=d(e,o))||s.enumerable});return t};var T=t=>_(c({},"__esModule",{value:!0}),t);var y={};C(y,{make:()=>m,useComputed:()=>k,useTilia:()=>b});module.exports=T(y);var u=require("tilia"),n=require("react");function m({_observe:t,signal:e}){function r(){let[o,a]=(0,n.useState)(0),i=t(()=>a(f=>f+1));(0,n.useEffect)(()=>((0,u._ready)(i,!0),()=>(0,u._clear)(i)))}function s(o){return(0,n.useMemo)(()=>e((0,u.computed)(o)),[])}return{useTilia:r,useComputed:s}}var p=m(u._ctx),b=p.useTilia,k=p.useComputed;
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.js"],
|
|
4
|
-
"sourcesContent": ["import {
|
|
5
|
-
"mappings": "4ZAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,
|
|
6
|
-
"names": ["index_exports", "__export", "useTilia", "__toCommonJS", "import_react", "
|
|
4
|
+
"sourcesContent": ["import { _ctx, _ready, _clear, computed } from \"tilia\";\nimport { useMemo, useState, useEffect } from \"react\";\n\nexport function make({ _observe, signal }) {\n function useTilia() {\n const [_, setCount] = useState(0);\n const o = _observe(() => setCount((i) => i + 1));\n useEffect(() => {\n _ready(o, true);\n return () => _clear(o);\n });\n }\n function useComputed(fn) {\n return useMemo(() => signal(computed(fn)), []);\n }\n return { useTilia, useComputed };\n}\n\nconst lib = make(_ctx);\nexport const useTilia = lib.useTilia;\nexport const useComputed = lib.useComputed;\n"],
|
|
5
|
+
"mappings": "4ZAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,UAAAE,EAAA,gBAAAC,EAAA,aAAAC,IAAA,eAAAC,EAAAL,GAAA,IAAAM,EAA+C,iBAC/CC,EAA6C,iBAEtC,SAASL,EAAK,CAAE,SAAAM,EAAU,OAAAC,CAAO,EAAG,CACzC,SAASL,GAAW,CAClB,GAAM,CAACM,EAAGC,CAAQ,KAAI,YAAS,CAAC,EAC1BC,EAAIJ,EAAS,IAAMG,EAAUE,GAAMA,EAAI,CAAC,CAAC,KAC/C,aAAU,QACR,UAAOD,EAAG,EAAI,EACP,OAAM,UAAOA,CAAC,EACtB,CACH,CACA,SAAST,EAAYW,EAAI,CACvB,SAAO,WAAQ,IAAML,KAAO,YAASK,CAAE,CAAC,EAAG,CAAC,CAAC,CAC/C,CACA,MAAO,CAAE,SAAAV,EAAU,YAAAD,CAAY,CACjC,CAEA,IAAMY,EAAMb,EAAK,MAAI,EACRE,EAAWW,EAAI,SACfZ,EAAcY,EAAI",
|
|
6
|
+
"names": ["index_exports", "__export", "make", "useComputed", "useTilia", "__toCommonJS", "import_tilia", "import_react", "_observe", "signal", "_", "setCount", "o", "i", "fn", "lib"]
|
|
7
7
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import type { Tilia } from "tilia";
|
|
2
|
+
export interface TiliaReact {
|
|
3
|
+
useTilia: () => void;
|
|
4
|
+
useComputed: <T>(fn: () => T) => signal<A>;
|
|
5
|
+
}
|
|
1
6
|
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export function
|
|
5
|
-
export function
|
|
6
|
-
export function track<a>(tree: a, fn: (tree: a) => void): observer;
|
|
7
|
-
export function clear(observer: observer): void;
|
|
8
|
-
export const useTilia: <a>(tree: a) => a;
|
|
7
|
+
export function make(tilia: Tilia): TiliaReact;
|
|
8
|
+
|
|
9
|
+
export function useTilia(): void;
|
|
10
|
+
export function useComputed<T>(fn: () => T): signal<T>;
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{_ctx as m,_ready as p,_clear as a,computed as f}from"tilia";import{useMemo as d,useState as l,useEffect as x}from"react";function C({_observe:u,signal:r}){function n(){let[e,c]=l(0),t=u(()=>c(i=>i+1));x(()=>(p(t,!0),()=>a(t)))}function s(e){return d(()=>r(f(e)),[])}return{useTilia:n,useComputed:s}}var o=C(m),b=o.useTilia,k=o.useComputed;export{C as make,k as useComputed,b as useTilia};
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.js"],
|
|
4
|
-
"sourcesContent": ["import {
|
|
5
|
-
"mappings": "AAAA,OAAS,
|
|
6
|
-
"names": ["
|
|
4
|
+
"sourcesContent": ["import { _ctx, _ready, _clear, computed } from \"tilia\";\nimport { useMemo, useState, useEffect } from \"react\";\n\nexport function make({ _observe, signal }) {\n function useTilia() {\n const [_, setCount] = useState(0);\n const o = _observe(() => setCount((i) => i + 1));\n useEffect(() => {\n _ready(o, true);\n return () => _clear(o);\n });\n }\n function useComputed(fn) {\n return useMemo(() => signal(computed(fn)), []);\n }\n return { useTilia, useComputed };\n}\n\nconst lib = make(_ctx);\nexport const useTilia = lib.useTilia;\nexport const useComputed = lib.useComputed;\n"],
|
|
5
|
+
"mappings": "AAAA,OAAS,QAAAA,EAAM,UAAAC,EAAQ,UAAAC,EAAQ,YAAAC,MAAgB,QAC/C,OAAS,WAAAC,EAAS,YAAAC,EAAU,aAAAC,MAAiB,QAEtC,SAASC,EAAK,CAAE,SAAAC,EAAU,OAAAC,CAAO,EAAG,CACzC,SAASC,GAAW,CAClB,GAAM,CAACC,EAAGC,CAAQ,EAAIP,EAAS,CAAC,EAC1BQ,EAAIL,EAAS,IAAMI,EAAU,GAAM,EAAI,CAAC,CAAC,EAC/CN,EAAU,KACRL,EAAOY,EAAG,EAAI,EACP,IAAMX,EAAOW,CAAC,EACtB,CACH,CACA,SAASC,EAAYC,EAAI,CACvB,OAAOX,EAAQ,IAAMK,EAAON,EAASY,CAAE,CAAC,EAAG,CAAC,CAAC,CAC/C,CACA,MAAO,CAAE,SAAAL,EAAU,YAAAI,CAAY,CACjC,CAEA,IAAME,EAAMT,EAAKP,CAAI,EACRU,EAAWM,EAAI,SACfF,EAAcE,EAAI",
|
|
6
|
+
"names": ["_ctx", "_ready", "_clear", "computed", "useMemo", "useState", "useEffect", "make", "_observe", "signal", "useTilia", "_", "setCount", "o", "useComputed", "fn", "lib"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tilia/react",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-beta.20250713T165812",
|
|
4
|
+
"homepage": "https://tiliajs.com",
|
|
4
5
|
"main": "./dist/index.cjs",
|
|
5
6
|
"module": "./dist/index.mjs",
|
|
6
7
|
"types": "./dist/index.d.ts",
|
|
@@ -11,12 +12,17 @@
|
|
|
11
12
|
},
|
|
12
13
|
"description": "🍃 React State Management with Tilia.",
|
|
13
14
|
"keywords": [
|
|
15
|
+
"state management",
|
|
16
|
+
"domain-driven",
|
|
17
|
+
"reactive",
|
|
18
|
+
"typescript",
|
|
14
19
|
"rescript",
|
|
20
|
+
"frp",
|
|
15
21
|
"react",
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
22
|
+
"hooks",
|
|
23
|
+
"computed",
|
|
24
|
+
"minimal",
|
|
25
|
+
"simple"
|
|
20
26
|
],
|
|
21
27
|
"author": "Gaspard Anna Bucher <g.a.bucher@midasum.com>",
|
|
22
28
|
"license": "MIT",
|
|
@@ -24,6 +30,6 @@
|
|
|
24
30
|
"react": "*"
|
|
25
31
|
},
|
|
26
32
|
"dependencies": {
|
|
27
|
-
"
|
|
33
|
+
"tilia": "2.0.0-beta.20250713T165812"
|
|
28
34
|
}
|
|
29
35
|
}
|
package/rescript.json
CHANGED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
@module("react") external useState: int => (int, (int => int) => unit) = "useState"
|
|
2
|
+
@module("react") external useEffect: (unit => option<unit => unit>) => unit = "useEffect"
|
|
3
|
+
@module("react") external useMemo: (unit => 'a, 'b) => 'a = "useMemo"
|
|
4
|
+
open Tilia
|
|
5
|
+
|
|
6
|
+
type tilia_react = {
|
|
7
|
+
useTilia: unit => unit,
|
|
8
|
+
useComputed: 'a. (unit => 'a) => Tilia.signal<'a>,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
let make = ctx => {
|
|
12
|
+
let {Tilia._observe: _observe, signal} = ctx
|
|
13
|
+
let useTilia = () => {
|
|
14
|
+
let (_, setCount) = useState(0)
|
|
15
|
+
let o = _observe(() => setCount(i => i + 1))
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
_ready(o, true)
|
|
18
|
+
Some(() => _clear(o))
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
let useComputed = fn => useMemo(() => signal(computed(fn)), [])
|
|
22
|
+
{useTilia, useComputed}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let tr = make(_ctx)
|
|
26
|
+
let useTilia = tr.useTilia
|
|
27
|
+
let useComputed = tr.useComputed
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
open Tilia
|
|
2
|
+
type tilia_react = {
|
|
3
|
+
useTilia: unit => unit,
|
|
4
|
+
useComputed: 'a. (unit => 'a) => signal<'a>,
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
let useTilia: unit => unit
|
|
8
|
+
let useComputed: (unit => 'a) => signal<'a>
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Create api from a tilia context.
|
|
12
|
+
*/
|
|
13
|
+
let make: tilia => tilia_react
|
package/src/Tilia.res
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
@module("react") external useState: int => (int, (int => int) => unit) = "useState"
|
|
2
|
-
@module("react") external useEffect: (unit => option<unit => unit>) => unit = "useEffect"
|
|
3
|
-
open TiliaCore
|
|
4
|
-
|
|
5
|
-
type observer = observer
|
|
6
|
-
let make = make
|
|
7
|
-
let observe = observe
|
|
8
|
-
let track = track
|
|
9
|
-
let clear = clear
|
|
10
|
-
let compute = compute
|
|
11
|
-
|
|
12
|
-
let use = p => {
|
|
13
|
-
let (_, setCount) = useState(0)
|
|
14
|
-
let o = _connect(p, () => setCount(i => i + 1))
|
|
15
|
-
useEffect(() => {
|
|
16
|
-
_ready(o)
|
|
17
|
-
Some(() => clear(o))
|
|
18
|
-
})
|
|
19
|
-
p
|
|
20
|
-
}
|
package/src/Tilia.resi
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
type observer = TiliaCore.observer
|
|
2
|
-
let make: ('a, ~flush: (unit => unit) => unit=?) => 'a
|
|
3
|
-
let observe: ('a, 'a => unit) => unit
|
|
4
|
-
let compute: ('a, string, 'a => unit) => observer
|
|
5
|
-
let track: ('a, 'a => unit) => observer
|
|
6
|
-
let clear: observer => unit
|
|
7
|
-
|
|
8
|
-
let use: 'a => 'a
|