2019-06-13 10:23:03 +08:00
|
|
|
package event
|
|
|
|
|
2019-06-17 00:15:35 +08:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SubSettings struct {
|
|
|
|
forcedType reflect.Type
|
|
|
|
}
|
|
|
|
type SubOption func(*SubSettings) error
|
|
|
|
|
|
|
|
func ForceSubType(evtType interface{}) SubOption {
|
|
|
|
return func(s *SubSettings) error {
|
|
|
|
typ := reflect.TypeOf(evtType)
|
|
|
|
if typ.Kind() != reflect.Ptr {
|
|
|
|
return errors.New("ForceSubType called with non-pointer type")
|
|
|
|
}
|
|
|
|
s.forcedType = typ
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2019-06-13 16:04:12 +08:00
|
|
|
|
2019-06-17 01:06:49 +08:00
|
|
|
type EmitterSettings struct{}
|
2019-06-13 16:04:12 +08:00
|
|
|
type EmitterOption func(*EmitterSettings)
|
|
|
|
|
2019-06-13 10:23:03 +08:00
|
|
|
type Bus interface {
|
2019-06-17 01:06:49 +08:00
|
|
|
// Subscribe creates new subscription. Failing to drain the channel will cause
|
|
|
|
// publishers to get blocked
|
|
|
|
Subscribe(typedChan interface{}, opts ...SubOption) (CancelFunc, error)
|
|
|
|
|
|
|
|
// Emitter creates new emitter
|
2019-06-13 14:51:54 +08:00
|
|
|
//
|
2019-06-17 01:06:49 +08:00
|
|
|
// eventType accepts typed nil pointers, and uses the type information to
|
2019-06-13 14:51:54 +08:00
|
|
|
// select output type
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
// sub, cancel, err := eventbus.Subscribe(new(os.Signal))
|
|
|
|
// defer cancel()
|
2019-06-13 16:04:12 +08:00
|
|
|
Emitter(eventType interface{}, opts ...EmitterOption) (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()
|