Commit 0976d4b3 authored by Jakub Sztandera's avatar Jakub Sztandera

Enable buffering by default

License: MIT
Signed-off-by: default avatarJakub Sztandera <kubuxu@protonmail.ch>
parent 61257f90
...@@ -129,7 +129,7 @@ var _ event.Subscription = (*sub)(nil) ...@@ -129,7 +129,7 @@ var _ event.Subscription = (*sub)(nil)
// publishers to get blocked. CancelFunc is guaranteed to return after last send // publishers to get blocked. CancelFunc is guaranteed to return after last send
// to the channel // to the channel
func (b *basicBus) Subscribe(evtTypes interface{}, opts ...event.SubscriptionOpt) (_ event.Subscription, err error) { func (b *basicBus) Subscribe(evtTypes interface{}, opts ...event.SubscriptionOpt) (_ event.Subscription, err error) {
var settings subSettings settings := subSettings(subSettingsDefault)
for _, opt := range opts { for _, opt := range opts {
if err := opt(&settings); err != nil { if err := opt(&settings); err != nil {
return nil, err return nil, err
...@@ -184,6 +184,7 @@ func (b *basicBus) Subscribe(evtTypes interface{}, opts ...event.SubscriptionOpt ...@@ -184,6 +184,7 @@ func (b *basicBus) Subscribe(evtTypes interface{}, opts ...event.SubscriptionOpt
// emit(EventT{}) // emit(EventT{})
func (b *basicBus) Emitter(evtType interface{}, opts ...event.EmitterOpt) (e event.Emitter, err error) { func (b *basicBus) Emitter(evtType interface{}, opts ...event.EmitterOpt) (e event.Emitter, err error) {
var settings emitterSettings var settings emitterSettings
for _, opt := range opts { for _, opt := range opts {
if err := opt(&settings); err != nil { if err := opt(&settings); err != nil {
return nil, err return nil, err
......
...@@ -25,6 +25,17 @@ func (EventA) String() string { ...@@ -25,6 +25,17 @@ func (EventA) String() string {
return "Oh, Hello" return "Oh, Hello"
} }
func TestDefaultSubIsBuffered(t *testing.T) {
bus := NewBus()
s, err := bus.Subscribe(new(EventA))
if err != nil {
t.Fatal(err)
}
if cap(s.(*sub).ch) == 0 {
t.Fatalf("without any options subscribe should be buffered. was %d", cap(s.(*sub).ch))
}
}
func TestEmit(t *testing.T) { func TestEmit(t *testing.T) {
bus := NewBus() bus := NewBus()
sub, err := bus.Subscribe(new(EventA)) sub, err := bus.Subscribe(new(EventA))
......
...@@ -4,6 +4,10 @@ type subSettings struct { ...@@ -4,6 +4,10 @@ type subSettings struct {
buffer int buffer int
} }
var subSettingsDefault = subSettings{
buffer: 16,
}
func BufSize(n int) func(interface{}) error { func BufSize(n int) func(interface{}) error {
return func(s interface{}) error { return func(s interface{}) error {
s.(*subSettings).buffer = n s.(*subSettings).buffer = n
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment