@@ -11,42 +11,51 @@ type ReplicatedModelJS = org.modelix.model.client2.ReplicatedModelJS;
11
11
type ChangeJS = org . modelix . model . client2 . ChangeJS ;
12
12
13
13
/**
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
15
18
*
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 .
18
21
*
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 .
20
23
*
21
24
* @experimental This feature is expected to be finalized with https://issues.modelix.org/issue/MODELIX-500.
22
25
*
23
26
* @param client - Reactive reference of a client to a model server.
24
27
* @param repositoryId - Reactive reference of a repositoryId on the model server.
25
28
* @param branchId - Reactive reference of a branchId in the repository of the model server.
26
29
*
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.
29
33
* @returns {() => void } values.dispose A function to manually dispose the root node.
30
34
* @returns {Ref<unknown> } values.error Reactive reference to a connection error.
31
35
*/
32
- export function useRootNode (
36
+ export function useReplicatedModel (
33
37
client : MaybeRefOrGetter < ClientJS | null > ,
34
38
repositoryId : MaybeRefOrGetter < string | null > ,
35
39
branchId : MaybeRefOrGetter < string | null > ,
36
40
) : {
41
+ replicatedModel : Ref < ReplicatedModelJS | null > ;
37
42
rootNode : Ref < INodeJS | null > ;
38
43
dispose : ( ) => void ;
39
44
error : Ref < unknown > ;
40
45
} {
46
+ // Use `replicatedModel` to access the replicated model without tracking overhead of Vue.js.
41
47
let replicatedModel : ReplicatedModelJS | null = null ;
48
+ const replicatedModelRef : Ref < ReplicatedModelJS | null > = shallowRef ( null ) ;
42
49
const rootNodeRef : Ref < INodeJS | null > = shallowRef ( null ) ;
43
50
const errorRef : Ref < unknown > = shallowRef ( null ) ;
44
51
45
52
const dispose = ( ) => {
53
+ // Using `replicatedModelRef.value` here would create a circular dependency.
54
+ // `toRaw` does not work on `Ref<>`.
46
55
if ( replicatedModel !== null ) {
47
56
replicatedModel . dispose ( ) ;
48
57
}
49
- replicatedModel = null ;
58
+ replicatedModelRef . value = null ;
50
59
rootNodeRef . value = null ;
51
60
errorRef . value = null ;
52
61
} ;
@@ -86,6 +95,7 @@ export function useRootNode(
86
95
} ) ;
87
96
const unreactiveRootNode = branch . rootNode ;
88
97
const reactiveRootNode = toReactiveINodeJS ( unreactiveRootNode , cache ) ;
98
+ replicatedModelRef . value = replicatedModel ;
89
99
rootNodeRef . value = reactiveRootNode ;
90
100
} else {
91
101
connectedReplicatedModel . dispose ( ) ;
@@ -99,6 +109,7 @@ export function useRootNode(
99
109
) ;
100
110
101
111
return {
112
+ replicatedModel : replicatedModelRef ,
102
113
rootNode : rootNodeRef ,
103
114
dispose,
104
115
error : errorRef ,
0 commit comments