@katn30/trakr 4.1.0 → 4.2.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 +27 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -463,13 +463,15 @@ Adding or removing a `TrackedObject` from a `TrackedCollection` transitions its
|
|
|
463
463
|
```typescript
|
|
464
464
|
const item = tracker.construct(() => new ItemModel(tracker)); // Unchanged
|
|
465
465
|
items.push(item); // → Insert
|
|
466
|
-
items.remove(item); // → Unchanged (was never saved)
|
|
466
|
+
items.remove(item); // → Unchanged, and untracked (was never saved)
|
|
467
467
|
|
|
468
468
|
const loaded = tracker.construct(() => new ItemModel(tracker, { id: 1 })); // Unchanged
|
|
469
469
|
items.push(loaded); // → Insert
|
|
470
470
|
items.remove(loaded); // → Deleted
|
|
471
471
|
```
|
|
472
472
|
|
|
473
|
+
Removing an item whose state is `Insert` collapses it as if it had never existed: the state resets to `Unchanged` and the object is removed from `tracker.trackedObjects` in the same undo step. You do not need to call `destroy()` yourself — the collection handles it. Undo of the remove re-tracks the item and puts it back in the collection at state `Insert`.
|
|
474
|
+
|
|
473
475
|
**Via @Tracked property**
|
|
474
476
|
|
|
475
477
|
When a `@Tracked` property holds a `TrackedObject` value, assigning to it has the same effect: the outgoing value transitions to `Deleted` (or `Unchanged` if it was `Insert`), and the incoming value transitions to `Insert`:
|
|
@@ -536,7 +538,7 @@ There is no separate redo transition. Redo simply re-runs the original `do` acti
|
|
|
536
538
|
|
|
537
539
|
#### Key notes
|
|
538
540
|
|
|
539
|
-
**`removed/do` from `Insert` collapses to `Unchanged
|
|
541
|
+
**`removed/do` from `Insert` collapses to `Unchanged` and untracks the object** — if an object was added and then removed before ever being committed, it was never persisted. The transition resets `dirtyCounter` to zero, removes the object from `tracker.trackedObjects`, and clears its dependency-tracking entries — as if the add never happened. Nothing needs to be sent to the server, and the object no longer contributes to `tracker.isValid`. Undo of the remove re-tracks the object and restores its previous validity accounting.
|
|
540
542
|
|
|
541
543
|
**`committed/undo` reverses the server operation** — undoing past a commit puts the object into the state that requires the inverse server operation. Undoing a committed INSERT requires a DELETE; undoing a committed DELETE requires a new INSERT; undoing a committed UPDATE requires another UPDATE with the pre-edit values.
|
|
542
544
|
|
|
@@ -1190,7 +1192,7 @@ accessor status: string = '';
|
|
|
1190
1192
|
|
|
1191
1193
|
### `TrackedCollection<T>`
|
|
1192
1194
|
|
|
1193
|
-
A
|
|
1195
|
+
A tracked collection with a full array-shaped API. All mutation methods are recorded and undoable. Implements `Array<T>` for type-level interop with array-consuming APIs — with one runtime gap, described in **Positional access at runtime** below.
|
|
1194
1196
|
|
|
1195
1197
|
```typescript
|
|
1196
1198
|
const items = new TrackedCollection<string>(tracker);
|
|
@@ -1206,6 +1208,28 @@ const items = new TrackedCollection<string>(
|
|
|
1206
1208
|
);
|
|
1207
1209
|
```
|
|
1208
1210
|
|
|
1211
|
+
**Positional access at runtime**
|
|
1212
|
+
|
|
1213
|
+
`TrackedCollection<T>` declares the `[n: number]: T` index signature (inherited from `implements Array<T>`), but numeric-index access is **not** wired up at runtime. TypeScript accepts `col[n]` and `col[n] = x` without complaint, but neither behaves like a real array:
|
|
1214
|
+
|
|
1215
|
+
- `col[0]` returns `undefined` at runtime, even when `col.length > 0`.
|
|
1216
|
+
- `col[0] = x` does not mutate the collection and does not trigger tracking. It silently sets a data property on the class instance that shadows the (unimplemented) indexer — subsequent `col[0]` reads will return `x` while `col.length`, iteration, and `col.collection` still reflect the real state. This divergence will not surface as an error; it will surface as inconsistent UI or a save payload missing the write.
|
|
1217
|
+
|
|
1218
|
+
The interface is kept for interop (passing a `TrackedCollection` to code typed against `Array<T>`), but the runtime backing is deliberately not provided: wiring it up requires either a `Proxy` (breaks instance identity across the tracker API and adds an indirection layer on every property access) or per-index `defineProperty` accessors (allocates one closure per element, and numeric properties become enumerable and appear in `Object.keys` and `for...in`). Neither trade-off is worth it for a pattern the class already has first-class alternatives for.
|
|
1219
|
+
|
|
1220
|
+
Use these instead:
|
|
1221
|
+
|
|
1222
|
+
| Instead of | Use |
|
|
1223
|
+
|---|---|
|
|
1224
|
+
| `col[n]` (read) | `col.at(n)` — participates in dependency tracking |
|
|
1225
|
+
| — | `col.collection[n]` — direct read of the underlying array, no dependency tracking |
|
|
1226
|
+
| `col[n] = x` (write) | `col.replaceAt(n, x)` — tracked, undoable |
|
|
1227
|
+
| — | `col.splice(n, 1, x)` — tracked, undoable |
|
|
1228
|
+
| `for (let i = 0; i < col.length; i++) col[i]` | `for (const item of col) ...` |
|
|
1229
|
+
| — | `col.forEach(...)`, `col.map(...)`, etc. |
|
|
1230
|
+
|
|
1231
|
+
When passing a `TrackedCollection` to a third-party function typed against `Array<T>` that reads by index internally, pass `col.collection` instead. It exposes the underlying `T[]` with zero copying. Treat it as read-only: any mutation applied to that reference bypasses the tracker entirely.
|
|
1232
|
+
|
|
1209
1233
|
**Tracked mutation methods**
|
|
1210
1234
|
|
|
1211
1235
|
All of these create undo steps:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@katn30/trakr",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"description": "TypeScript state-management library implementing the Unit of Work pattern, with object tracking for undo/redo and validation support.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/prod/index.cjs",
|