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

commonMain.com.attafitamim.navigation.router.base.result.SimpleResultWire.kt Maven / Gradle / Ivy

There is a newer version: 3.0.2-alpha13
Show newest version
package com.attafitamim.navigation.router.base.result

import com.attafitamim.navigation.router.core.commands.MessageHandler
import com.attafitamim.navigation.router.core.global.Disposable
import com.attafitamim.navigation.router.core.result.ResultKey
import com.attafitamim.navigation.router.core.result.ResultListener
import com.attafitamim.navigation.router.core.result.ResultWire

class SimpleResultWire(
    private val messageHandler: MessageHandler
) : ResultWire {

    private val listeners = HashMap, ResultListener<*>>()

    override fun  setResultListener(
        key: ResultKey,
        listener: ResultListener
    ): Disposable {
        addListener(key, listener)
        return Disposable {
            removeKey(key)
        }
    }

    @Suppress("UNCHECKED_CAST")
    override fun  sendResult(key: ResultKey, data: T) {
        messageHandler.post {
            val listener = listeners.remove(key) as? ResultListener ?: return@post
            listener.onResult(data)
        }
    }

    private fun removeKey(key: ResultKey<*>) = messageHandler.post {
        listeners.remove(key)
    }

    private fun addListener(
        key: ResultKey<*>,
        listener: ResultListener<*>
    ) = messageHandler.post {
        listeners[key] = listener
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy