AsyncEvent

open class AsyncEvent<T>(val clientSide: Boolean) : Event<T> (source)

The same as Event but with additional async collectors (via a flow). This allows you to listen to the event asynchronously in a totally different CoroutineScope, and unregister via cancellation of that scope. To create an event of this type, have a look at the Event.syncAsync and Event.syncAsyncImmutable functions.

Constructors

Link copied to clipboard
constructor(clientSide: Boolean)

Properties

Link copied to clipboard
Link copied to clipboard
open val flow: MutableSharedFlow<T>

The internal flow to which events are emitted. It has no buffer, so new calls to MutableSharedFlow.emit will suspend if the listeners have not finished handling the previous event.

Link copied to clipboard
val invokeScope: CoroutineScope

The scope used for emitting events without blocking the current execution.

Link copied to clipboard

The listeners added by listen, sorted by EventPriority via the index in the outer list.

Link copied to clipboard

The listeners added by monitor.

Functions

Link copied to clipboard
suspend fun collect(collector: FlowCollector<T>)

Calls SharedFlow.collect on the underlying flow. This way the collector receives all events. Note that the thread and / or CoroutineScope you call this in decides on which thread you handle the events.

Link copied to clipboard
fun collectInScope(collector: FlowCollector<T>): Job

Listens to this event, and emits to the collector if it is called. Launches SharedFlow.collect on the underlying flow in the current CoroutineScope (taken from context), using the dispatcher from the current scope.

Link copied to clipboard
fun collectInScopeSync(collector: FlowCollector<T>): Job

Listens to this event, and emits to the collector if it is called. Launches SharedFlow.collect on the underlying flow in the current CoroutineScope (taken from context), using the fitting synchronous Minecraft main thread dispatcher (syncDispatcher).

Link copied to clipboard
open override fun invoke(instance: T)

Invokes this event. Calling this function will trigger all listeners and collectors.

Link copied to clipboard
fun listen(priority: EventPriority = EventPriority.NORMAL, register: Boolean = true, callback: (MutableEventScope, event: T) -> Unit): ListenerInstance<*>

Listens to this event. The callback will always be called synchronously. This function is synchronized, so it may be called from any thread.

Link copied to clipboard
fun monitor(register: Boolean = true, callback: (T) -> Unit): ListenerInstance<*>

Monitors this event. This is the same as listen, but you do not have access to the mutable functions of the event scope. This callback will be executed after EventPriority.LAST, therefore monitor sees the final state.