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

iosMain.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 cocoapods.mokoSocketIo.SocketEventConnect
import cocoapods.mokoSocketIo.SocketEventConnecting
import cocoapods.mokoSocketIo.SocketEventDisconnect
import cocoapods.mokoSocketIo.SocketEventError
import cocoapods.mokoSocketIo.SocketEventMessage
import cocoapods.mokoSocketIo.SocketEventPing
import cocoapods.mokoSocketIo.SocketEventPong
import cocoapods.mokoSocketIo.SocketEventReconnect
import cocoapods.mokoSocketIo.SocketEventReconnectAttempt
import kotlinx.cinterop.ExperimentalForeignApi
import cocoapods.mokoSocketIo.SocketEvent as SocketIoEvent

@OptIn(ExperimentalForeignApi::class)
actual sealed class SocketEvent : Mapper {
    actual object Connect : SocketEvent(), Mapper by UnitMapper() {
        override val platformEvent: SocketIoEvent = SocketEventConnect

        override fun mapper(data: List<*>) = Unit
    }

    actual object Connecting : SocketEvent(), Mapper by UnitMapper() {
        override val platformEvent: SocketIoEvent = SocketEventConnecting
    }

    actual object Disconnect : SocketEvent(), Mapper by UnitMapper() {
        override val platformEvent: SocketIoEvent = SocketEventDisconnect
    }

    actual object Error : SocketEvent() {
        override val platformEvent: SocketIoEvent = SocketEventError

        override fun mapper(data: List<*>): Throwable {
            val message = data.first() as String
            return Exception(message)
        }
    }

    actual object Message : SocketEvent() {
        override val platformEvent: SocketIoEvent = SocketEventMessage

        override fun mapper(data: List<*>): Any = data
    }

    actual object Reconnect : SocketEvent(), Mapper by UnitMapper() {
        override val platformEvent: SocketIoEvent = SocketEventReconnect
    }

    actual object ReconnectAttempt : SocketEvent() {
        override val platformEvent: SocketIoEvent = SocketEventReconnectAttempt

        override fun mapper(data: List<*>): Int {
            // ios send negative numbers of attempt
            return -(data.first() as Long).toInt()
        }
    }

    actual object Ping : SocketEvent(), Mapper by UnitMapper() {
        override val platformEvent: SocketIoEvent = SocketEventPing
    }

    actual object Pong : SocketEvent(), Mapper by UnitMapper() {
        override val platformEvent: SocketIoEvent = SocketEventPong
    }

    abstract val platformEvent: SocketIoEvent

    private class UnitMapper : Mapper {
        override fun mapper(data: List<*>) = Unit
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy