@homeostate/store-mobx-state-tree 0.1.1
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 +9 -0
- package/LICENSE +21 -0
- package/README.md +58 -0
- package/dist/__tests__/adapter.test.d.ts +2 -0
- package/dist/__tests__/adapter.test.d.ts.map +1 -0
- package/dist/__tests__/adapter.test.js +113 -0
- package/dist/__tests__/adapter.test.js.map +1 -0
- package/dist/adapter.d.ts +45 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +57 -0
- package/dist/adapter.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +32 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Aliaksandr Mialeshka
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# @homeostate/store-mobx-state-tree
|
|
2
|
+
|
|
3
|
+
> **Early release.** The API is still being designed and may change in any `0.x` minor
|
|
4
|
+
> version.
|
|
5
|
+
|
|
6
|
+
[MobX-State-Tree](https://mobx-state-tree.js.org) store adapter for
|
|
7
|
+
[`@homeostate/core`](https://github.com/mixedrays/homeostate/tree/main/packages/core).
|
|
8
|
+
It keeps a state tree node in sync with a CRDT backend such as
|
|
9
|
+
[`@homeostate/crdt-yjs`](https://github.com/mixedrays/homeostate/tree/main/packages/crdt-yjs),
|
|
10
|
+
[`@homeostate/crdt-loro`](https://github.com/mixedrays/homeostate/tree/main/packages/crdt-loro), or
|
|
11
|
+
[`@homeostate/crdt-automerge`](https://github.com/mixedrays/homeostate/tree/main/packages/crdt-automerge).
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @homeostate/core @homeostate/store-mobx-state-tree mobx mobx-state-tree @homeostate/crdt-yjs yjs
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
`mobx` 6 or 7 and `mobx-state-tree` 7 or 8 are peer dependencies.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import * as Y from "yjs";
|
|
25
|
+
import { types } from "mobx-state-tree";
|
|
26
|
+
import { createSyncEngine } from "@homeostate/core";
|
|
27
|
+
import { createYjsBackend } from "@homeostate/crdt-yjs";
|
|
28
|
+
import { createMobxStateTreeAdapter } from "@homeostate/store-mobx-state-tree";
|
|
29
|
+
|
|
30
|
+
const Todo = types.model({
|
|
31
|
+
id: types.identifier,
|
|
32
|
+
title: types.string,
|
|
33
|
+
done: false,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const TodoStore = types.model({ todos: types.array(Todo) }).actions((self) => ({
|
|
37
|
+
add(title: string) {
|
|
38
|
+
self.todos.push({ id: crypto.randomUUID(), title });
|
|
39
|
+
},
|
|
40
|
+
}));
|
|
41
|
+
|
|
42
|
+
const store = TodoStore.create();
|
|
43
|
+
|
|
44
|
+
const engine = createSyncEngine(
|
|
45
|
+
createYjsBackend(new Y.Doc(), "shared"),
|
|
46
|
+
createMobxStateTreeAdapter(store),
|
|
47
|
+
);
|
|
48
|
+
engine.connect();
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
The adapter syncs the node's snapshot: local changes go to the backend once per action, and
|
|
52
|
+
remote changes are applied with `applySnapshot`, so instances with an identifier are
|
|
53
|
+
reconciled in place instead of being recreated. Pass the root instance, or any subtree node
|
|
54
|
+
to sync only that part.
|
|
55
|
+
|
|
56
|
+
## License
|
|
57
|
+
|
|
58
|
+
MIT — see [LICENSE](https://github.com/mixedrays/homeostate/blob/main/LICENSE).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/adapter.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,113 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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,EAAE,KAAK,CAAC;CAC5C,CAAC;KACD,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IAClB,GAAG,CAAC,EAAU,EAAE,KAAa;QAC3B,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;IACjC,CAAC;IACD,MAAM,CAAC,EAAU;QACf,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,K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AAE,KAAK,EAAE;aACrC;YACD,MAAM,EAAE,KAAK;SACd,CAAC,CAAC;QAEH,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACvC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACtC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,wCAAwC,EAAE,GAAG,EAAE;QAChD,MAAM,KAAK,GAAG,WAAW,EAAE,CAAC;QAC5B,MAAM,OAAO,GAAG,mBAAmB,EAAE,CAAC;QACtC,MAAM,MAAM,GAAG,gBAAgB,CAAC,OAAO,EAAE,0BAA0B,CAAC,KAAK,CAAC,CAAC,CAAC;QAC5E,MAAM,CAAC,OAAO,EAAE,CAAC;QAEjB,MAAM,CAAC,UAAU,EAAE,CAAC;QACpB,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QACpB,MAAM,CAAE,OAAO,CAAC,IAAI,EAA2B,CAAC,KAAK,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;QAEvE,OAAO,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;QAC9C,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;IACtC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type IStateTreeNode, type IType } from "mobx-state-tree";
|
|
2
|
+
import type { StoreAdapter, Unsubscribe } from "@homeostate/core";
|
|
3
|
+
/** A state tree node whose output snapshot is `S` */
|
|
4
|
+
export type SnapshotNode<S> = IStateTreeNode<IType<unknown, S, unknown>>;
|
|
5
|
+
/**
|
|
6
|
+
* MobX-State-Tree adapter that syncs a tree node through its snapshots.
|
|
7
|
+
* Snapshots go to the backend once per action, and remote changes are applied
|
|
8
|
+
* with `applySnapshot`, so instances with identifiers are reconciled in place.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { types } from 'mobx-state-tree';
|
|
13
|
+
* import * as Y from 'yjs';
|
|
14
|
+
* import { createYjsBackend } from '@homeostate/crdt-yjs';
|
|
15
|
+
*
|
|
16
|
+
* const Counter = types
|
|
17
|
+
* .model({ count: 0 })
|
|
18
|
+
* .actions((self) => ({ increment() { self.count++; } }));
|
|
19
|
+
*
|
|
20
|
+
* const counter = Counter.create();
|
|
21
|
+
* const adapter = createMobxStateTreeAdapter(counter);
|
|
22
|
+
* const engine = createSyncEngine(createYjsBackend(new Y.Doc(), 'shared'), adapter);
|
|
23
|
+
* engine.connect();
|
|
24
|
+
* counter.increment();
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export declare class MobxStateTreeAdapter<S extends object> implements StoreAdapter<S> {
|
|
28
|
+
private node;
|
|
29
|
+
/**
|
|
30
|
+
* Create a MobX-State-Tree adapter
|
|
31
|
+
* @param node - The tree node to sync; usually the root instance
|
|
32
|
+
*/
|
|
33
|
+
constructor(node: SnapshotNode<S>);
|
|
34
|
+
getState(): S;
|
|
35
|
+
setState(state: S): void;
|
|
36
|
+
subscribe(onStoreChange: () => void): Unsubscribe;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Factory function to create a MobX-State-Tree adapter
|
|
40
|
+
*
|
|
41
|
+
* @param node - The tree node to sync; usually the root instance
|
|
42
|
+
* @returns StoreAdapter instance for the node
|
|
43
|
+
*/
|
|
44
|
+
export declare function createMobxStateTreeAdapter<S extends object>(node: SnapshotNode<S>): StoreAdapter<S>;
|
|
45
|
+
//# sourceMappingURL=adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,cAAc,EACnB,KAAK,KAAK,EACX,MAAM,iBAAiB,CAAC;AACzB,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAElE,qDAAqD;AACrD,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,cAAc,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC;AAEzE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,oBAAoB,CAAC,CAAC,SAAS,MAAM,CAAE,YAAW,YAAY,CAAC,CAAC,CAAC;IAC5E,OAAO,CAAC,IAAI,CAAkB;IAE9B;;;OAGG;gBACS,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC;IAIjC,QAAQ,IAAI,CAAC;IAIb,QAAQ,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI;IAIxB,SAAS,CAAC,aAAa,EAAE,MAAM,IAAI,GAAG,WAAW;CAGlD;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,CAAC,SAAS,MAAM,EACzD,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,GACpB,YAAY,CAAC,CAAC,CAAC,CAEjB"}
|
package/dist/adapter.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { applySnapshot, getSnapshot, onSnapshot, } from "mobx-state-tree";
|
|
2
|
+
/**
|
|
3
|
+
* MobX-State-Tree adapter that syncs a tree node through its snapshots.
|
|
4
|
+
* Snapshots go to the backend once per action, and remote changes are applied
|
|
5
|
+
* with `applySnapshot`, so instances with identifiers are reconciled in place.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { types } from 'mobx-state-tree';
|
|
10
|
+
* import * as Y from 'yjs';
|
|
11
|
+
* import { createYjsBackend } from '@homeostate/crdt-yjs';
|
|
12
|
+
*
|
|
13
|
+
* const Counter = types
|
|
14
|
+
* .model({ count: 0 })
|
|
15
|
+
* .actions((self) => ({ increment() { self.count++; } }));
|
|
16
|
+
*
|
|
17
|
+
* const counter = Counter.create();
|
|
18
|
+
* const adapter = createMobxStateTreeAdapter(counter);
|
|
19
|
+
* const engine = createSyncEngine(createYjsBackend(new Y.Doc(), 'shared'), adapter);
|
|
20
|
+
* engine.connect();
|
|
21
|
+
* counter.increment();
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export class MobxStateTreeAdapter {
|
|
25
|
+
/**
|
|
26
|
+
* Create a MobX-State-Tree adapter
|
|
27
|
+
* @param node - The tree node to sync; usually the root instance
|
|
28
|
+
*/
|
|
29
|
+
constructor(node) {
|
|
30
|
+
Object.defineProperty(this, "node", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
configurable: true,
|
|
33
|
+
writable: true,
|
|
34
|
+
value: void 0
|
|
35
|
+
});
|
|
36
|
+
this.node = node;
|
|
37
|
+
}
|
|
38
|
+
getState() {
|
|
39
|
+
return getSnapshot(this.node);
|
|
40
|
+
}
|
|
41
|
+
setState(state) {
|
|
42
|
+
applySnapshot(this.node, state);
|
|
43
|
+
}
|
|
44
|
+
subscribe(onStoreChange) {
|
|
45
|
+
return onSnapshot(this.node, () => onStoreChange());
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Factory function to create a MobX-State-Tree adapter
|
|
50
|
+
*
|
|
51
|
+
* @param node - The tree node to sync; usually the root instance
|
|
52
|
+
* @returns StoreAdapter instance for the node
|
|
53
|
+
*/
|
|
54
|
+
export function createMobxStateTreeAdapter(node) {
|
|
55
|
+
return new MobxStateTreeAdapter(node);
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.js","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,WAAW,EACX,UAAU,GAGX,MAAM,iBAAiB,CAAC;AAMzB;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,OAAO,oBAAoB;IAG/B;;;OAGG;IACH,YAAY,IAAqB;QANzB;;;;;WAAsB;QAO5B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAED,QAAQ;QACN,OAAO,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAChC,CAAC;IAED,QAAQ,CAAC,KAAQ;QACf,aAAa,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAClC,CAAC;IAED,SAAS,CAAC,aAAyB;QACjC,OAAO,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,CAAC,aAAa,EAAE,CAAC,CAAC;IACtD,CAAC;CACF;AAED;;;;;GAKG;AACH,MAAM,UAAU,0BAA0B,CACxC,IAAqB;IAErB,OAAO,IAAI,oBAAoB,CAAC,IAAI,CAAC,CAAC;AACxC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC;AAChF,YAAY,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@homeostate/store-mobx-state-tree",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "MobX-State-Tree adapter for @homeostate/core",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"CHANGELOG.md"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@homeostate/core": "^0.1.1"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"mobx": "^6.0.0 || ^7.0.0",
|
|
23
|
+
"mobx-state-tree": "^7.0.0 || ^8.0.0"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"mobx": "^7.0.3",
|
|
27
|
+
"mobx-state-tree": "^8.0.0"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsc -b"
|
|
31
|
+
}
|
|
32
|
+
}
|