Skip to content

Commit

Permalink
update the editor when the MPS model changes
Browse files Browse the repository at this point in the history
  • Loading branch information
slisson committed Nov 25, 2023
1 parent 0793cf7 commit dd2eae4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ modelix-model-api-gen = { id = "org.modelix.model-api-gen", version.ref = "model
npm-publish = { id = "dev.petuska.npm.publish", version = "3.4.1" }

[versions]
modelixCore = "3.14.2-9-gdeccc44-SNAPSHOT"
modelixCore = "3.14.2-11-g3bffd93-SNAPSHOT"

[libraries]
modelix-model-api = { group = "org.modelix", name = "model-api", version.ref = "modelixCore" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import org.modelix.model.api.BuiltinLanguages
import org.modelix.model.api.INode
import org.modelix.model.api.NodeReference
import org.modelix.model.api.runSynchronized
import org.modelix.model.mpsadapters.MPSLanguageRepository
import org.modelix.model.mpsadapters.MPSChangeTranslator
import org.modelix.model.mpsadapters.MPSRepositoryAsNode
import java.net.URLEncoder
import java.nio.charset.StandardCharsets
Expand All @@ -66,6 +66,7 @@ class ModelixSSRServerForMPS : Disposable {
private var ssrServer: ModelixSSRServer? = null
private var ktorServer: ApplicationEngine? = null
private var aspectsFromMPS: LanguageAspectsFromMPSModules? = null
private var mpsChangeTranslator: MPSChangeTranslator? = null
private val projects: MutableSet<Project> = Collections.synchronizedSet(HashSet())

fun registerProject(project: Project) {
Expand Down Expand Up @@ -95,10 +96,12 @@ class ModelixSSRServerForMPS : Disposable {

println("starting modelix SSR server")

val repository = getMPSProjects().first().repository
mpsChangeTranslator = MPSChangeTranslator()
mpsChangeTranslator!!.start(repository)
val ssrServer = ModelixSSRServer((getRootNode() ?: return).getArea())
val aspectsFromMPS = LanguageAspectsFromMPSModules(getMPSProjects().first().repository)
this.aspectsFromMPS = aspectsFromMPS
ssrServer.editorEngine.addRegistry(aspectsFromMPS)
aspectsFromMPS = LanguageAspectsFromMPSModules(repository)
ssrServer.editorEngine.addRegistry(aspectsFromMPS!!)
ktorServer = embeddedServer(Netty, port = 43593) {
initKtorServer(ssrServer)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,68 @@ import org.modelix.editor.ssr.common.INodeUpdateData
import org.modelix.editor.ssr.common.MessageFromClient
import org.modelix.editor.ssr.common.MessageFromServer
import org.modelix.editor.ssr.common.TextNodeUpdateData
import org.modelix.incremental.DependencyTracking
import org.modelix.incremental.IDependencyListener
import org.modelix.incremental.IStateVariableGroup
import org.modelix.incremental.IStateVariableReference
import org.modelix.incremental.IncrementalEngine
import org.modelix.kotlin.utils.runSynchronized
import org.modelix.model.api.INodeResolutionScope
import org.modelix.model.api.NodeReference
import org.modelix.model.api.resolveIn
import org.modelix.model.api.runSynchronized
import org.modelix.model.area.IArea
import java.util.Collections

private val LOG = KotlinLogging.logger { }

class ModelixSSRServer(private val nodeResolutionScope: INodeResolutionScope) {

private val incrementalEngine = IncrementalEngine()
val editorEngine: EditorEngine = EditorEngine(incrementalEngine)
private val allSessions: MutableSet<WebsocketSession> = Collections.synchronizedSet(LinkedHashSet())
private val lock = Any()
private val dependencyListener: IDependencyListener = object : IDependencyListener {
override fun parentGroupChanged(childGroup: IStateVariableGroup) {}
override fun accessed(key: IStateVariableReference<*>) {}
override fun modified(key: IStateVariableReference<*>) { updateAll() }
}

fun install(route: Route) {
route.installRoutes()
}

fun dispose() {
DependencyTracking.removeListener(dependencyListener)
}

private fun Route.installRoutes() {
route("client") {
DependencyTracking.registerListener(dependencyListener)

}
webSocket("ws") {
val session = WebsocketSession(this)
try {
allSessions.add(session)
session.receiveMessages()
} finally {
allSessions.remove(session)
session.dispose()
}
}
}

fun updateAll() {
val sessions = runSynchronized(allSessions) { allSessions.toList() }
runSynchronized(lock) {
sessions.forEach {
try {
it.updateAllEditors()
} catch (ex: Exception) {
LOG.error(ex) { "Failed to send editor update" }
}
}
}
}

private inner class WebsocketSession(val ws: DefaultWebSocketServerSession) {
private val editors = HashMap<String, EditorSession>()

Expand Down Expand Up @@ -100,6 +129,10 @@ class ModelixSSRServer(private val nodeResolutionScope: INodeResolutionScope) {
editors.clear()
}

fun updateAllEditors() {
editors.values.forEach { it.sendUpdate() }
}

private inner class EditorSession(val editorId: String) {
private var editorComponent: EditorComponent? = null
private val commonElementPrefix = editorId + "-"
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pluginManagement {
}
versionCatalogs {
create("coreLibs") {
from("org.modelix:core-version-catalog:3.14.2-9-gdeccc44-SNAPSHOT")
from("org.modelix:core-version-catalog:3.14.2-11-g3bffd93-SNAPSHOT")
}
}
}
Expand Down

0 comments on commit dd2eae4

Please sign in to comment.