|
5 | 5 | Contents
|
6 | 6 |
|
7 | 7 | - Installation & Imports
|
8 |
| - - Core: Mirror and Store |
| 8 | +- Core: Mirror |
9 | 9 | - Schema Builder
|
10 | 10 | - Validation & Defaults
|
11 | 11 | - Utilities (Advanced)
|
|
15 | 15 | ## Installation & Imports
|
16 | 16 |
|
17 | 17 | - Install: `npm install loro-mirror loro-crdt`
|
18 |
| - - Import styles: |
19 |
| - - Named imports (recommended): `import { Mirror, createStore, schema } from "loro-mirror"` |
| 18 | +- Import styles: |
| 19 | + - Named imports (recommended): `import { Mirror, schema } from "loro-mirror"` |
20 | 20 | - Default (convenience bundle of `schema` + `core`): `import loroMirror from "loro-mirror"`
|
21 | 21 |
|
22 |
| - ## Core: Mirror and Store |
| 22 | +## Core: Mirror |
23 | 23 |
|
24 | 24 | ### Mirror
|
25 | 25 |
|
|
31 | 31 | - `validateUpdates?: boolean` (default `true`) — validate on `setState`
|
32 | 32 | - `throwOnValidationError?: boolean` (default `false`) — throw on schema validation errors
|
33 | 33 | - `debug?: boolean` — verbose logging to console for diagnostics
|
34 |
| - - `checkStateConsistency?: boolean` (default `false`) — deep checks in-memory state equals normalized `doc` JSON after `setState` |
35 |
| - - `inferOptions?: { defaultLoroText?: boolean; defaultMovableList?: boolean }` — inference hints when no schema covers a field |
| 34 | + - `checkStateConsistency?: boolean` (default `false`) — deep checks in-memory state equals normalized `doc` JSON after `setState` |
| 35 | + - `inferOptions?: { defaultLoroText?: boolean; defaultMovableList?: boolean }` — inference hints when no schema covers a field |
36 | 36 |
|
37 | 37 | - Methods
|
38 | 38 | - `getState(): InferType<S>` — returns the current mirror state (immutable snapshot)
|
|
87 | 87 | unsub();
|
88 | 88 | ```
|
89 | 89 |
|
90 |
| - ### Store |
91 |
| - |
92 |
| - Convenience wrapper around `Mirror` with a minimal Redux‑like surface. |
93 |
| - |
94 |
| - - `createStore(options): Store<S>` |
95 |
| - - `options: CreateStoreOptions<S>` |
96 |
| - - `doc: LoroDoc` |
97 |
| - - `schema: S` |
98 |
| - - `initialState?: Partial<InferInputType<S>>` |
99 |
| - - `validateUpdates?: boolean` |
100 |
| - - `throwOnValidationError?: boolean` (default `true`) |
101 |
| - - `debug?: boolean` |
102 |
| - - `checkStateConsistency?: boolean` |
103 |
| - - Returns `Store<S>` with: |
104 |
| - - `getState(): InferType<S>` |
105 |
| - - `setState(updater): Promise<void>` — same updater shapes as Mirror; await in non-React code |
106 |
| - - `subscribe(cb): () => void` (same metadata as Mirror) |
107 |
| - - `getMirror(): Mirror<S>` |
108 |
| - - `getLoro(): LoroDoc` |
109 |
| - |
110 |
| - - `createReducer(handlers) -> (store) => dispatch(type, payload)` |
111 |
| - - Define an object of handlers that mutate an Immer draft. The returned `dispatch` wires those actions to `store.setState`. |
112 |
| - |
113 |
| - Example |
114 |
| - |
115 |
| - ```ts |
116 |
| - import { createStore, createReducer, schema } from "loro-mirror"; |
117 |
| - import { LoroDoc } from "loro-crdt"; |
118 |
| - |
119 |
| - const s = schema({ |
120 |
| - todos: schema.LoroList(schema.LoroMap({ id: schema.String(), text: schema.String(), done: schema.Boolean({ defaultValue: false }) }), (t) => t.id), |
121 |
| - }); |
122 |
| - |
123 |
| - const store = createStore({ doc: new LoroDoc(), schema: s }); |
124 |
| - |
125 |
| - const actions = { |
126 |
| - add(state, { id, text }: { id: string; text: string }) { |
127 |
| - state.todos.push({ id, text }); |
128 |
| - }, |
129 |
| - toggle(state, id: string) { |
130 |
| - const item = state.todos.find((t) => t.id === id); |
131 |
| - if (item) item.done = !item.done; |
132 |
| - }, |
133 |
| - }; |
134 |
| - |
135 |
| - const dispatch = createReducer(actions)(store); |
136 |
| - |
137 |
| - dispatch("add", { id: "a", text: "Task" }); |
138 |
| - dispatch("toggle", "a"); |
139 |
| - ``` |
140 | 90 |
|
141 | 91 | ## Schema Builder
|
142 | 92 |
|
|
278 | 228 |
|
279 | 229 | ---
|
280 | 230 |
|
281 |
| - Questions or gaps? If you need deeper internals (diff pipelines, event application), explore the source under `src/core/` — but for most apps, `Mirror`, the schema builders, and `createStore` are all you need. |
| 231 | +Questions or gaps? If you need deeper internals (diff pipelines, event application), explore the source under `src/core/` — but for most apps, `Mirror` and the schema builders are all you need. |
0 commit comments