tilia 5.2.0 → 6.0.0
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 +261 -57
- package/dist/index.cjs +89 -153
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +176 -63
- package/dist/index.mjs +89 -153
- package/dist/index.mjs.map +3 -3
- package/llms.txt +127 -59
- package/package.json +2 -2
- package/rescript.json +1 -1
- package/src/Tilia.mjs +89 -152
- package/src/Tilia.res +80 -146
- package/src/Tilia.resi +24 -29
- package/llms-rescript.md +0 -387
- package/llms-typescript.md +0 -403
package/llms-rescript.md
DELETED
|
@@ -1,387 +0,0 @@
|
|
|
1
|
-
# Tilia LLM Guide (ReScript)
|
|
2
|
-
|
|
3
|
-
AI-focused ReScript documentation for the full Tilia API.
|
|
4
|
-
|
|
5
|
-
## Install
|
|
6
|
-
|
|
7
|
-
npm install tilia @tilia/react
|
|
8
|
-
|
|
9
|
-
## Primary Rule
|
|
10
|
-
|
|
11
|
-
- Use `carve` as the default for feature modules.
|
|
12
|
-
- `carve` must glue parts into one reactive object.
|
|
13
|
-
- Do not put business complexity inside `carve`.
|
|
14
|
-
- Put logic in standalone functions that take `self`.
|
|
15
|
-
- Bind with `field: derived(field)` when possible.
|
|
16
|
-
|
|
17
|
-
## Canonical Carve Pattern
|
|
18
|
-
|
|
19
|
-
```rescript
|
|
20
|
-
open Tilia
|
|
21
|
-
|
|
22
|
-
type feature = {
|
|
23
|
-
mutable count: int,
|
|
24
|
-
double: int,
|
|
25
|
-
add: int => unit,
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
type service = {
|
|
29
|
-
fetchCount: (int, int => unit) => unit,
|
|
30
|
-
updateCount: (int, unit => unit) => unit,
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
let double = (self: feature) => self.count * 2
|
|
34
|
-
|
|
35
|
-
let add = (service: service) => (self: feature) => (count: int) => {
|
|
36
|
-
let prevValue = self.count
|
|
37
|
-
self.count += count // optimistic write
|
|
38
|
-
service.updateCount(self.count, () => self.count = prevValue)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
let makeFeature = service =>
|
|
42
|
-
carve(({derived}) => {
|
|
43
|
-
count: source(0, service.fetchCount),
|
|
44
|
-
double: derived(double),
|
|
45
|
-
add: derived(add(service)),
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
let feature = makeFeature(service)
|
|
49
|
-
feature.add(4)
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
## Anti-Pattern (Avoid)
|
|
53
|
-
|
|
54
|
-
```rescript
|
|
55
|
-
open Tilia
|
|
56
|
-
|
|
57
|
-
// Avoid: logic-heavy body inside carve.
|
|
58
|
-
let cart = carve(({derived}) => {
|
|
59
|
-
items: [{price: 10.0, quantity: 1}],
|
|
60
|
-
total: derived(self => {
|
|
61
|
-
let mutable sum = 0.0
|
|
62
|
-
for i in 0 to Array.length(self.items) - 1 {
|
|
63
|
-
switch self.items[i] {
|
|
64
|
-
| Some(item) => sum = sum +. item.price *. Float.fromInt(item.quantity)
|
|
65
|
-
| None => ()
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
sum
|
|
69
|
-
}),
|
|
70
|
-
})
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
## Feature File Organization
|
|
74
|
-
|
|
75
|
-
Use this if the project does not provide other guidelines.
|
|
76
|
-
|
|
77
|
-
```text
|
|
78
|
-
[feature-name]/
|
|
79
|
-
index.res // carve glue
|
|
80
|
-
type.res // type of the feature
|
|
81
|
-
computed.res // all computed/derived values
|
|
82
|
-
actions.res // all mutating functions
|
|
83
|
-
service.res // external dependencies (db fetch, etc)
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
## Full API Map (ReScript)
|
|
87
|
-
|
|
88
|
-
### `tilia`
|
|
89
|
-
|
|
90
|
-
Wrap an object/array into a reactive proxy.
|
|
91
|
-
|
|
92
|
-
```rescript
|
|
93
|
-
let state = tilia({count: 0})
|
|
94
|
-
state.count = 1
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
### `carve`
|
|
98
|
-
|
|
99
|
-
Create a reactive feature object with helper-function injection via `derived`.
|
|
100
|
-
|
|
101
|
-
```rescript
|
|
102
|
-
type feature = {value: int, doubled: int}
|
|
103
|
-
let doubled = (self: feature) => self.value * 2
|
|
104
|
-
|
|
105
|
-
let feature = carve(({derived}) => {
|
|
106
|
-
value: 1,
|
|
107
|
-
doubled: derived(doubled),
|
|
108
|
-
})
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
### `observe`
|
|
112
|
-
|
|
113
|
-
Push reactivity. Callback re-runs when tracked reads change.
|
|
114
|
-
|
|
115
|
-
```rescript
|
|
116
|
-
observe(() => Js.log(state.count))
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
### `watch`
|
|
120
|
-
|
|
121
|
-
Split tracking and effect.
|
|
122
|
-
|
|
123
|
-
```rescript
|
|
124
|
-
watch(
|
|
125
|
-
() => state.count,
|
|
126
|
-
v => Js.log(v),
|
|
127
|
-
)
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
### `batch`
|
|
131
|
-
|
|
132
|
-
Group writes and flush once.
|
|
133
|
-
|
|
134
|
-
```rescript
|
|
135
|
-
batch(() => {
|
|
136
|
-
state.a = 1
|
|
137
|
-
state.b = 2
|
|
138
|
-
})
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
### `signal`
|
|
142
|
-
|
|
143
|
-
Single mutable reactive value + setter.
|
|
144
|
-
|
|
145
|
-
```rescript
|
|
146
|
-
let (count, setCount) = signal(0)
|
|
147
|
-
setCount(2)
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
### `derived` (signal API)
|
|
151
|
-
|
|
152
|
-
Create a derived signal from signals or reactive reads.
|
|
153
|
-
|
|
154
|
-
```rescript
|
|
155
|
-
let (a, setA) = signal(1)
|
|
156
|
-
let b = derived(() => a.value * 2)
|
|
157
|
-
setA(2)
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
### `lift`
|
|
161
|
-
|
|
162
|
-
Insert signal value into a `tilia` object.
|
|
163
|
-
|
|
164
|
-
```rescript
|
|
165
|
-
let (s, setS) = signal(0)
|
|
166
|
-
let state = tilia({count: lift(s), setS})
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
### `readonly`
|
|
170
|
-
|
|
171
|
-
Insert non-tracked immutable data wrapper.
|
|
172
|
-
|
|
173
|
-
```rescript
|
|
174
|
-
let app = tilia({form: readonly(bigData)})
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
### `computed`
|
|
178
|
-
|
|
179
|
-
Pull reactivity. Re-computes on read after dependency invalidation.
|
|
180
|
-
|
|
181
|
-
```rescript
|
|
182
|
-
let state = tilia({
|
|
183
|
-
count: 1,
|
|
184
|
-
double: computed(() => state.count * 2),
|
|
185
|
-
})
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
### `source` (async query and re-query)
|
|
189
|
-
|
|
190
|
-
Use `source(initial, setup)` for async loaders and re-loaders.
|
|
191
|
-
|
|
192
|
-
- **Tracking rule:** Never use `async` in the `setup` function. Tilia tracks reactive reads during synchronous execution of `setup` only. Tracking ONLY HAPPENS INSIDE A CALLBACK. Read dependencies synchronously, then delegate async work.
|
|
193
|
-
- `initial` is returned until `set` is called.
|
|
194
|
-
- `setup(previous, set)` runs on first read.
|
|
195
|
-
- `setup(previous, set)` runs again when tracked dependencies inside setup change.
|
|
196
|
-
- `set(next)` updates the source value imperatively.
|
|
197
|
-
- `previous` is the last emitted value, useful for incremental updates.
|
|
198
|
-
- `previous` also supports stale-while-revalidate UI (keep old data visible, e.g. greyed out, while reloading to avoid blinking).
|
|
199
|
-
|
|
200
|
-
```rescript
|
|
201
|
-
open Tilia
|
|
202
|
-
|
|
203
|
-
let sleep: unit => promise<unit> = async () =>
|
|
204
|
-
%raw(`new Promise(resolve => setTimeout(resolve, 10))`)
|
|
205
|
-
|
|
206
|
-
let fetchName = self => (previous, set) => {
|
|
207
|
-
// 1. Synchronous read (tracked)
|
|
208
|
-
let q = self.query
|
|
209
|
-
|
|
210
|
-
// 2. Delegate async work
|
|
211
|
-
let _ = sleep()->Promise.thenResolve(() => {
|
|
212
|
-
switch q {
|
|
213
|
-
| "helena" => set("Helena")
|
|
214
|
-
| "bob" => set("William")
|
|
215
|
-
| _ => set(previous ++ "+" ++ q)
|
|
216
|
-
}
|
|
217
|
-
})
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
let user = carve(({derived}) => {
|
|
221
|
-
query: "helena",
|
|
222
|
-
name: source("Loading", derived(fetchName)),
|
|
223
|
-
})
|
|
224
|
-
|
|
225
|
-
// Re-query
|
|
226
|
-
user.query = "bob"
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
#### `source` + `derived` inside `carve` (conditional loader strategy)
|
|
230
|
-
|
|
231
|
-
```rescript
|
|
232
|
-
open Tilia
|
|
233
|
-
|
|
234
|
-
let loader = service => self => (previous, set) => {
|
|
235
|
-
// 1. Synchronous read (tracked)
|
|
236
|
-
let id = self.projectId
|
|
237
|
-
set(stale(previous))
|
|
238
|
-
|
|
239
|
-
// 2. Delegate async work
|
|
240
|
-
let _ = service.loadProject(id)->Promise.thenResolve(project => {
|
|
241
|
-
set(loaded(project))
|
|
242
|
-
})
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
let selectProject = self => id => self.projectId = id
|
|
246
|
-
|
|
247
|
-
let projectBranch = carve(({derived}) => {
|
|
248
|
-
projectId: "main",
|
|
249
|
-
project: source(empty(), derived(loader(service))),
|
|
250
|
-
selectProject: derived(selectProject),
|
|
251
|
-
})
|
|
252
|
-
```
|
|
253
|
-
|
|
254
|
-
Use this when loading depends on feature fields (`self.projectId`) and keep `previous` to preserve old UI data until new values are ready.
|
|
255
|
-
|
|
256
|
-
#### Loading state sentinel pattern
|
|
257
|
-
|
|
258
|
-
To represent a "loading" state, use an empty tilia object as a sentinel. Use `===` identity comparison to distinguish "loading" from "loaded but empty."
|
|
259
|
-
|
|
260
|
-
```rescript
|
|
261
|
-
let loading = tilia(Dict.make())
|
|
262
|
-
let repo = carve(({derived}) => {
|
|
263
|
-
data: source(loading, derived(loader(service))),
|
|
264
|
-
})
|
|
265
|
-
if repo.data === loading { /* still loading */ }
|
|
266
|
-
```
|
|
267
|
-
|
|
268
|
-
### `store`
|
|
269
|
-
|
|
270
|
-
Managed state with setup returning current value and receiving a setter.
|
|
271
|
-
|
|
272
|
-
```rescript
|
|
273
|
-
type auth = LoggedOut | Loading | LoggedIn
|
|
274
|
-
|
|
275
|
-
let machine = set => LoggedOut
|
|
276
|
-
let app = tilia({auth: store(machine)})
|
|
277
|
-
```
|
|
278
|
-
|
|
279
|
-
### `changing` (write tracking for sync connectors)
|
|
280
|
-
|
|
281
|
-
Track key-level writes on a tilia-proxied dict. Takes an accessor `() => dict<'a>` so the tracker can follow source swaps. Returns `{ changes, mute }`: `changes` drains accumulated changes into `{ upsert, remove }` when read by `watch`; `mute` runs a callback with tracking suppressed. `upsert` contains objects captured at write time. `remove` contains keys of deleted entries. Last write wins per key. Each call creates an independent accumulator.
|
|
282
|
-
|
|
283
|
-
- `changing(() => data)` -- returns `{ changes, mute }`. `changes` drains accumulated changes as `{ upsert: array<'a>, remove: array<string> }`. `mute(fn)` runs `fn` without tracking (for inbound writes).
|
|
284
|
-
- `changing(() => data, ~guard=() => expr)` -- optional reactive guard. When guard returns false, changes accumulate silently. When guard flips to true, all accumulated changes drain and the effect fires.
|
|
285
|
-
|
|
286
|
-
```rescript
|
|
287
|
-
open Tilia
|
|
288
|
-
|
|
289
|
-
type item = {id: string, name: string, quantity: int}
|
|
290
|
-
type syncService = {
|
|
291
|
-
upsert: array<item> => unit,
|
|
292
|
-
remove: array<string> => unit,
|
|
293
|
-
}
|
|
294
|
-
type localDb = {
|
|
295
|
-
upsert: array<item> => unit,
|
|
296
|
-
remove: array<string> => unit,
|
|
297
|
-
}
|
|
298
|
-
type actor = {mutable online: bool}
|
|
299
|
-
|
|
300
|
-
let makeItemsRepo = (service: syncService, localDb: localDb, actor: actor) => {
|
|
301
|
-
let data: dict<item> = tilia(Dict.make())
|
|
302
|
-
|
|
303
|
-
// Local DB: always sync
|
|
304
|
-
let {changes} = changing(() => data)
|
|
305
|
-
watch(changes, ({upsert, remove}) => {
|
|
306
|
-
localDb.upsert(upsert)
|
|
307
|
-
localDb.remove(remove)
|
|
308
|
-
})
|
|
309
|
-
|
|
310
|
-
// Remote: sync only when online
|
|
311
|
-
let remote = changing(() => data, ~guard=() => actor.online)
|
|
312
|
-
watch(remote.changes, ({upsert, remove}) => {
|
|
313
|
-
service.upsert(upsert)
|
|
314
|
-
service.remove(remove)
|
|
315
|
-
})
|
|
316
|
-
|
|
317
|
-
// Inbound: apply remote data without triggering outbound sync
|
|
318
|
-
// remote.mute(() => Dict.assign(data, remoteData))
|
|
319
|
-
|
|
320
|
-
data
|
|
321
|
-
}
|
|
322
|
-
```
|
|
323
|
-
|
|
324
|
-
Multiple repos use the same pattern:
|
|
325
|
-
|
|
326
|
-
```rescript
|
|
327
|
-
let settings = changing(() => settingsRepo.data, ~guard=() => actor.online)
|
|
328
|
-
watch(settings.changes, ({upsert, remove}) => {
|
|
329
|
-
settingsService.upsert(upsert)
|
|
330
|
-
settingsService.remove(remove)
|
|
331
|
-
})
|
|
332
|
-
```
|
|
333
|
-
|
|
334
|
-
Architectural summary:
|
|
335
|
-
- `source` handles inbound (loading from external into reactive data)
|
|
336
|
-
- `changing` + `watch` handles outbound (pushing reactive writes to external)
|
|
337
|
-
- `changes` returns `{ upsert, remove }` — upsert contains objects captured at write time, remove contains keys of deleted entries
|
|
338
|
-
- `mute` prevents feedback loops: inbound writes are reactive but not tracked
|
|
339
|
-
- The guard parameter leverages tilia's natural tracking for offline accumulation
|
|
340
|
-
- The accessor pattern `() => data` lets the tracker follow source swaps automatically
|
|
341
|
-
|
|
342
|
-
### `make`
|
|
343
|
-
|
|
344
|
-
Create isolated Tilia context (`tilia`, `carve`, `observe`, etc.).
|
|
345
|
-
|
|
346
|
-
```rescript
|
|
347
|
-
let ctx = make()
|
|
348
|
-
let local = ctx.tilia({count: 0})
|
|
349
|
-
```
|
|
350
|
-
|
|
351
|
-
## React Integration
|
|
352
|
-
|
|
353
|
-
### Dependency Injection (useApp)
|
|
354
|
-
|
|
355
|
-
Create a context-based hook to inject the app state. Track with `leaf`, provision app with `useApp`.
|
|
356
|
-
|
|
357
|
-
```rescript
|
|
358
|
-
let context = React.createContext(emptyApp)
|
|
359
|
-
let useApp = () => React.useContext(context)
|
|
360
|
-
```
|
|
361
|
-
|
|
362
|
-
### Avoid Over-Destructuring
|
|
363
|
-
|
|
364
|
-
Do not destructure all state variables at the top of a component, as it defeats granular tracking by reading everything immediately.
|
|
365
|
-
|
|
366
|
-
```rescript
|
|
367
|
-
// ❌ BAD: Reads `total` immediately, defeating conditional tracking
|
|
368
|
-
let {cart: {total}} = useApp()
|
|
369
|
-
|
|
370
|
-
// ✅ GOOD: Read properties only when needed in the JSX
|
|
371
|
-
let {cart} = useApp()
|
|
372
|
-
<div>{React.float(cart.total)}</div>
|
|
373
|
-
```
|
|
374
|
-
|
|
375
|
-
## Generation Rules for AI
|
|
376
|
-
|
|
377
|
-
- Prefer `carve` over ad-hoc `tilia` for feature modules.
|
|
378
|
-
- Keep `carve` declarative and short.
|
|
379
|
-
- Extract domain logic into external functions.
|
|
380
|
-
- Functions wired with `derived` take `self` first.
|
|
381
|
-
- Use `source` for async query/re-query flows.
|
|
382
|
-
- Explain `set` as imperative emitter and `previous` as last emitted value.
|
|
383
|
-
- Use `changing(() => data)` + `watch` for outbound sync connectors (persistence, remote sync). Destructure `{ changes, mute }`.
|
|
384
|
-
- `changes` returns `{ upsert, remove }` — upsert contains objects, remove contains keys. Use the accessor pattern so the tracker follows source swaps.
|
|
385
|
-
- Use `mute` for inbound writes to prevent feedback loops in bidirectional sync.
|
|
386
|
-
- `source` = inbound, `changing` = outbound.
|
|
387
|
-
|