mirror of
https://github.com/libp2p/go-libp2p-core.git
synced 2025-01-15 02:30:05 +08:00
eventbus: add wildcard subscription type; getter to enumerate known types (#153)
This commit is contained in:
parent
30bf48c8d6
commit
264788628f
25
event/bus.go
25
event/bus.go
@ -1,6 +1,9 @@
|
|||||||
package event
|
package event
|
||||||
|
|
||||||
import "io"
|
import (
|
||||||
|
"io"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
// SubscriptionOpt represents a subscriber option. Use the options exposed by the implementation of choice.
|
// SubscriptionOpt represents a subscriber option. Use the options exposed by the implementation of choice.
|
||||||
type SubscriptionOpt = func(interface{}) error
|
type SubscriptionOpt = func(interface{}) error
|
||||||
@ -11,6 +14,14 @@ type EmitterOpt = func(interface{}) error
|
|||||||
// CancelFunc closes a subscriber.
|
// CancelFunc closes a subscriber.
|
||||||
type CancelFunc = func()
|
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.
|
// Emitter represents an actor that emits events onto the eventbus.
|
||||||
type Emitter interface {
|
type Emitter interface {
|
||||||
io.Closer
|
io.Closer
|
||||||
@ -39,6 +50,11 @@ type Bus interface {
|
|||||||
//
|
//
|
||||||
// Failing to drain the channel may cause publishers to block.
|
// 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
|
// Simple example
|
||||||
//
|
//
|
||||||
// sub, err := eventbus.Subscribe(new(EventType))
|
// 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
|
// defer em.Close() // MUST call this after being done with the emitter
|
||||||
// em.Emit(EventT{})
|
// em.Emit(EventT{})
|
||||||
Emitter(eventType interface{}, opts ...EmitterOpt) (Emitter, error)
|
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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user