@homeostate/store-mobx-state-tree 0.1.1 → 0.2.0
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 +22 -0
- package/LICENSE +1 -1
- package/README.md +0 -3
- package/dist/adapter.d.ts +5 -19
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +21 -24
- package/dist/adapter.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +28 -2
- package/src/adapter.ts +66 -0
- package/src/index.ts +2 -0
- package/dist/__tests__/adapter.test.d.ts +0 -2
- package/dist/__tests__/adapter.test.d.ts.map +0 -1
- package/dist/__tests__/adapter.test.js +0 -113
- package/dist/__tests__/adapter.test.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# @homeostate/store-mobx-state-tree
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 81d1d18: Stop exporting the adapter classes (`JotaiAdapter`, `MobxAdapter`, `MobxStateTreeAdapter`, `ReduxAdapter`, `TanStackStoreAdapter`, `ValtioAdapter`, `ZustandAdapter`); create adapters with the `create*Adapter` factories, which now carry the adapter docs and examples.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- 33c7b14: Mark the package side-effect free, so bundlers can drop modules that are not imported.
|
|
12
|
+
- Updated dependencies [a643bbe]
|
|
13
|
+
- Updated dependencies [0bbf653]
|
|
14
|
+
- Updated dependencies [fb76f77]
|
|
15
|
+
- @homeostate/core@0.2.0
|
|
16
|
+
|
|
17
|
+
## 0.1.2
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- 30abf27: Remove draft release warnings, update docs links
|
|
22
|
+
- Updated dependencies [30abf27]
|
|
23
|
+
- @homeostate/core@0.1.2
|
|
24
|
+
|
|
3
25
|
## 0.1.1
|
|
4
26
|
|
|
5
27
|
### 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/dist/adapter.d.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { type IStateTreeNode, type IType } from "mobx-state-tree";
|
|
2
|
-
import type { StoreAdapter
|
|
2
|
+
import type { StoreAdapter } from "@homeostate/core";
|
|
3
3
|
/** A state tree node whose output snapshot is `S` */
|
|
4
4
|
export type SnapshotNode<S> = IStateTreeNode<IType<unknown, S, unknown>>;
|
|
5
5
|
/**
|
|
6
|
-
* MobX-State-Tree adapter that syncs a tree node through its snapshots.
|
|
6
|
+
* Creates a MobX-State-Tree adapter that syncs a tree node through its snapshots.
|
|
7
7
|
* Snapshots go to the backend once per action, and remote changes are applied
|
|
8
8
|
* with `applySnapshot`, so instances with identifiers are reconciled in place.
|
|
9
9
|
*
|
|
10
|
+
* @param node - The tree node to sync; usually the root instance
|
|
11
|
+
* @returns StoreAdapter instance for the node
|
|
12
|
+
*
|
|
10
13
|
* @example
|
|
11
14
|
* ```typescript
|
|
12
15
|
* import { types } from 'mobx-state-tree';
|
|
@@ -24,22 +27,5 @@ export type SnapshotNode<S> = IStateTreeNode<IType<unknown, S, unknown>>;
|
|
|
24
27
|
* counter.increment();
|
|
25
28
|
* ```
|
|
26
29
|
*/
|
|
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
30
|
export declare function createMobxStateTreeAdapter<S extends object>(node: SnapshotNode<S>): StoreAdapter<S>;
|
|
45
31
|
//# sourceMappingURL=adapter.d.ts.map
|
package/dist/adapter.d.ts.map
CHANGED
|
@@ -1 +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,
|
|
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,MAAM,kBAAkB,CAAC;AAElE,qDAAqD;AACrD,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,cAAc,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC;AA0BzE;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,0BAA0B,CAAC,CAAC,SAAS,MAAM,EACzD,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,GACpB,YAAY,CAAC,CAAC,CAAC,CAEjB"}
|
package/dist/adapter.js
CHANGED
|
@@ -1,27 +1,5 @@
|
|
|
1
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 {
|
|
2
|
+
class MobxStateTreeAdapter {
|
|
25
3
|
/**
|
|
26
4
|
* Create a MobX-State-Tree adapter
|
|
27
5
|
* @param node - The tree node to sync; usually the root instance
|
|
@@ -46,10 +24,29 @@ export class MobxStateTreeAdapter {
|
|
|
46
24
|
}
|
|
47
25
|
}
|
|
48
26
|
/**
|
|
49
|
-
*
|
|
27
|
+
* Creates a MobX-State-Tree adapter that syncs a tree node through its snapshots.
|
|
28
|
+
* Snapshots go to the backend once per action, and remote changes are applied
|
|
29
|
+
* with `applySnapshot`, so instances with identifiers are reconciled in place.
|
|
50
30
|
*
|
|
51
31
|
* @param node - The tree node to sync; usually the root instance
|
|
52
32
|
* @returns StoreAdapter instance for the node
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* import { types } from 'mobx-state-tree';
|
|
37
|
+
* import * as Y from 'yjs';
|
|
38
|
+
* import { createYjsBackend } from '@homeostate/crdt-yjs';
|
|
39
|
+
*
|
|
40
|
+
* const Counter = types
|
|
41
|
+
* .model({ count: 0 })
|
|
42
|
+
* .actions((self) => ({ increment() { self.count++; } }));
|
|
43
|
+
*
|
|
44
|
+
* const counter = Counter.create();
|
|
45
|
+
* const adapter = createMobxStateTreeAdapter(counter);
|
|
46
|
+
* const engine = createSyncEngine(createYjsBackend(new Y.Doc(), 'shared'), adapter);
|
|
47
|
+
* engine.connect();
|
|
48
|
+
* counter.increment();
|
|
49
|
+
* ```
|
|
53
50
|
*/
|
|
54
51
|
export function createMobxStateTreeAdapter(node) {
|
|
55
52
|
return new MobxStateTreeAdapter(node);
|
package/dist/adapter.js.map
CHANGED
|
@@ -1 +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
|
|
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,MAAM,oBAAoB;IAGxB;;;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;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,0BAA0B,CACxC,IAAqB;IAErB,OAAO,IAAI,oBAAoB,CAAC,IAAI,CAAC,CAAC;AACxC,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC;AAC1D,YAAY,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { createMobxStateTreeAdapter } from "./adapter.js";
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,8 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@homeostate/store-mobx-state-tree",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
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": {
|
|
@@ -11,12 +29,20 @@
|
|
|
11
29
|
"default": "./dist/index.js"
|
|
12
30
|
}
|
|
13
31
|
},
|
|
32
|
+
"sideEffects": false,
|
|
14
33
|
"files": [
|
|
15
34
|
"dist",
|
|
35
|
+
"src",
|
|
36
|
+
"!src/**/__tests__",
|
|
37
|
+
"README.md",
|
|
38
|
+
"LICENSE",
|
|
16
39
|
"CHANGELOG.md"
|
|
17
40
|
],
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
18
44
|
"dependencies": {
|
|
19
|
-
"@homeostate/core": "^0.
|
|
45
|
+
"@homeostate/core": "^0.2.0"
|
|
20
46
|
},
|
|
21
47
|
"peerDependencies": {
|
|
22
48
|
"mobx": "^6.0.0 || ^7.0.0",
|
package/src/adapter.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
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
|
+
class MobxStateTreeAdapter<S extends object> implements StoreAdapter<S> {
|
|
14
|
+
private node: SnapshotNode<S>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Create a MobX-State-Tree adapter
|
|
18
|
+
* @param node - The tree node to sync; usually the root instance
|
|
19
|
+
*/
|
|
20
|
+
constructor(node: SnapshotNode<S>) {
|
|
21
|
+
this.node = node;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
getState(): S {
|
|
25
|
+
return getSnapshot(this.node);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
setState(state: S): void {
|
|
29
|
+
applySnapshot(this.node, state);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
subscribe(onStoreChange: () => void): Unsubscribe {
|
|
33
|
+
return onSnapshot(this.node, () => onStoreChange());
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Creates a MobX-State-Tree adapter that syncs a tree node through its snapshots.
|
|
39
|
+
* Snapshots go to the backend once per action, and remote changes are applied
|
|
40
|
+
* with `applySnapshot`, so instances with identifiers are reconciled in place.
|
|
41
|
+
*
|
|
42
|
+
* @param node - The tree node to sync; usually the root instance
|
|
43
|
+
* @returns StoreAdapter instance for the node
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* import { types } from 'mobx-state-tree';
|
|
48
|
+
* import * as Y from 'yjs';
|
|
49
|
+
* import { createYjsBackend } from '@homeostate/crdt-yjs';
|
|
50
|
+
*
|
|
51
|
+
* const Counter = types
|
|
52
|
+
* .model({ count: 0 })
|
|
53
|
+
* .actions((self) => ({ increment() { self.count++; } }));
|
|
54
|
+
*
|
|
55
|
+
* const counter = Counter.create();
|
|
56
|
+
* const adapter = createMobxStateTreeAdapter(counter);
|
|
57
|
+
* const engine = createSyncEngine(createYjsBackend(new Y.Doc(), 'shared'), adapter);
|
|
58
|
+
* engine.connect();
|
|
59
|
+
* counter.increment();
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export function createMobxStateTreeAdapter<S extends object>(
|
|
63
|
+
node: SnapshotNode<S>,
|
|
64
|
+
): StoreAdapter<S> {
|
|
65
|
+
return new MobxStateTreeAdapter(node);
|
|
66
|
+
}
|
package/src/index.ts
ADDED
|
@@ -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,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,KAAK,EAAE,CAAC,CAAC;QACjD,IAAI,IAAI;YAAE,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,IAAI,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