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

org.jetbrains.kotlinx.jupyter.protocol.AbstractJupyterConnection.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-290
Show newest version
package org.jetbrains.kotlinx.jupyter.protocol

import org.jetbrains.kotlinx.jupyter.api.libraries.JupyterConnection
import org.jetbrains.kotlinx.jupyter.api.libraries.JupyterSocketType
import org.jetbrains.kotlinx.jupyter.api.libraries.RawMessage
import org.jetbrains.kotlinx.jupyter.api.libraries.RawMessageCallback
import org.jetbrains.kotlinx.jupyter.api.libraries.type

abstract class AbstractJupyterConnection : JupyterConnection {
    private val callbacks = mutableMapOf()

    abstract val socketManager: JupyterSocketManagerBase

    override fun addMessageCallback(callback: RawMessageCallback): RawMessageCallback {
        val socket = socketManager.fromSocketType(callback.socket)
        val socketCallback: SocketRawMessageCallback = { message ->
            if (callback.messageType?.let { it == message.type } != false) {
                callback.action(message)
            }
        }
        callbacks[callback] = socket.onRawMessage(socketCallback)
        return callback
    }

    override fun removeMessageCallback(callback: RawMessageCallback) {
        val socketCallback = callbacks[callback] ?: return
        val socket = socketManager.fromSocketType(callback.socket)
        socket.removeCallback(socketCallback)
    }

    override fun send(socketName: JupyterSocketType, message: RawMessage) {
        val socket = socketManager.fromSocketType(socketName)
        socket.sendRawMessage(message)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy