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

commonJvm.dev.icerock.moko.socket.SocketEvent.kt Maven / Gradle / Ivy

/*
 * Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
 */

package dev.icerock.moko.socket

import io.socket.client.Socket

actual sealed class SocketEvent : Mapper {
    actual object Connect : SocketEvent(), Mapper by UnitMapper() {
        override val socketIoEvents: List = listOf(Socket.EVENT_CONNECT)
    }

    actual object Connecting : SocketEvent(), Mapper by UnitMapper() {
        override val socketIoEvents: List = listOf(Constants.EVENT_CONNECTING)
    }

    actual object Disconnect : SocketEvent(), Mapper by UnitMapper() {
        override val socketIoEvents: List = listOf(Socket.EVENT_DISCONNECT)
    }

    actual object Error : SocketEvent() {
        override val socketIoEvents: List = listOf(
            Constants.EVENT_ERROR,
            Socket.EVENT_CONNECT_ERROR,
            Constants.EVENT_RECONNECT_ERROR
        )

        override fun mapper(array: Array): Throwable {
            return array[0] as Throwable
        }
    }

    actual object Message : SocketEvent() {
        override val socketIoEvents: List = listOf(Constants.EVENT_MESSAGE)
        override fun mapper(array: Array): Any {
            return array
        }
    }

    actual object Reconnect : SocketEvent(), Mapper by UnitMapper() {
        override val socketIoEvents: List = listOf(Constants.EVENT_RECONNECT)
    }

    actual object ReconnectAttempt : SocketEvent() {
        override val socketIoEvents: List = listOf(Constants.EVENT_RECONNECT_ATTEMPT)
        override fun mapper(array: Array): Int {
            return array[0] as Int
        }
    }

    actual object Ping : SocketEvent(), Mapper by UnitMapper() {
        override val socketIoEvents: List = listOf(Constants.EVENT_PING)
    }

    actual object Pong : SocketEvent(), Mapper by UnitMapper() {
        override val socketIoEvents: List = listOf(Constants.EVENT_PONG)
    }

    abstract val socketIoEvents: List

    private class UnitMapper : Mapper {
        override fun mapper(array: Array) = Unit
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy