|
| 1 | +# Implementation Plan: LoroTree Support in Mirror |
| 2 | + |
| 3 | +Status: Completed • Owner: Core • Last updated: 2025-08-28 |
| 4 | + |
| 5 | +## Goals |
| 6 | + |
| 7 | +- Provide first-class LoroTree support in Mirror for bidirectional sync between app state and loro-crdt Tree containers. |
| 8 | +- Maintain parity with existing Map/List/Text/MovableList flows, including schema validation and event-driven state updates. |
| 9 | + |
| 10 | +## Non-Goals |
| 11 | + |
| 12 | +- Rich tree-aware UIs (out of scope, Mirror focuses on sync/state). |
| 13 | +- Automatic inference of Tree from plain arrays without schema. |
| 14 | + |
| 15 | +## Current Gaps (as-is) |
| 16 | + |
| 17 | +- Mirror has no Tree handling in: root init, nested registration, read path, or write path. |
| 18 | +- loroEventApply does not apply `tree` diffs; path walker cannot resolve nodes by id within arrays. |
| 19 | +- diff does not compute structural Tree diffs. |
| 20 | +- Schema lacks `loro-tree` type/guards/defaults; utils lacks `Tree` in helpers. |
| 21 | + |
| 22 | +## State Model |
| 23 | + |
| 24 | +Represent a Tree in Mirror state as nested nodes: |
| 25 | + |
| 26 | +```ts |
| 27 | +type TreeNode<T = Record<string, unknown>> = { |
| 28 | + id: string; // TreeID string from Loro |
| 29 | + data: T; // node metadata (validated by nodeSchema) |
| 30 | + children: TreeNode[]; // ordered children |
| 31 | +} |
| 32 | + |
| 33 | +// Mirror state value for a LoroTree: array of roots |
| 34 | +type TreeValue<T> = TreeNode<T>[] |
| 35 | +``` |
| 36 | +
|
| 37 | +Notes: |
| 38 | +- When app creates new nodes via state, `id` may be omitted; Loro assigns it on create; events will fill it back. |
| 39 | +- Node data is a LoroMap schema (`nodeSchema`) for validation and nested containers. |
| 40 | +
|
| 41 | +## Public Schema API Changes |
| 42 | +
|
| 43 | +- Add `schema.LoroTree(nodeSchema, options?)` |
| 44 | + - `type: "loro-tree"`, `getContainerType(): "Tree"`. |
| 45 | + - `nodeSchema`: `LoroMapSchema<Record<string, SchemaType>>` for `node.data`. |
| 46 | +- `types.ts` |
| 47 | + - Add `LoroTreeSchema<T>` to `SchemaType` and `ContainerSchemaType` unions. |
| 48 | + - `InferType<LoroTreeSchema<T>>` resolves to `Array<{ id: string; data: InferType<T>; children: ... }>`. |
| 49 | +- `validators.ts` |
| 50 | + - Add `isLoroTreeSchema` type guard. |
| 51 | + - `validateSchema` support: top-level is `Array`; validate `node.data` recursively using `nodeSchema`; validate `children` recursively. |
| 52 | + - `getDefaultValue` returns `[]` when required; else `undefined`. |
| 53 | +
|
| 54 | +## Core Changes (Read Path) |
| 55 | +
|
| 56 | +`loroEventApply.ts` |
| 57 | +- Implement `tree` diff application: |
| 58 | + - Initialize target as `[]` if missing. |
| 59 | + - `create`: insert `{ id, data: {}, children: [] }` at `parent/index` (root if `parent` undefined). |
| 60 | + - `move`: remove from `oldParent/oldIndex` and insert at `parent/index` (if same parent and `oldIndex < index`, decrement target index). |
| 61 | + - `delete`: remove subtree at `oldParent/oldIndex`. |
| 62 | +- Enhance path resolution to support node lookup by `id` inside arrays so map diffs to `node.data` apply cleanly: |
| 63 | + - When current is an array and next segment is a string, interpret as `TreeID` string and select element with `elem.id === seg`. |
| 64 | +- Continue using `applyMapDiff` for `node.data` changes. |
| 65 | +
|
| 66 | +## Core Changes (Write Path) |
| 67 | +
|
| 68 | +`diff.ts` |
| 69 | +- Extend `diffContainer` to handle `ContainerType === "Tree"` with `diffTree(...)`. |
| 70 | +- Implement `diffTree` (structure + node data): |
| 71 | + - Build old/new id->node maps and parent relationships. |
| 72 | + - Deletions: nodes in old not in new (delete deepest-first to avoid orphaning). |
| 73 | + - Creates: nodes in new not in old (create top-down so parents exist). |
| 74 | + - Moves: common nodes whose `(parentId, index)` changed. |
| 75 | + - Node data updates: for common nodes, route to `diffContainer` of `node.data` via the attached `LoroMap` container id. |
| 76 | +
|
| 77 | +`mirror.ts` |
| 78 | +- `Change` union: add Tree operations |
| 79 | + - `tree-create`: `{ container: ContainerID, kind: "tree-create", parent?: TreeID, index: number }` |
| 80 | + - `tree-move`: `{ container: ContainerID, kind: "tree-move", target: TreeID, parent?: TreeID, index: number }` |
| 81 | + - `tree-delete`: `{ container: ContainerID, kind: "tree-delete", target: TreeID }` |
| 82 | +- Initialization |
| 83 | + - Include `"loro-tree"` in root container registration and `getRootContainerByType` calls. |
| 84 | + - For Tree nested registration, when visiting nodes, register `node.data` container with `nodeSchema`. |
| 85 | +- Loro event registration |
| 86 | + - For `event.diff.type === "tree"`, on `create` resolve node then `registerContainer(node.data.id, nodeSchema)`. |
| 87 | +- Apply changes |
| 88 | + - `applyRootChanges`: support `"loro-tree"` root and forward to `updateTopLevelContainer`. |
| 89 | + - `applyContainerChanges`: add `case "Tree"` to handle `tree-*` changes via `LoroTree.createNode/move/delete`. |
| 90 | + - `updateTopLevelContainer`: add `"Tree"` branch to compute tree diffs and apply. |
| 91 | + - `initializeContainer`: when kind `"Tree"` and initial value present, seed structure with `createNode`, then initialize each `node.data` using schema. |
| 92 | + - `createContainerFromSchema`: return `[new LoroTree(), "Tree"]` for `"loro-tree"`. |
| 93 | + - `getSchemaForChild`: when parent schema is `loro-tree`, return `nodeSchema` for node data. |
| 94 | +
|
| 95 | +`utils.ts` |
| 96 | +- `getRootContainerByType`: add `"Tree" -> doc.getTree(key)`. |
| 97 | +- Do not infer `Tree` in `tryInferContainerType` (requires schema to avoid ambiguity with plain lists). |
| 98 | +
|
| 99 | +## Tests |
| 100 | +
|
| 101 | +New: `packages/core/tests/core/mirror-tree.test.ts` |
| 102 | +- FROM_LORO: create/move/delete in Loro updates Mirror state (`{id,data,children}`), including nested `data` updates. |
| 103 | +- TO_LORO: mutating state (new nodes without id, moves, deletes, data changes) updates Loro via `tree-*` changes and map updates. |
| 104 | +- Mixed operations in one `setState` produce consistent changes. |
| 105 | +- Fractional index compatible: numeric `index` passed; Loro handles ordering. |
| 106 | +
|
| 107 | +## Edge Cases & Performance |
| 108 | +
|
| 109 | +- Same-parent move index adjustment when `oldIndex < index`. |
| 110 | +- Deepest-first deletion ordering. |
| 111 | +- Optional in-memory index per tree during event application for O(1) node lookup (can be a follow-up optimization). |
| 112 | +- Concurrency handled by Loro; Mirror applies diffs idempotently. |
| 113 | +
|
| 114 | +## Rollout & Verification |
| 115 | +
|
| 116 | +1) Baseline |
| 117 | +- [x] `pnpm build && pnpm test && pnpm typecheck` |
| 118 | +- [ ] `pnpm lint` |
| 119 | +
|
| 120 | +2) Schema & Utils |
| 121 | +- [x] Add `LoroTreeSchema` to `types.ts` (+ InferType) |
| 122 | +- [x] Add `schema.LoroTree()` to `schema/index.ts` |
| 123 | +- [x] Add `isLoroTreeSchema`, extend validators and defaults |
| 124 | +- [x] Add `Tree` to `getRootContainerByType` |
| 125 | +
|
| 126 | +3) Read Path |
| 127 | +- [x] loroEventApply: apply `tree` diffs (create/move/delete) |
| 128 | +- [x] loroEventApply: path walker supports node id in arrays |
| 129 | +- [x] Register `node.data` containers on tree creates |
| 130 | +
|
| 131 | +4) Write Path |
| 132 | +- [x] diff: add `Tree` branch and implement `diffTree` |
| 133 | +- [x] mirror: extend `Change` union with `tree-*` |
| 134 | +- [x] mirror: handle `case "Tree"` in `applyContainerChanges` |
| 135 | +- [x] mirror: update top-level container branch for `"Tree"` |
| 136 | +- [x] mirror: nested registration + initialization for `node.data` |
| 137 | +
|
| 138 | +5) Tests |
| 139 | +- [x] Add `mirror-tree.test.ts` with FROM_LORO, TO_LORO, mixed flows |
| 140 | +- [x] Run and fix regressions |
| 141 | +
|
| 142 | +6) Docs |
| 143 | +- [ ] README entry: Tree shape `{ id, data, children }` and schema requirements |
| 144 | +
|
| 145 | +7) Final QA |
| 146 | +- [x] `pnpm build` |
| 147 | +- [x] `pnpm test` |
| 148 | +- [ ] `pnpm lint` |
| 149 | +- [x] `pnpm typecheck` |
| 150 | +
|
| 151 | +## Progress Notes |
| 152 | +
|
| 153 | +- Normalized tree JSON from `{ id, meta, children }` to `{ id, data, children }` during initialization for consistent state shape. |
| 154 | +- Scoped path remapping so only tree node `meta` is treated as `data` (does not affect root `meta` maps), fixing a regression in state.test profile.bio. |
| 155 | +- Initial Tree top-level updates rebuild structure; `diffTree` is implemented and can be used at root for minimal ops in a follow-up if desired. |
| 156 | +
|
| 157 | +## Risks / Open Questions |
| 158 | +
|
| 159 | +- Node identification in state: require `id` to match Loro `TreeID` (string). For newly created nodes without id, Mirror will create and fill id from events; interim state may temporarily show empty `id` values—acceptable for UI with optimistic updates. |
| 160 | +- Large tree performance: consider indexing maps for event application if profiling shows hotspots. |
| 161 | +- Schema composition: `nodeSchema` can itself contain containers; ensure nested registration paths are correct. |
0 commit comments