-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Java API to receive events (Fix #23)
- Loading branch information
1 parent
f4f3263
commit 2853cdc
Showing
11 changed files
with
118 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
java/src/main/kotlin/dev/schlaubi/lavakord/interop/JavaEventSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package dev.schlaubi.lavakord.interop | ||
|
||
import dev.schlaubi.lavakord.audio.EventSource | ||
import kotlinx.coroutines.jdk9.asPublisher | ||
import mu.KotlinLogging | ||
import java.util.concurrent.Flow | ||
import java.util.concurrent.Flow.Subscriber | ||
import java.util.function.Consumer | ||
|
||
private val LOG = KotlinLogging.logger { } | ||
|
||
/** | ||
* Java equivalent of [EventSource]. | ||
* | ||
* @param T the base event type | ||
* | ||
* @property suspendingEventSource the underlying delegate [EventSource] | ||
* @property events All events received using a [Flow.Publisher] | ||
*/ | ||
public interface JavaEventSource<T : Any> { | ||
public val suspendingEventSource: EventSource<T> | ||
|
||
public val events: Flow.Publisher<T> | ||
get() = suspendingEventSource.events.asPublisher() | ||
|
||
/** | ||
* Creates an event handler which executes [Consumer] for every event of [eventType]. | ||
*/ | ||
public fun <E : T> on(eventType: Class<E>, handler: Consumer<E>) { | ||
events.subscribe(object : Subscriber<T> { | ||
override fun onSubscribe(subscription: Flow.Subscription) = Unit | ||
|
||
override fun onError(throwable: Throwable) { | ||
// This in theory should never happen | ||
LOG.error(throwable) { "An error occurred whilst subscribing to events" } | ||
} | ||
|
||
override fun onComplete() = Unit | ||
|
||
override fun onNext(item: T) { | ||
if (eventType.isInstance(item)) { | ||
handler.accept(eventType.cast(item)) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters