14
14
* limitations under the License.
15
15
*/
16
16
17
- @file:OptIn(UnstableModelixFeature ::class )
17
+ @file:OptIn(UnstableModelixFeature ::class , UnstableModelixFeature :: class )
18
18
19
19
package org.modelix.model.client2
20
20
@@ -25,13 +25,8 @@ import kotlinx.coroutines.GlobalScope
25
25
import kotlinx.coroutines.promise
26
26
import org.modelix.kotlin.utils.UnstableModelixFeature
27
27
import org.modelix.model.ModelFacade
28
- import org.modelix.model.api.IBranch
29
- import org.modelix.model.api.IBranchListener
30
28
import org.modelix.model.api.INode
31
- import org.modelix.model.api.ITree
32
- import org.modelix.model.api.ITreeChangeVisitor
33
29
import org.modelix.model.api.JSNodeConverter
34
- import org.modelix.model.api.PNodeAdapter
35
30
import org.modelix.model.data.ModelData
36
31
import org.modelix.model.lazy.RepositoryId
37
32
import org.modelix.model.withAutoTransactions
@@ -43,11 +38,8 @@ import kotlin.js.Promise
43
38
intendedFinalization = " The client is intended to be finalized when the overarching task is finished." ,
44
39
)
45
40
@JsExport
46
- fun loadModelsFromJson (
47
- json : Array <String >,
48
- changeCallback : (ChangeJS ) -> Unit ,
49
- ): INodeJS {
50
- val branch = loadModelsFromJsonAsBranch(json, changeCallback)
41
+ fun loadModelsFromJson (json : Array <String >): INodeJS {
42
+ val branch = loadModelsFromJsonAsBranch(json)
51
43
return branch.rootNode
52
44
}
53
45
@@ -56,13 +48,9 @@ fun loadModelsFromJson(
56
48
intendedFinalization = " The client is intended to be finalized when the overarching task is finished." ,
57
49
)
58
50
@JsExport
59
- fun loadModelsFromJsonAsBranch (
60
- json : Array <String >,
61
- changeCallback : (ChangeJS ) -> Unit ,
62
- ): BranchJS {
51
+ fun loadModelsFromJsonAsBranch (json : Array <String >): BranchJS {
63
52
val branch = ModelFacade .toLocalBranch(ModelFacade .newLocalTree())
64
53
json.forEach { ModelData .fromJson(it).load(branch) }
65
- branch.addListener(ChangeListener (branch, changeCallback))
66
54
return BranchJSImpl ({}, branch.withAutoTransactions())
67
55
}
68
56
@@ -88,11 +76,7 @@ fun connectClient(url: String): Promise<ClientJS> {
88
76
interface ClientJS {
89
77
fun dispose ()
90
78
91
- fun connectBranch (
92
- repositoryId : String ,
93
- branchId : String ,
94
- changeCallback : (ChangeJS ) -> Unit ,
95
- ): Promise <BranchJS >
79
+ fun connectBranch (repositoryId : String , branchId : String ): Promise <BranchJS >
96
80
97
81
fun fetchBranches (repositoryId : String ): Promise <Array <String >>
98
82
@@ -121,18 +105,13 @@ class ClientJSImpl(private val modelClient: ModelClientV2) : ClientJS {
121
105
}
122
106
123
107
@DelicateCoroutinesApi
124
- override fun connectBranch (
125
- repositoryId : String ,
126
- branchId : String ,
127
- changeCallback : (ChangeJS ) -> Unit ,
128
- ): Promise <BranchJS > {
108
+ override fun connectBranch (repositoryId : String , branchId : String ): Promise <BranchJS > {
129
109
return GlobalScope .promise {
130
110
val modelClient = modelClient
131
111
val branchReference = RepositoryId (repositoryId).getBranchReference(branchId)
132
112
val model: ReplicatedModel = modelClient.getReplicatedModel(branchReference)
133
113
model.start()
134
114
val branch = model.getBranch()
135
- branch.addListener(ChangeListener (branch, changeCallback))
136
115
val branchWithAutoTransaction = branch.withAutoTransactions()
137
116
return @promise BranchJSImpl ({ model.dispose() }, branchWithAutoTransaction)
138
117
}
@@ -143,6 +122,8 @@ class ClientJSImpl(private val modelClient: ModelClientV2) : ClientJS {
143
122
}
144
123
}
145
124
125
+ typealias ChangeHandler = (ChangeJS ) -> Unit
126
+
146
127
@UnstableModelixFeature(
147
128
reason = " The overarching task https://issues.modelix.org/issue/MODELIX-500 is in development." ,
148
129
intendedFinalization = " The client is intended to be finalized when the overarching task is finished." ,
@@ -152,43 +133,8 @@ interface BranchJS {
152
133
val rootNode: INodeJS
153
134
fun dispose ()
154
135
fun resolveNode (reference : INodeReferenceJS ): INodeJS ?
155
- }
156
-
157
- class ChangeListener (private val branch : IBranch , private val changeCallback : (ChangeJS ) -> Unit ) : IBranchListener {
158
-
159
- fun nodeIdToInode (nodeId : Long ): INodeJS {
160
- return toNodeJs(PNodeAdapter (nodeId, branch))
161
- }
162
-
163
- override fun treeChanged (oldTree : ITree ? , newTree : ITree ) {
164
- if (oldTree == null ) {
165
- return
166
- }
167
- newTree.visitChanges(
168
- oldTree,
169
- object : ITreeChangeVisitor {
170
- override fun containmentChanged (nodeId : Long ) {
171
- changeCallback(ContainmentChanged (nodeIdToInode(nodeId)))
172
- }
173
-
174
- override fun conceptChanged (nodeId : Long ) {
175
- changeCallback(ConceptChanged (nodeIdToInode(nodeId)))
176
- }
177
-
178
- override fun childrenChanged (nodeId : Long , role : String? ) {
179
- changeCallback(ChildrenChanged (nodeIdToInode(nodeId), role))
180
- }
181
-
182
- override fun referenceChanged (nodeId : Long , role : String ) {
183
- changeCallback(ReferenceChanged (nodeIdToInode(nodeId), role))
184
- }
185
-
186
- override fun propertyChanged (nodeId : Long , role : String ) {
187
- changeCallback(PropertyChanged (nodeIdToInode(nodeId), role))
188
- }
189
- },
190
- )
191
- }
136
+ fun addListener (handler : ChangeHandler )
137
+ fun removeListener (handler : ChangeHandler )
192
138
}
193
139
194
140
fun toNodeJs (rootNode : INode ) = JSNodeConverter .nodeToJs(rootNode).unsafeCast<INodeJS >()
0 commit comments