Simple implementation of an event bus using kotlin coroutines shared flow
Create the event:
class OrderWasCreated (override val occurredOn: OffsetDateTime = OffsetDateTime.now()) : DomainEvent
Create the subscriber:
class OrderWasCreatedSubscriber : DomainEventSubscriber<OrderWasCreated> {
override fun consume(event: OrderWasCreated) {
println("Order created")
}
}
Register the subscriber in the event bus:
val eventBus = SharedFlowEventBus()
.subscribe(OrderWasCreatedSubscriber())
Publish the event:
eventBus.publish(OrderWasCreated())