Skip to content

Commit f28f06c

Browse files
committed
refactor: rm the thin store abstraction
1 parent 5d0b6d3 commit f28f06c

File tree

11 files changed

+69
-449
lines changed

11 files changed

+69
-449
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules
22
dist
33
# Ignore TS incremental build info everywhere in the repo
44
*.tsbuildinfo
5+
.DS_Store

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pnpm add loro-mirror loro-crdt
3535

3636
```typescript
3737
import { LoroDoc } from "loro-crdt";
38-
import { schema, createStore } from "loro-mirror";
38+
import { Mirror, schema } from "loro-mirror";
3939

4040
// Define your schema
4141
const todoSchema = schema({
@@ -51,16 +51,16 @@ const todoSchema = schema({
5151

5252
// Create a Loro document
5353
const doc = new LoroDoc();
54-
// Create a store
55-
const store = createStore({
54+
// Create a mirror
55+
const mirror = new Mirror({
5656
doc,
5757
schema: todoSchema,
5858
initialState: { todos: [] },
5959
});
6060

6161
// Update the state (immutable update)
6262
// Note: setState is async; await it in non-React code
63-
await store.setState((s) => ({
63+
await mirror.setState((s) => ({
6464
...s,
6565
todos: [
6666
...s.todos,
@@ -72,7 +72,7 @@ await store.setState((s) => ({
7272
}));
7373

7474
// Or: draft-style updates (mutate a draft)
75-
await store.setState((state) => {
75+
await mirror.setState((state) => {
7676
state.todos.push({
7777
text: "Learn Loro Mirror",
7878
completed: false,
@@ -82,7 +82,7 @@ await store.setState((state) => {
8282
});
8383

8484
// Subscribe to state changes
85-
store.subscribe((state) => {
85+
mirror.subscribe((state) => {
8686
console.log("State updated:", state);
8787
});
8888
```

api.md

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Contents
66

77
- Installation & Imports
8-
- Core: Mirror and Store
8+
- Core: Mirror
99
- Schema Builder
1010
- Validation & Defaults
1111
- Utilities (Advanced)
@@ -15,11 +15,11 @@
1515
## Installation & Imports
1616

1717
- 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"`
2020
- Default (convenience bundle of `schema` + `core`): `import loroMirror from "loro-mirror"`
2121

22-
## Core: Mirror and Store
22+
## Core: Mirror
2323

2424
### Mirror
2525

@@ -31,8 +31,8 @@
3131
- `validateUpdates?: boolean` (default `true`) — validate on `setState`
3232
- `throwOnValidationError?: boolean` (default `false`) — throw on schema validation errors
3333
- `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
3636

3737
- Methods
3838
- `getState(): InferType<S>` — returns the current mirror state (immutable snapshot)
@@ -87,56 +87,6 @@
8787
unsub();
8888
```
8989

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-
```
14090

14191
## Schema Builder
14292

@@ -278,4 +228,4 @@
278228

279229
---
280230

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.

packages/core/README.md

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
## Quick Start
44

5-
Define a schema and wire a store to a `LoroDoc`.
5+
Define a schema and instantiate a `Mirror` with a `LoroDoc`.
66

77
```ts
8-
import { createStore, schema } from "loro-mirror";
8+
import { Mirror, schema } from "loro-mirror";
99
import { LoroDoc } from "loro-crdt";
1010

1111
const doc = new LoroDoc();
@@ -27,7 +27,7 @@ const appSchema = schema({
2727
notes: schema.LoroText(),
2828
});
2929

30-
const store = createStore({ doc, schema: appSchema });
30+
const store = new Mirror({ doc, schema: appSchema });
3131

3232
// Read state
3333
const state = store.getState();
@@ -53,8 +53,6 @@ const unsubscribe = store.subscribe((next, { direction }) => {
5353
});
5454
```
5555

56-
Tip: If you need direct access, `store.getMirror()` returns the underlying `Mirror`.
57-
5856
## Installation
5957

6058
```bash
@@ -92,15 +90,6 @@ Trees are advanced usage; see Advanced: Trees at the end.
9290

9391
Types: `SyncDirection`, `UpdateMetadata`, `SetStateOptions`.
9492

95-
### Store
96-
97-
- `createStore({ doc, schema, initialState?, validateUpdates?=true, throwOnValidationError?=true, debug?=false })`
98-
- Returns: `{ getState, setState, subscribe, getMirror, getLoro }`
99-
100-
### Reducer Helper
101-
102-
- `createReducer(handlers) -> (store) => dispatch(type, payload)`
103-
- Handlers get an Immer draft; call `dispatch("addTodo", { text })` etc.
10493

10594
### Schema Builder
10695

@@ -143,9 +132,9 @@ Trees are for hierarchical data where each node has a `data` map. The state shap
143132
```ts
144133
const node = schema.LoroMap({ name: schema.String({ required: true }) });
145134
const s = schema({ tree: schema.LoroTree(node) });
146-
const store = createStore({ doc: new LoroDoc(), schema: s });
135+
const mirror = new Mirror({ doc: new LoroDoc(), schema: s });
147136

148-
await store.setState((st) => {
137+
await mirror.setState((st) => {
149138
st.tree.push({ data: { name: "root" }, children: [] });
150139
});
151140
```

packages/core/src/core/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@
22
* Core mirroring functionality for syncing application state with Loro CRDT
33
*/
44

5-
export * from "./mirror";
6-
export * from "./state";
7-
export * from "./utils";
5+
export { Mirror, toNormalizedJson } from "./mirror";

packages/core/src/core/state.ts

Lines changed: 0 additions & 149 deletions
This file was deleted.

packages/core/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
// Re-export all public APIs
77
export * from "./schema";
8-
export * from "./core";
9-
export * from "./constants";
8+
export { Mirror, toNormalizedJson } from "./core";
109

1110
// Default export
1211
import * as schema from "./schema";

0 commit comments

Comments
 (0)