All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlinx.jupyter.messaging.comms.CommHandler.kt Maven / Gradle / Ivy

Go to download

Implementation of REPL compiler and preprocessor for Jupyter dialect of Kotlin (IDE-compatible)

There is a newer version: 0.12.0-333
Show newest version
package org.jetbrains.kotlinx.jupyter.messaging.comms

import kotlinx.serialization.json.JsonObject
import org.jetbrains.kotlinx.jupyter.api.libraries.Comm
import org.jetbrains.kotlinx.jupyter.repl.ReplForJupyter

interface CommHandler {
    val targetId: String

    fun onReceive(
        comm: Comm,
        messageContent: JsonObject,
        repl: ReplForJupyter,
    )
}

fun ReplForJupyter.installCommHandler(commHandler: CommHandler) {
    val repl = this
    notebook.commManager.registerCommTarget(commHandler.targetId) { comm, _ ->
        // handler.onReceive(comm, data, repl) // maybe send right away?

        comm.onMessage {
            commHandler.onReceive(comm, it, repl)
        }
    }
}

fun Collection.requireUniqueTargets() {
    val commHandlers = this
    val uniqueTargets = commHandlers.distinctBy { it.targetId }.size
    require(uniqueTargets == commHandlers.size) {
        val duplicates = commHandlers.groupingBy { it }.eachCount().filter { it.value > 1 }.keys
        "Duplicated bundled comm targets found! $duplicates"
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy