Skip to content

Commit 39b8ea8

Browse files
committed
chore: return rootNodes from useReplicatedModels
1 parent fef53f7 commit 39b8ea8

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

vue-model-api/src/useReplicatedModel.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ test("test wrapper backwards compatibility", (done) => {
2424

2525
const branch = {
2626
rootNode,
27+
getRootNodes: () => [rootNode],
2728
addListener: jest.fn(),
2829
removeListener: jest.fn(),
2930
resolveNode: jest.fn(),

vue-model-api/src/useReplicatedModels.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class SuccessfulBranchJS {
2525
this.rootNode.setPropertyValue(toRoleJS("branchId"), branchId);
2626
}
2727

28+
getRootNodes() {
29+
return [this.rootNode];
30+
}
31+
2832
addListener = jest.fn();
2933
}
3034

@@ -58,12 +62,12 @@ test("test branch connects", (done) => {
5862
const { client } = useModelClient("anURL", () =>
5963
Promise.resolve(new SuccessfulClientJS() as unknown as ClientJS),
6064
);
61-
const { rootNode, replicatedModel } = useReplicatedModels(client, [
65+
const { rootNodes, replicatedModel } = useReplicatedModels(client, [
6266
new ReplicatedModelParameters("aRepository", "aBranch", IdSchemeJS.MODELIX),
6367
]);
6468
watchEffect(() => {
65-
if (rootNode.value !== null && replicatedModel.value !== null) {
66-
expect(rootNode.value.getPropertyValue(toRoleJS("branchId"))).toBe(
69+
if (rootNodes.value.length > 0 && replicatedModel.value !== null) {
70+
expect(rootNodes.value[0].getPropertyValue(toRoleJS("branchId"))).toBe(
6771
"aBranch",
6872
);
6973
done();

vue-model-api/src/useReplicatedModels.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,38 @@ function isDefined<T>(value: T | null | undefined): value is T {
1919
}
2020

2121
/**
22-
* Creates a replicated model for a given repository and branch.
22+
* Creates replicated models for the given repositories and branches.
2323
* A replicated model exposes a branch that can be used to read and write model data.
2424
* The written model data is automatically synced to the model server.
25-
* Changed from the model server are automatically synced to the branch in the replicated model
25+
* Changes from the model server are automatically synced to the branch in the replicated model.
2626
*
27-
* Also creates root node that uses Vues reactivity and can be used in Vue like a reactive object.
27+
* Also creates root nodes that use Vue's reactivity and can be used in Vue like reactive objects.
2828
* Changes to model data trigger recalculation of computed properties or re-rendering of components using that data.
2929
*
30-
* Calling the returned dispose function stops syncing the root node to the underlying branch on the server.
30+
* Calling the returned dispose function stops syncing the root nodes to the underlying branches on the server.
3131
*
3232
* @param client - Reactive reference of a client to a model server.
33-
* @param repositoryId - Reactive reference of a repositoryId on the model server.
34-
* @param branchId - Reactive reference of a branchId in the repository of the model server.
33+
* @param models - Reactive reference to an array of ReplicatedModelParameters.
3534
*
3635
* @returns {Object} values Wrapper around different returned values.
37-
* @returns {Ref<ReplicatedModelJS | null>} values.rootNode Reactive reference to the replicated model for the specified branch.
38-
* @returns {Ref<INodeJS | null>} values.rootNode Reactive reference to the root node with Vue.js reactivity for the specified branch.
39-
* @returns {() => void} values.dispose A function to manually dispose the root node.
36+
* @returns {Ref<ReplicatedModelJS | null>} values.replicatedModel Reactive reference to the replicated model for the specified branches.
37+
* @returns {Ref<INodeJS[]>} values.rootNodes Reactive reference to an array of root nodes with Vue.js reactivity for the specified branches.
38+
* @returns {() => void} values.dispose A function to manually dispose the root nodes.
4039
* @returns {Ref<unknown>} values.error Reactive reference to a connection error.
4140
*/
4241
export function useReplicatedModels(
4342
client: MaybeRefOrGetter<ClientJS | null | undefined>,
4443
models: MaybeRefOrGetter<ReplicatedModelParameters[] | null | undefined>,
4544
): {
4645
replicatedModel: Ref<ReplicatedModelJS | null>;
47-
rootNode: Ref<INodeJS | null>;
46+
rootNodes: Ref<INodeJS[]>;
4847
dispose: () => void;
4948
error: Ref<unknown>;
5049
} {
5150
// Use `replicatedModel` to access the replicated model without tracking overhead of Vue.js.
5251
let replicatedModel: ReplicatedModelJS | null = null;
5352
const replicatedModelRef: Ref<ReplicatedModelJS | null> = shallowRef(null);
54-
const rootNodeRef: Ref<INodeJS | null> = shallowRef(null);
53+
const rootNodesRef: Ref<INodeJS[]> = shallowRef([]);
5554
const errorRef: Ref<unknown> = shallowRef(null);
5655

5756
const dispose = () => {
@@ -61,7 +60,7 @@ export function useReplicatedModels(
6160
replicatedModel.dispose();
6261
}
6362
replicatedModelRef.value = null;
64-
rootNodeRef.value = null;
63+
rootNodesRef.value = [];
6564
errorRef.value = null;
6665
};
6766

@@ -97,10 +96,12 @@ export function useReplicatedModels(
9796
}
9897
handleChange(change, cache);
9998
});
100-
const unreactiveRootNode = branch.rootNode;
101-
const reactiveRootNode = toReactiveINodeJS(unreactiveRootNode, cache);
99+
const unreactiveRootNodes = branch.getRootNodes();
100+
const reactiveRootNodes = unreactiveRootNodes.map((node) =>
101+
toReactiveINodeJS(node, cache),
102+
);
102103
replicatedModelRef.value = replicatedModel;
103-
rootNodeRef.value = reactiveRootNode;
104+
rootNodesRef.value = reactiveRootNodes;
104105
} else {
105106
connectedReplicatedModel.dispose();
106107
}
@@ -114,7 +115,7 @@ export function useReplicatedModels(
114115

115116
return {
116117
replicatedModel: replicatedModelRef,
117-
rootNode: rootNodeRef,
118+
rootNodes: rootNodesRef,
118119
dispose,
119120
error: errorRef,
120121
};
@@ -172,5 +173,15 @@ export function useReplicatedModel(
172173
];
173174
});
174175

175-
return useReplicatedModels(client, models);
176+
const result = useReplicatedModels(client, models);
177+
178+
// Extract the single root node from the array for backward compatibility
179+
const rootNode = computed(() => result.rootNodes.value[0] ?? null);
180+
181+
return {
182+
replicatedModel: result.replicatedModel,
183+
rootNode: rootNode,
184+
dispose: result.dispose,
185+
error: result.error,
186+
};
176187
}

0 commit comments

Comments
 (0)