Skip to content

Commit

Permalink
Refactor: use EventListener<T> interface for Java compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaDemchenko committed Jan 24, 2025
1 parent 46214d2 commit 6637a4b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
24 changes: 12 additions & 12 deletions p2pml/src/main/java/com/novage/p2pml/CoreEventMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,61 @@ import kotlinx.serialization.Serializable
*
* See [P2P Media Loader CoreEventMap](https://novage.github.io/p2p-media-loader/docs/v2.1.0/types/p2p-media-loader-core.CoreEventMap.html)
*/
sealed class CoreEventMap<out T> {
sealed class CoreEventMap<T> {
/**
* Fired when a segment is fully downloaded and available for use.
*/
data object OnSegmentLoaded : CoreEventMap<SegmentLoadDetails>()
object OnSegmentLoaded : CoreEventMap<SegmentLoadDetails>()

/**
* Fired at the beginning of a segment download process.
*/
data object OnSegmentStart : CoreEventMap<SegmentStartDetails>()
object OnSegmentStart : CoreEventMap<SegmentStartDetails>()

/**
* Fired when an error occurs during the download of a segment.
*/
data object OnSegmentError : CoreEventMap<SegmentErrorDetails>()
object OnSegmentError : CoreEventMap<SegmentErrorDetails>()

/**
* Fired if the download of a segment is aborted before completion.
*/
data object OnSegmentAbort : CoreEventMap<SegmentAbortDetails>()
object OnSegmentAbort : CoreEventMap<SegmentAbortDetails>()

/**
* Fired when a new peer-to-peer connection is established.
*/
data object OnPeerConnect : CoreEventMap<PeerDetails>()
object OnPeerConnect : CoreEventMap<PeerDetails>()

/**
* Fired when an existing peer-to-peer connection is closed.
*/
data object OnPeerClose : CoreEventMap<PeerDetails>()
object OnPeerClose : CoreEventMap<PeerDetails>()

/**
* Fired when an error occurs during a peer-to-peer connection.
*/
data object OnPeerError : CoreEventMap<PeerErrorDetails>()
object OnPeerError : CoreEventMap<PeerErrorDetails>()

/**
* Fired after a chunk of data from a segment has been successfully downloaded.
*/
data object OnChunkDownloaded : CoreEventMap<ChunkDownloadedDetails>()
object OnChunkDownloaded : CoreEventMap<ChunkDownloadedDetails>()

/**
* Fired when a chunk of data has been successfully uploaded to a peer.
*/
data object OnChunkUploaded : CoreEventMap<ChunkUploadedDetails>()
object OnChunkUploaded : CoreEventMap<ChunkUploadedDetails>()

/**
* Fired when an error occurs during the tracker request process.
*/
data object OnTrackerError : CoreEventMap<TrackerErrorDetails>()
object OnTrackerError : CoreEventMap<TrackerErrorDetails>()

/**
* Fired when a warning occurs during the tracker request process.
*/
data object OnTrackerWarning : CoreEventMap<TrackerWarningDetails>()
object OnTrackerWarning : CoreEventMap<TrackerWarningDetails>()
}

/**
Expand Down
22 changes: 14 additions & 8 deletions p2pml/src/main/java/com/novage/p2pml/utils/EventEmitter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,36 @@ package com.novage.p2pml.utils

import com.novage.p2pml.CoreEventMap

fun interface EventListener<T> {
fun onEvent(data: T)
}

class EventEmitter {
private val listeners = mutableMapOf<CoreEventMap<*>, MutableList<(Any?) -> Unit>>()
private val listeners = mutableMapOf<CoreEventMap<*>, MutableList<EventListener<*>>>()

fun <T> addEventListener(
event: CoreEventMap<T>,
listener: (T) -> Unit,
listener: EventListener<T>,
) {
@Suppress("UNCHECKED_CAST")
val wrapper: (Any?) -> Unit = { data -> listener(data as T) }
listeners.getOrPut(event) { mutableListOf() }.add(wrapper)
val list = listeners.getOrPut(event) { mutableListOf() }
list.add(listener)
}

fun <T> emit(
event: CoreEventMap<T>,
data: T,
) {
listeners[event]?.forEach { it(data) }
listeners[event]?.forEach { listener ->
@Suppress("UNCHECKED_CAST")
(listener as EventListener<T>).onEvent(data)
}
}

fun <T> removeEventListener(
event: CoreEventMap<T>,
listener: (T) -> Unit,
listener: EventListener<T>,
) {
listeners.remove(event)?.removeAll { it == listener }
listeners[event]?.remove(listener)
}

fun <T> hasListeners(event: CoreEventMap<T>): Boolean = listeners[event]?.isNotEmpty() ?: false
Expand Down

0 comments on commit 6637a4b

Please sign in to comment.