Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions event_bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ func (bus *EventBus) Publish(topic string, args ...interface{}) {
// so make a copy and iterate the copied slice.
copyHandlers := make([]*eventHandler, 0, len(handlers))
copyHandlers = append(copyHandlers, handlers...)
for i, handler := range copyHandlers {
for _, handler := range copyHandlers {
if handler.flagOnce {
bus.removeHandler(topic, i)
bus.removeHandler(topic, bus.findHandlerIdx(topic, handler.callBack))
}
if !handler.async {
bus.doPublish(handler, topic, args...)
Expand Down
17 changes: 17 additions & 0 deletions event_bus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,20 @@ func TestSubscribeAsync(t *testing.T) {
t.Fail()
}
}

func TestManySubscribeOnce(t *testing.T) {
bus := New()
event := "topic"
var flags [3]byte

bus.SubscribeOnce(event, func() { flags[0]++ })
bus.SubscribeOnce(event, func() { flags[1]++ })
bus.Subscribe(event, func() { flags[2]++ })

bus.Publish(event)
bus.Publish(event)

if flags != [3]byte{1, 1, 2} {
t.Fail()
}
}