-
Notifications
You must be signed in to change notification settings - Fork 8
/
ClientJS.kt
190 lines (165 loc) · 6.28 KB
/
ClientJS.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Copyright (c) 2023.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:OptIn(UnstableModelixFeature::class)
package org.modelix.model.client2
import INodeJS
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.promise
import org.modelix.kotlin.utils.UnstableModelixFeature
import org.modelix.model.ModelFacade
import org.modelix.model.api.IBranch
import org.modelix.model.api.IBranchListener
import org.modelix.model.api.INode
import org.modelix.model.api.ITree
import org.modelix.model.api.ITreeChangeVisitor
import org.modelix.model.api.JSNodeConverter
import org.modelix.model.api.PNodeAdapter
import org.modelix.model.api.getRootNode
import org.modelix.model.data.ModelData
import org.modelix.model.lazy.RepositoryId
import org.modelix.model.withAutoTransactions
import kotlin.Unit
import kotlin.js.Promise
@UnstableModelixFeature(
reason = "The overarching task https://issues.modelix.org/issue/MODELIX-500 is in development.",
intendedFinalization = "The client is intended to be finalized when the overarching task is finished.",
)
@JsExport
fun loadModelsFromJson(
json: Array<String>,
changeCallback: (ChangeJS) -> Unit,
): INodeJS {
val branch = ModelFacade.toLocalBranch(ModelFacade.newLocalTree())
json.forEach { ModelData.fromJson(it).load(branch) }
branch.addListener(ChangeListener(branch, changeCallback))
val rootNode = branch.withAutoTransactions().getRootNode()
return toNodeJs(rootNode)
}
@UnstableModelixFeature(
reason = "The overarching task https://issues.modelix.org/issue/MODELIX-500 is in development.",
intendedFinalization = "The client is intended to be finalized when the overarching task is finished.",
)
@JsExport
@DelicateCoroutinesApi
fun connectClient(url: String): Promise<ClientJS> {
return GlobalScope.promise {
val client = ModelClientV2.builder().url(url).build()
client.init()
return@promise ClientJSImpl(client)
}
}
@UnstableModelixFeature(
reason = "The overarching task https://issues.modelix.org/issue/MODELIX-500 is in development.",
intendedFinalization = "The client is intended to be finalized when the overarching task is finished.",
)
@JsExport
interface ClientJS {
fun dispose()
fun connectBranch(
repositoryId: String,
branchId: String,
changeCallback: (ChangeJS) -> Unit,
): Promise<BranchJS>
fun fetchBranches(repositoryId: String): Promise<Array<String>>
fun fetchRepositories(): Promise<Array<String>>
}
class ClientJSImpl(private val modelClient: ModelClientV2) : ClientJS {
@DelicateCoroutinesApi
override fun fetchRepositories(): Promise<Array<String>> {
return GlobalScope.promise {
return@promise modelClient.listRepositories()
.map { it.id }.toTypedArray()
}
}
@DelicateCoroutinesApi
override fun fetchBranches(
repositoryId: String,
): Promise<Array<String>> {
return GlobalScope.promise {
val repositoryIdObject = RepositoryId(repositoryId)
return@promise modelClient.listBranches(repositoryIdObject)
.map { it.branchName }.toTypedArray()
}
}
@DelicateCoroutinesApi
override fun connectBranch(
repositoryId: String,
branchId: String,
changeCallback: (ChangeJS) -> Unit,
): Promise<BranchJS> {
return GlobalScope.promise {
val modelClient = modelClient
val branchReference = RepositoryId(repositoryId).getBranchReference(branchId)
val model: ReplicatedModel = modelClient.getReplicatedModel(branchReference)
model.start()
val branch = model.getBranch()
branch.addListener(ChangeListener(branch, changeCallback))
val rootNode = branch.withAutoTransactions().getRootNode()
val jsRootNode = toNodeJs(rootNode)
return@promise BranchJSImpl(model, jsRootNode)
}
}
override fun dispose() {
modelClient.close()
}
}
@UnstableModelixFeature(
reason = "The overarching task https://issues.modelix.org/issue/MODELIX-500 is in development.",
intendedFinalization = "The client is intended to be finalized when the overarching task is finished.",
)
@JsExport
interface BranchJS {
val rootNode: INodeJS
fun dispose()
}
class BranchJSImpl(private val model: ReplicatedModel, private val jsRootNode: INodeJS) : BranchJS {
override val rootNode: INodeJS
get() {
return jsRootNode
}
override fun dispose() {
model.dispose()
}
}
class ChangeListener(private val branch: IBranch, private val changeCallback: (ChangeJS) -> Unit) : IBranchListener {
fun nodeIdToInode(nodeId: Long): INodeJS {
return toNodeJs(PNodeAdapter(nodeId, branch))
}
override fun treeChanged(oldTree: ITree?, newTree: ITree) {
if (oldTree == null) {
return
}
newTree.visitChanges(
oldTree,
object : ITreeChangeVisitor {
override fun containmentChanged(nodeId: Long) {
changeCallback(ContainmentChanged(nodeIdToInode(nodeId)))
}
override fun childrenChanged(nodeId: Long, role: String?) {
changeCallback(ChildrenChanged(nodeIdToInode(nodeId), role))
}
override fun referenceChanged(nodeId: Long, role: String) {
changeCallback(ReferenceChanged(nodeIdToInode(nodeId), role))
}
override fun propertyChanged(nodeId: Long, role: String) {
changeCallback(PropertyChanged(nodeIdToInode(nodeId), role))
}
},
)
}
}
fun toNodeJs(rootNode: INode) = JSNodeConverter.nodeToJs(rootNode).unsafeCast<INodeJS>()