@tilia/react 1.6.0 → 2.0.0-beta.20250630T222639

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 CHANGED
@@ -1,189 +1,69 @@
1
- # Tilia React
1
+ # React hooks for tilia
2
2
 
3
- Tilia is a simple state management library.
3
+ Check the [**website**](https://tiliajs.com) for documentation and examples.
4
4
 
5
- This package contains the hook for React. It is compatible with _TypeScript_ and
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
- ## Installation
7
+ (TypeScript version below)
9
8
 
10
- ```sh
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
- function Clouds(props: { clouds: MyState["clouds"] }) {
31
- // Start observing the clouds props (must be a tilia object).
32
- const clouds = useTilia(props.clouds);
33
- function onChange(e) {
34
- // Write to a tilia object and trigger re-draw in
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
- ## How tracking works
18
+ let useTilia: unit => unit
19
+ let useComputed: (unit => 'a) => signal<'a>
53
20
 
54
- An observer registers itself whenever it **READS** a key inside a tracked object or array.
55
-
56
- For example:
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
- Does the banana observer get triggered ?
82
-
83
- What about this ?
27
+ ### TypeScript
84
28
 
85
29
  ```ts
86
- // B
87
- groceries.fruits = {
88
- bananas: 5,
89
- apples: 10,
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
- Or this ?
36
+ export function useTilia(): void;
37
+ export function useComputed<T>(fn: () => T): signal<T>;
94
38
 
95
- ```ts
96
- // C
97
- Object.assign(groceries.fruits, { bananas: 8 });
39
+ /**
40
+ * Create api from a tilia context.
41
+ */
42
+ export function make(tilia: Tilia): TiliaReact;
98
43
  ```
99
44
 
100
- In **A**, nothing happens because the "bananas observer" only read keys "fruits" and "bananas", and here
101
- only the value for the "apples" key is changed.
45
+ ## Example
102
46
 
103
- In **B**, the "bananas observer" sees that there is a write to the "fruits" key that it watches and gets triggered, even though the content of the object is the same because tilia only checks for direct equality (`===`) to avoid triggering on same value write.
104
-
105
- In **C**, the "bananas" key gets written and triggers the observer.
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
- ```ts
112
- // D
113
- observe(groceries, () => {
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
- The "index observer" watches changes to the keys (adding or removing keys), but not to the
133
- values that the keys contain. This means that the observer, is not triggered with **A** but
134
- is triggered with **B**.
56
+ // If alice.name or alice.age changes, this will re-rendered.
57
+ function ShowPerson({ person }: { person: Person }) {
58
+ useTilia();
135
59
 
136
- That's it !
60
+ const selected = useComputed(() => app.selected.id === person.id);
137
61
 
138
- # ReScript example
139
-
140
- ```res
141
- open JsxEvent.Form
142
-
143
- type clouds = {
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 a=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var s=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var l=(e,t)=>{for(var r in t)a(e,r,{get:t[r],enumerable:!0})},p=(e,t,r,u)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of s(t))!m.call(e,n)&&n!==r&&a(e,n,{get:()=>t[n],enumerable:!(u=f(t,n))||u.enumerable});return e};var x=e=>p(a({},"__esModule",{value:!0}),e);var b={};l(b,{clear:()=>o.clear,compute:()=>o.compute,observe:()=>o.observe,tilia:()=>o.tilia,track:()=>o.track,useTilia:()=>_});module.exports=x(b);var i=require("react"),c=require("@tilia/core"),o=require("@tilia/core");function _(e){let[,t]=(0,i.useState)(0),r=(0,c._connect)(e,()=>t(u=>u+1));return(0,i.useEffect)(function(){return(0,c._ready)(r),()=>(0,c.clear)(r)}),e}
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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/index.js"],
4
- "sourcesContent": ["import { useState, useEffect } from \"react\";\nimport { _connect, _ready, clear } from \"@tilia/core\";\nexport { tilia, observe, track, clear, compute } from \"@tilia/core\";\n\nexport function useTilia(p) {\n const [, setCount] = useState(0);\n const o = _connect(p, () => setCount((i) => i + 1));\n useEffect(function () {\n _ready(o);\n return () => clear(o);\n });\n return p;\n}\n"],
5
- "mappings": "4ZAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,gHAAAE,IAAA,eAAAC,EAAAH,GAAA,IAAAI,EAAoC,iBACpCC,EAAwC,uBACxCA,EAAsD,uBAE/C,SAASH,EAASI,EAAG,CAC1B,GAAM,CAAC,CAAEC,CAAQ,KAAI,YAAS,CAAC,EACzBC,KAAI,YAASF,EAAG,IAAMC,EAAUE,GAAMA,EAAI,CAAC,CAAC,EAClD,sBAAU,UAAY,CACpB,mBAAOD,CAAC,EACD,OAAM,SAAMA,CAAC,CACtB,CAAC,EACMF,CACT",
6
- "names": ["index_exports", "__export", "useTilia", "__toCommonJS", "import_react", "import_core", "p", "setCount", "o", "i"]
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
- import type { observer } from "@tilia/core";
3
- export function tilia<a extends object>(tree: a, flush?: (fn: () => void) => void): a;
4
- export function observe<a>(tree: a, fn: (tree: a) => void): void;
5
- export function compute<a extends object>(tree: a, key: keyof a, fn: (tree: a) => void): observer;
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{useState as c,useEffect as n}from"react";import{_connect as u,_ready as i,clear as a}from"@tilia/core";import{tilia as x,observe as _,track as b,clear as d,compute as k}from"@tilia/core";function m(t){let[,o]=c(0),e=u(t,()=>o(r=>r+1));return n(function(){return i(e),()=>a(e)}),t}export{d as clear,k as compute,_ as observe,x as tilia,b as track,m as useTilia};
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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/index.js"],
4
- "sourcesContent": ["import { useState, useEffect } from \"react\";\nimport { _connect, _ready, clear } from \"@tilia/core\";\nexport { tilia, observe, track, clear, compute } from \"@tilia/core\";\n\nexport function useTilia(p) {\n const [, setCount] = useState(0);\n const o = _connect(p, () => setCount((i) => i + 1));\n useEffect(function () {\n _ready(o);\n return () => clear(o);\n });\n return p;\n}\n"],
5
- "mappings": "AAAA,OAAS,YAAAA,EAAU,aAAAC,MAAiB,QACpC,OAAS,YAAAC,EAAU,UAAAC,EAAQ,SAAAC,MAAa,cACxC,OAAS,SAAAC,EAAO,WAAAC,EAAS,SAAAC,EAAO,SAAAH,EAAO,WAAAI,MAAe,cAE/C,SAASC,EAASC,EAAG,CAC1B,GAAM,CAAC,CAAEC,CAAQ,EAAIX,EAAS,CAAC,EACzBY,EAAIV,EAASQ,EAAG,IAAMC,EAAUE,GAAMA,EAAI,CAAC,CAAC,EAClD,OAAAZ,EAAU,UAAY,CACpB,OAAAE,EAAOS,CAAC,EACD,IAAMR,EAAMQ,CAAC,CACtB,CAAC,EACMF,CACT",
6
- "names": ["useState", "useEffect", "_connect", "_ready", "clear", "tilia", "observe", "track", "compute", "useTilia", "p", "setCount", "o", "i"]
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": "1.6.0",
3
+ "version": "2.0.0-beta.20250630T222639",
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
+ "reactive",
17
+ "typescript",
14
18
  "rescript",
19
+ "frp",
15
20
  "react",
16
- "state",
17
- "state management",
18
- "tilia",
19
- "frp"
21
+ "hooks",
22
+ "computed",
23
+ "store",
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
- "@tilia/core": "1.6.0"
33
+ "tilia": "2.0.0-beta.20250630T222639"
28
34
  }
29
35
  }
package/rescript.json CHANGED
@@ -4,7 +4,7 @@
4
4
  {
5
5
  "dir": "src",
6
6
  "public": [
7
- "Tilia"
7
+ "TiliaReact"
8
8
  ],
9
9
  "subdirs": true
10
10
  },
@@ -19,7 +19,7 @@
19
19
  },
20
20
  "suffix": ".mjs",
21
21
  "bs-dependencies": [
22
- "@tilia/core"
22
+ "tilia"
23
23
  ],
24
24
  "bs-dev-dependencies": [
25
25
  "@rescript/core",
@@ -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