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

com.github.stormbit.sdk.longpoll.UpdatesHandler.kt Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
package com.github.stormbit.sdk.longpoll

import com.github.stormbit.sdk.callbacks.AbstractCallback
import com.github.stormbit.sdk.callbacks.Callback
import com.github.stormbit.sdk.clients.Client
import com.github.stormbit.sdk.clients.Client.Companion.scheduler
import com.google.gson.JsonArray
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.TimeUnit

@Suppress("unused")
abstract class UpdatesHandler(private val client: Client) : Thread() {
    @Volatile
    protected var queue = Queue()

    @Volatile
    var sendTyping = false

    /**
     * Maps with callbacks
     */
    protected val callbacks = ConcurrentHashMap>()
    protected val chatCallbacks = ConcurrentHashMap()
    protected val abstractCallbacks = ConcurrentHashMap()

    fun handle(updates: JsonArray) {
        this.queue.putAll(updates)
    }

    override fun run() {
        scheduler.scheduleWithFixedDelay(this::handleCurrentUpdate, 0, 1, TimeUnit.MILLISECONDS)
    }

    /**
     * Handle one event from longpoll server
     */
    abstract fun handleCurrentUpdate()

    fun registerCallback(name: String, callback: Callback<*>) {
        this.callbacks[name] = callback as Callback
    }

    fun registerChatCallback(name: String, chatCallback: AbstractCallback) {
        this.chatCallbacks[name] = chatCallback
    }

    fun registerAbstractCallback(name: String, abstractCallback: AbstractCallback) {
        this.abstractCallbacks[name] = abstractCallback
    }

    fun callbacksCount(): Int = this.callbacks.size

    fun abstractCallbacksCount(): Int = this.abstractCallbacks.size

    fun chatCallbacksCount(): Int = this.chatCallbacks.size

    fun commandsCount(): Int = this.client.commands.size
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy