@homeostate/store-mobx-state-tree 0.1.1 → 0.1.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @homeostate/store-mobx-state-tree
2
2
 
3
+ ## 0.1.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 30abf27: Remove draft release warnings, update docs links
8
+ - Updated dependencies [30abf27]
9
+ - @homeostate/core@0.1.2
10
+
3
11
  ## 0.1.1
4
12
 
5
13
  ### Patch Changes
package/LICENSE CHANGED
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,8 +1,5 @@
1
1
  # @homeostate/store-mobx-state-tree
2
2
 
3
- > **Early release.** The API is still being designed and may change in any `0.x` minor
4
- > version.
5
-
6
3
  [MobX-State-Tree](https://mobx-state-tree.js.org) store adapter for
7
4
  [`@homeostate/core`](https://github.com/mixedrays/homeostate/tree/main/packages/core).
8
5
  It keeps a state tree node in sync with a CRDT backend such as
package/package.json CHANGED
@@ -1,8 +1,26 @@
1
1
  {
2
2
  "name": "@homeostate/store-mobx-state-tree",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "MobX-State-Tree adapter for @homeostate/core",
5
+ "license": "MIT",
5
6
  "type": "module",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/mixedrays/homeostate.git",
10
+ "directory": "packages/store-mobx-state-tree"
11
+ },
12
+ "homepage": "https://homeostate.pages.dev",
13
+ "bugs": {
14
+ "url": "https://github.com/mixedrays/homeostate/issues"
15
+ },
16
+ "keywords": [
17
+ "mobx-state-tree",
18
+ "mobx",
19
+ "crdt",
20
+ "sync",
21
+ "state-management",
22
+ "collaborative"
23
+ ],
6
24
  "main": "./dist/index.js",
7
25
  "types": "./dist/index.d.ts",
8
26
  "exports": {
@@ -13,10 +31,17 @@
13
31
  },
14
32
  "files": [
15
33
  "dist",
34
+ "src",
35
+ "!src/**/__tests__",
36
+ "README.md",
37
+ "LICENSE",
16
38
  "CHANGELOG.md"
17
39
  ],
40
+ "publishConfig": {
41
+ "access": "public"
42
+ },
18
43
  "dependencies": {
19
- "@homeostate/core": "^0.1.1"
44
+ "@homeostate/core": "^0.1.2"
20
45
  },
21
46
  "peerDependencies": {
22
47
  "mobx": "^6.0.0 || ^7.0.0",
package/src/adapter.ts ADDED
@@ -0,0 +1,69 @@
1
+ import {
2
+ applySnapshot,
3
+ getSnapshot,
4
+ onSnapshot,
5
+ type IStateTreeNode,
6
+ type IType,
7
+ } from "mobx-state-tree";
8
+ import type { StoreAdapter, Unsubscribe } from "@homeostate/core";
9
+
10
+ /** A state tree node whose output snapshot is `S` */
11
+ export type SnapshotNode<S> = IStateTreeNode<IType<unknown, S, unknown>>;
12
+
13
+ /**
14
+ * MobX-State-Tree adapter that syncs a tree node through its snapshots.
15
+ * Snapshots go to the backend once per action, and remote changes are applied
16
+ * with `applySnapshot`, so instances with identifiers are reconciled in place.
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * import { types } from 'mobx-state-tree';
21
+ * import * as Y from 'yjs';
22
+ * import { createYjsBackend } from '@homeostate/crdt-yjs';
23
+ *
24
+ * const Counter = types
25
+ * .model({ count: 0 })
26
+ * .actions((self) => ({ increment() { self.count++; } }));
27
+ *
28
+ * const counter = Counter.create();
29
+ * const adapter = createMobxStateTreeAdapter(counter);
30
+ * const engine = createSyncEngine(createYjsBackend(new Y.Doc(), 'shared'), adapter);
31
+ * engine.connect();
32
+ * counter.increment();
33
+ * ```
34
+ */
35
+ export class MobxStateTreeAdapter<S extends object> implements StoreAdapter<S> {
36
+ private node: SnapshotNode<S>;
37
+
38
+ /**
39
+ * Create a MobX-State-Tree adapter
40
+ * @param node - The tree node to sync; usually the root instance
41
+ */
42
+ constructor(node: SnapshotNode<S>) {
43
+ this.node = node;
44
+ }
45
+
46
+ getState(): S {
47
+ return getSnapshot(this.node);
48
+ }
49
+
50
+ setState(state: S): void {
51
+ applySnapshot(this.node, state);
52
+ }
53
+
54
+ subscribe(onStoreChange: () => void): Unsubscribe {
55
+ return onSnapshot(this.node, () => onStoreChange());
56
+ }
57
+ }
58
+
59
+ /**
60
+ * Factory function to create a MobX-State-Tree adapter
61
+ *
62
+ * @param node - The tree node to sync; usually the root instance
63
+ * @returns StoreAdapter instance for the node
64
+ */
65
+ export function createMobxStateTreeAdapter<S extends object>(
66
+ node: SnapshotNode<S>,
67
+ ): StoreAdapter<S> {
68
+ return new MobxStateTreeAdapter(node);
69
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { MobxStateTreeAdapter, createMobxStateTreeAdapter } from "./adapter.js";
2
+ export type { SnapshotNode } from "./adapter.js";
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=adapter.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"adapter.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/adapter.test.ts"],"names":[],"mappings":""}
@@ -1,113 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { getSnapshot, types } from "mobx-state-tree";
3
- import { createMemoryBackend, createSyncEngine, } from "@homeostate/core";
4
- import { createMobxStateTreeAdapter } from "../index.js";
5
- const Todo = types.model("Todo", {
6
- id: types.identifier,
7
- title: types.string,
8
- done: false,
9
- });
10
- const TodoStore = types
11
- .model("TodoStore", {
12
- todos: types.array(Todo),
13
- filter: types.optional(types.string, "all"),
14
- })
15
- .actions((self) => ({
16
- add(id, title) {
17
- self.todos.push({ id, title });
18
- },
19
- toggle(id) {
20
- const todo = self.todos.find((t) => t.id === id);
21
- if (todo)
22
- todo.done = !todo.done;
23
- },
24
- reset(filter) {
25
- self.todos.clear();
26
- self.filter = filter;
27
- },
28
- }));
29
- const createStore = () => TodoStore.create({
30
- todos: [
31
- { id: "1", title: "a" },
32
- { id: "2", title: "b" },
33
- ],
34
- });
35
- const countingWrites = (backend) => {
36
- const writes = [];
37
- const counting = {
38
- ...backend,
39
- write: (next) => {
40
- writes.push(next);
41
- backend.write(next);
42
- },
43
- };
44
- return { backend: counting, writes };
45
- };
46
- describe("MobxStateTreeAdapter", () => {
47
- it("seeds the backend, writes actions, and applies remote changes", () => {
48
- const store = createStore();
49
- const backend = createMemoryBackend();
50
- const engine = createSyncEngine(backend, createMobxStateTreeAdapter(store));
51
- engine.connect();
52
- expect(backend.read()).toEqual({
53
- todos: [
54
- { id: "1", title: "a", done: false },
55
- { id: "2", title: "b", done: false },
56
- ],
57
- filter: "all",
58
- });
59
- store.toggle("2");
60
- expect(backend.read().todos[1].done).toBe(true);
61
- backend.receive({
62
- todos: [{ id: "3", title: "c", done: true }],
63
- filter: "done",
64
- });
65
- expect(getSnapshot(store)).toEqual({
66
- todos: [{ id: "3", title: "c", done: true }],
67
- filter: "done",
68
- });
69
- expect(store.todos[0].done).toBe(true);
70
- });
71
- it("adopts a non-empty backend as the initial state", () => {
72
- const store = createStore();
73
- const backend = createMemoryBackend({ todos: [], filter: "active" });
74
- createSyncEngine(backend, createMobxStateTreeAdapter(store)).connect();
75
- expect(getSnapshot(store)).toEqual({ todos: [], filter: "active" });
76
- });
77
- it("writes once per action even when the action mutates several fields", () => {
78
- const store = createStore();
79
- const { backend, writes } = countingWrites(createMemoryBackend());
80
- createSyncEngine(backend, createMobxStateTreeAdapter(store)).connect();
81
- expect(writes).toHaveLength(1);
82
- store.reset("done");
83
- expect(writes).toHaveLength(2);
84
- expect(backend.read()).toEqual({ todos: [], filter: "done" });
85
- });
86
- it("reconciles remote changes onto existing instances by identifier", () => {
87
- const store = createStore();
88
- const backend = createMemoryBackend();
89
- createSyncEngine(backend, createMobxStateTreeAdapter(store)).connect();
90
- const second = store.todos[1];
91
- backend.receive({
92
- todos: [
93
- { id: "1", title: "a", done: true },
94
- { id: "2", title: "b", done: false },
95
- ],
96
- filter: "all",
97
- });
98
- expect(store.todos[0].done).toBe(true);
99
- expect(store.todos[1]).toBe(second);
100
- });
101
- it("stops both directions after disconnect", () => {
102
- const store = createStore();
103
- const backend = createMemoryBackend();
104
- const engine = createSyncEngine(backend, createMobxStateTreeAdapter(store));
105
- engine.connect();
106
- engine.disconnect();
107
- store.add("3", "c");
108
- expect(backend.read().todos).toHaveLength(2);
109
- backend.receive({ todos: [], filter: "all" });
110
- expect(store.todos).toHaveLength(3);
111
- });
112
- });
113
- //# sourceMappingURL=adapter.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"adapter.test.js","sourceRoot":"","sources":["../../src/__tests__/adapter.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EACL,mBAAmB,EACnB,gBAAgB,GAEjB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,0BAA0B,EAAE,MAAM,aAAa,CAAC;AAEzD,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,MAAM,EAAE;IAC/B,EAAE,EAAE,KAAK,CAAC,UAAU;IACpB,KAAK,EAAE,KAAK,CAAC,MAAM;IACnB,IAAI,EAAE,KAAK;CACZ,CAAC,CAAC;AAEH,MAAM,SAAS,GAAG,KAAK;KACpB,KAAK,CAAC,WAAW,EAAE;IAClB,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC;IACxB,MAAM,EAAE,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,MAAM,E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