Skip to content

Commit ae6032c

Browse files
author
Oleksandr Dzhychko
committed
feat(vue-model-api): expose replicated model
The replicated model can be used to subscribe to changes on the branch or get information about the current version.
1 parent 408b23a commit ae6032c

File tree

4 files changed

+33
-20
lines changed

4 files changed

+33
-20
lines changed

vue-model-api/package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vue-model-api/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export { useModelsFromJson } from "./useModelsFromJson";
22
export { useModelClient } from "./useModelClient";
3-
export { useRootNode } from "./useRootNode";
3+
export { useReplicatedModel } from "./useReplicatedModel";

vue-model-api/src/useRootNode.test.ts renamed to vue-model-api/src/useReplicatedModel.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { org } from "@modelix/model-client";
22
import { INodeJS } from "@modelix/ts-model-api";
33
import { watchEffect } from "vue";
44
import { useModelClient } from "./useModelClient";
5-
import { useRootNode } from "./useRootNode";
5+
import { useReplicatedModel } from "./useReplicatedModel";
66

77
type BranchJS = org.modelix.model.client2.BranchJS;
88
type ReplicatedModelJS = org.modelix.model.client2.ReplicatedModelJS;
@@ -53,11 +53,13 @@ test("test branch connects", (done) => {
5353
const { client } = useModelClient("anURL", () =>
5454
Promise.resolve(new SuccessfulClientJS() as unknown as ClientJS),
5555
);
56-
57-
const { rootNode } = useRootNode(client, "aRepository", "aBranch");
58-
56+
const { rootNode, replicatedModel } = useReplicatedModel(
57+
client,
58+
"aRepository",
59+
"aBranch",
60+
);
5961
watchEffect(() => {
60-
if (rootNode.value !== null) {
62+
if (rootNode.value !== null && replicatedModel.value !== null) {
6163
expect(rootNode.value.getPropertyValue("branchId")).toBe("aBranch");
6264
done();
6365
}
@@ -78,7 +80,7 @@ test("test branch connection error is exposed", (done) => {
7880
Promise.resolve(new FailingClientJS() as unknown as ClientJS),
7981
);
8082

81-
const { error } = useRootNode(client, "aRepository", "aBranch");
83+
const { error } = useReplicatedModel(client, "aRepository", "aBranch");
8284

8385
watchEffect(() => {
8486
if (error.value !== null) {

vue-model-api/src/useRootNode.ts renamed to vue-model-api/src/useReplicatedModel.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,51 @@ type ReplicatedModelJS = org.modelix.model.client2.ReplicatedModelJS;
1111
type ChangeJS = org.modelix.model.client2.ChangeJS;
1212

1313
/**
14-
* Creates a reactive root node from a client for a given repository and branch.
14+
* Creates a replicated model for a given repository and branch.
15+
* A replicated model exposes a branch that can be used to read and write model data.
16+
* The written model data is automatically synced to the model server.
17+
* Changed from the model server are automatically synced to the branch in the replicated model
1518
*
16-
* The returned root node uses Vues reactivity and can be used in Vue like an reactive object.
17-
* Changes to the returned node or its descendants are synced to the branch on the model server.
19+
* Also creates root node that uses Vues reactivity and can be used in Vue like a reactive object.
20+
* Changes to model data trigger recalculation of computed properties or re-rendering of components using that data.
1821
*
19-
* Calling the returned dispose function stops syncing the root node to the underlying branch on the serever.
22+
* Calling the returned dispose function stops syncing the root node to the underlying branch on the server.
2023
*
2124
* @experimental This feature is expected to be finalized with https://issues.modelix.org/issue/MODELIX-500.
2225
*
2326
* @param client - Reactive reference of a client to a model server.
2427
* @param repositoryId - Reactive reference of a repositoryId on the model server.
2528
* @param branchId - Reactive reference of a branchId in the repository of the model server.
2629
*
27-
* @returns {Object} values Wrapper around diffrent returned values.
28-
* @returns {Ref<INodeJS | null>} values.rootNode Reactive reference to a reactive root node.
30+
* @returns {Object} values Wrapper around different returned values.
31+
* @returns {Ref<ReplicatedModelJS | null>} values.rootNode Reactive reference to the replicated model for the specified branch.
32+
* @returns {Ref<INodeJS | null>} values.rootNode Reactive reference to the root node with Vue.js reactivity for the specified branch.
2933
* @returns {() => void} values.dispose A function to manually dispose the root node.
3034
* @returns {Ref<unknown>} values.error Reactive reference to a connection error.
3135
*/
32-
export function useRootNode(
36+
export function useReplicatedModel(
3337
client: MaybeRefOrGetter<ClientJS | null>,
3438
repositoryId: MaybeRefOrGetter<string | null>,
3539
branchId: MaybeRefOrGetter<string | null>,
3640
): {
41+
replicatedModel: Ref<ReplicatedModelJS | null>;
3742
rootNode: Ref<INodeJS | null>;
3843
dispose: () => void;
3944
error: Ref<unknown>;
4045
} {
46+
// Use `replicatedModel` to access the replicated model without tracking overhead of Vue.js.
4147
let replicatedModel: ReplicatedModelJS | null = null;
48+
const replicatedModelRef: Ref<ReplicatedModelJS | null> = shallowRef(null);
4249
const rootNodeRef: Ref<INodeJS | null> = shallowRef(null);
4350
const errorRef: Ref<unknown> = shallowRef(null);
4451

4552
const dispose = () => {
53+
// Using `replicatedModelRef.value` here would create a circular dependency.
54+
// `toRaw` does not work on `Ref<>`.
4655
if (replicatedModel !== null) {
4756
replicatedModel.dispose();
4857
}
49-
replicatedModel = null;
58+
replicatedModelRef.value = null;
5059
rootNodeRef.value = null;
5160
errorRef.value = null;
5261
};
@@ -86,6 +95,7 @@ export function useRootNode(
8695
});
8796
const unreactiveRootNode = branch.rootNode;
8897
const reactiveRootNode = toReactiveINodeJS(unreactiveRootNode, cache);
98+
replicatedModelRef.value = replicatedModel;
8999
rootNodeRef.value = reactiveRootNode;
90100
} else {
91101
connectedReplicatedModel.dispose();
@@ -99,6 +109,7 @@ export function useRootNode(
99109
);
100110

101111
return {
112+
replicatedModel: replicatedModelRef,
102113
rootNode: rootNodeRef,
103114
dispose,
104115
error: errorRef,

0 commit comments

Comments
 (0)