Event

abstract class Event<Data, E : Event<Data, E, O>, O : Event.Observer<Data, E>>(val name: String)(source)

Base abstraction for creating enum like event/observer type hierarchies using kotlin sealed classes & data objects.

e.g.

public sealed class MyEvent private constructor(
    name: String,
): Event(name) {

    public data object THIS: MyEvent("THIS")
    public data object THAT: MyEvent("THAT")

    public open class Observer(
        event: MyEvent,
        tag: String?,
        executor: OnEvent.Executor?,
        onEvent: OnEvent,
    ): Event.Observer(
        event,
        tag,
        executor,
        onEvent
    )

    public companion object: Entries(numEvents = 2) {

        @JvmStatic
        @Throws(IllegalArgumentException::class)
        public override fun valueOf(name: String): MyEvent {
            return super.valueOf(name)
        }

        @JvmStatic
        public override fun valueOfOrNull(name: String): MyEvent? {
            return super.valueOfOrNull(name)
        }

        @JvmStatic
        public override fun entries(): Set {
            return super.entries()
        }

        protected override val lazyEntries: ThisBlock> =
            ThisBlock {
                // NOTE: Update numEvents when adding an event
                add(THIS); add(THAT);
            }
    }

    protected final override fun createProtected(
        event: MyEvent,
        tag: String?,
        executor: OnEvent.Executor?,
        onEvent: OnEvent,
    ): Observer = Observer(event, tag, executor, onEvent)
}

See also

io.matthewnelson.kmp.tor.runtime.RuntimeEvent

Inheritors

Constructors

Link copied to clipboard
protected constructor(name: String)

Types

Link copied to clipboard
abstract class Entries<E : Event<*, *, *>>(numEvents: Int)

Abstraction for the Event implementation's companion object.

Link copied to clipboard
abstract class Observer<Data, E : Event<Data, E, *>>(val event: E, tag: String?, executor: OnEvent.Executor?, onEvent: OnEvent<Data>)

Base abstraction for creating event observer types.

Properties

Link copied to clipboard
@JvmField
val name: String

Functions

Link copied to clipboard
protected abstract fun createProtected(event: E, tag: String?, executor: OnEvent.Executor?, onEvent: OnEvent<Data>): O

Protected factory function for creating instances of the Event.Observer implementation.

Link copied to clipboard
fun observer(onEvent: OnEvent<Data>): O

Creates a new observer for subscribing to its events.

fun observer(tag: String, onEvent: OnEvent<Data>): O

Creates a new observer with the provided tag for subscribing to its events.

fun observer(tag: String?, executor: OnEvent.Executor?, onEvent: OnEvent<Data>): O

Creates a new observer with the provided tag and executor for subscribing to its events.