@tilia/react 1.1.1 → 1.1.2-canary.20241230T160951
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 +91 -14
- package/dist/index.cjs.js +37 -1
- package/dist/index.cjs.js.map +2 -2
- package/dist/index.esm.js +17 -1
- package/dist/index.esm.js.map +2 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
# Tilia React
|
|
2
2
|
|
|
3
|
-
Tilia is a simple state management library
|
|
4
|
-
React.
|
|
3
|
+
Tilia is a simple state management library.
|
|
5
4
|
|
|
6
|
-
This package contains the hook for React.
|
|
7
|
-
|
|
8
|
-
This package is compatible with _TypeScript_ and _ReScript_, you can find the
|
|
9
|
-
examples for the latter at the end.
|
|
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.
|
|
10
7
|
|
|
11
8
|
## Installation
|
|
12
9
|
|
|
@@ -52,6 +49,92 @@ function Clouds(props: { clouds: MyState["clouds"] }) {
|
|
|
52
49
|
Note that you can re-insert a tracked object inside the same tree and share
|
|
53
50
|
state and tracking.
|
|
54
51
|
|
|
52
|
+
## How tracking works
|
|
53
|
+
|
|
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;
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Does the banana observer get triggered ?
|
|
82
|
+
|
|
83
|
+
What about this ?
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
// B
|
|
87
|
+
groceries.fruits = {
|
|
88
|
+
bananas: 5,
|
|
89
|
+
apples: 10,
|
|
90
|
+
};
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Or this ?
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
// C
|
|
97
|
+
Object.assign(groceries.fruits, { bananas: 8 });
|
|
98
|
+
```
|
|
99
|
+
|
|
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.
|
|
102
|
+
|
|
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 ?
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
// D
|
|
113
|
+
observe(groceries, () => {
|
|
114
|
+
console.log("I like these fruits:", Object.keys(groceries.fruits).join(", "));
|
|
115
|
+
});
|
|
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
|
+
|
|
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**.
|
|
135
|
+
|
|
136
|
+
That's it !
|
|
137
|
+
|
|
55
138
|
# ReScript example
|
|
56
139
|
|
|
57
140
|
```res
|
|
@@ -101,12 +184,6 @@ Please check the documentation for [@tilia/core](../core/README.md) for technica
|
|
|
101
184
|
|
|
102
185
|
- [An input field](https://codesandbox.io/p/sandbox/react-ts)
|
|
103
186
|
|
|
104
|
-
### Changelog
|
|
187
|
+
### Changelog
|
|
105
188
|
|
|
106
|
-
|
|
107
|
-
- 2024-12-18 **1.2.2** Fix readonly tracking: should not proxy.
|
|
108
|
-
- 2024-12-18 **1.2.1** Fix bug to not track prototype methods.
|
|
109
|
-
- 2024-12-18 **1.2.0** Improve ownKeys watching, notify on key deletion.
|
|
110
|
-
- 2024-12-18 **1.1.1** Fix build issue (rescript was still required)
|
|
111
|
-
- 2024-12-17 **1.1.0** Add support to share tracking between branches.
|
|
112
|
-
- 2024-12-13 **1.0.0** Alpha release.
|
|
189
|
+
See [core/changelog](https://github.com/tiliajs/tilia)
|
package/dist/index.cjs.js
CHANGED
|
@@ -1,2 +1,38 @@
|
|
|
1
|
-
var
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/index.js
|
|
20
|
+
var index_exports = {};
|
|
21
|
+
__export(index_exports, {
|
|
22
|
+
observe: () => import_core.observe,
|
|
23
|
+
tilia: () => import_core.tilia,
|
|
24
|
+
useTilia: () => useTilia
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
var import_react = require("react");
|
|
28
|
+
var import_core = require("@tilia/core");
|
|
29
|
+
function useTilia(p) {
|
|
30
|
+
const [, setCount] = (0, import_react.useState)(0);
|
|
31
|
+
const o = (0, import_core._connect)(p, () => setCount((i) => i + 1));
|
|
32
|
+
(0, import_react.useEffect)(function() {
|
|
33
|
+
(0, import_core._ready)(o);
|
|
34
|
+
return () => (0, import_core._clear)(o);
|
|
35
|
+
});
|
|
36
|
+
return p;
|
|
37
|
+
}
|
|
2
38
|
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.cjs.js.map
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.js"],
|
|
4
4
|
"sourcesContent": ["import { useState, useEffect } from \"react\";\nimport { _connect, _ready, _clear, tilia, observe } 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\nexport { tilia, observe };\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": [
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAoC;AACpC,kBAAyD;AAElD,SAAS,SAAS,GAAG;AAC1B,QAAM,CAAC,EAAE,QAAQ,QAAI,uBAAS,CAAC;AAC/B,QAAM,QAAI,sBAAS,GAAG,MAAM,SAAS,CAAC,MAAM,IAAI,CAAC,CAAC;AAClD,8BAAU,WAAY;AACpB,4BAAO,CAAC;AACR,WAAO,UAAM,oBAAO,CAAC;AAAA,EACvB,CAAC;AACD,SAAO;AACT;",
|
|
6
|
+
"names": []
|
|
7
7
|
}
|
package/dist/index.esm.js
CHANGED
|
@@ -1,2 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
// src/index.js
|
|
2
|
+
import { useState, useEffect } from "react";
|
|
3
|
+
import { _connect, _ready, _clear, tilia, observe } from "@tilia/core";
|
|
4
|
+
function useTilia(p) {
|
|
5
|
+
const [, setCount] = useState(0);
|
|
6
|
+
const o = _connect(p, () => setCount((i) => i + 1));
|
|
7
|
+
useEffect(function() {
|
|
8
|
+
_ready(o);
|
|
9
|
+
return () => _clear(o);
|
|
10
|
+
});
|
|
11
|
+
return p;
|
|
12
|
+
}
|
|
13
|
+
export {
|
|
14
|
+
observe,
|
|
15
|
+
tilia,
|
|
16
|
+
useTilia
|
|
17
|
+
};
|
|
2
18
|
//# sourceMappingURL=index.esm.js.map
|
package/dist/index.esm.js.map
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.js"],
|
|
4
4
|
"sourcesContent": ["import { useState, useEffect } from \"react\";\nimport { _connect, _ready, _clear, tilia, observe } 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\nexport { tilia, observe };\n"],
|
|
5
|
-
"mappings": "AAAA,
|
|
6
|
-
"names": [
|
|
5
|
+
"mappings": ";AAAA,SAAS,UAAU,iBAAiB;AACpC,SAAS,UAAU,QAAQ,QAAQ,OAAO,eAAe;AAElD,SAAS,SAAS,GAAG;AAC1B,QAAM,CAAC,EAAE,QAAQ,IAAI,SAAS,CAAC;AAC/B,QAAM,IAAI,SAAS,GAAG,MAAM,SAAS,CAAC,MAAM,IAAI,CAAC,CAAC;AAClD,YAAU,WAAY;AACpB,WAAO,CAAC;AACR,WAAO,MAAM,OAAO,CAAC;AAAA,EACvB,CAAC;AACD,SAAO;AACT;",
|
|
6
|
+
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tilia/react",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2-canary.20241230T160951",
|
|
4
4
|
"main": "./dist/index.cjs.js",
|
|
5
5
|
"module": "./dist/index.esm.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -24,6 +24,6 @@
|
|
|
24
24
|
"react": "*"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@tilia/core": "
|
|
27
|
+
"@tilia/core": "1.3.0-canary.20241230T160951"
|
|
28
28
|
}
|
|
29
29
|
}
|