2019-06-13 10:23:03 +08:00
|
|
|
package event
|
|
|
|
|
|
|
|
type Bus interface {
|
2019-06-13 14:51:54 +08:00
|
|
|
// Subscribe creates new subscription. Failing to drain the incoming channel
|
|
|
|
// will cause publishers to get blocked
|
|
|
|
//
|
|
|
|
// evtTypes only accepts typed nil pointers, and uses the type information to
|
|
|
|
// select output type
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
// sub, cancel, err := eventbus.Subscribe(new(os.Signal))
|
|
|
|
// defer cancel()
|
|
|
|
//
|
|
|
|
// evt := (<-sub).(os.Signal) // guaranteed to be safe
|
|
|
|
Subscribe(eventType interface{}) (<-chan interface{}, CancelFunc, error)
|
|
|
|
|
|
|
|
|
|
|
|
Emitter(eventType interface{}) (EmitFunc, CancelFunc, error)
|
2019-06-13 10:23:03 +08:00
|
|
|
}
|
|
|
|
|
2019-06-13 14:51:54 +08:00
|
|
|
// EmitFunc emits events. If any channel subscribed to the topic is blocked,
|
|
|
|
// calls to EmitFunc will block
|
|
|
|
//
|
|
|
|
// Calling this function with wrong event type will cause a panic
|
|
|
|
type EmitFunc func(event interface{})
|
2019-06-13 10:23:03 +08:00
|
|
|
|
2019-06-13 14:51:54 +08:00
|
|
|
type CancelFunc func()
|
2019-06-13 10:23:03 +08:00
|
|
|
|