diff --git a/core/event/bus.go b/core/event/bus.go index 9dcf937822..0cd8d2ff7c 100644 --- a/core/event/bus.go +++ b/core/event/bus.go @@ -1,6 +1,9 @@ package event -import "io" +import ( + "io" + "reflect" +) // SubscriptionOpt represents a subscriber option. Use the options exposed by the implementation of choice. type SubscriptionOpt = func(interface{}) error @@ -11,6 +14,14 @@ type EmitterOpt = func(interface{}) error // CancelFunc closes a subscriber. type CancelFunc = func() +// wildcardSubscriptionType is a virtual type to represent wildcard +// subscriptions. +type wildcardSubscriptionType interface{} + +// WildcardSubscription is the type to subscribe to to receive all events +// emitted in the eventbus. +var WildcardSubscription = new(wildcardSubscriptionType) + // Emitter represents an actor that emits events onto the eventbus. type Emitter interface { io.Closer @@ -39,6 +50,11 @@ type Bus interface { // // Failing to drain the channel may cause publishers to block. // + // If you want to subscribe to ALL events emitted in the bus, use + // `WildcardSubscription` as the `eventType`: + // + // eventbus.Subscribe(WildcardSubscription) + // // Simple example // // sub, err := eventbus.Subscribe(new(EventType)) @@ -71,4 +87,11 @@ type Bus interface { // defer em.Close() // MUST call this after being done with the emitter // em.Emit(EventT{}) Emitter(eventType interface{}, opts ...EmitterOpt) (Emitter, error) + + // GetAllEventTypes returns all the event types that this bus knows about + // (having emitters and subscribers). It omits the WildcardSubscription. + // + // The caller is guaranteed that this function will only return value types; + // no pointer types will be returned. + GetAllEventTypes() []reflect.Type }