Skip to content

Commit

Permalink
Adding functionality to unsubscribe from events. (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
infiniteyak authored Dec 29, 2022
1 parent d5a1431 commit 7bf72f0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
25 changes: 24 additions & 1 deletion features/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func NewEventType[T any]() *EventType[T] {
return e
}

// RegisterHandler registers a subscriber for the event.
// Subscribe registers a subscriber for the event.
func (e *EventType[T]) Subscribe(w donburi.World, subscriber Subscriber[T]) {
if e.eventBusQuery.Count(w) == 0 {
entity := w.Entry(w.Create(e.eventBus, eventType))
Expand All @@ -77,6 +77,29 @@ func (e *EventType[T]) Subscribe(w donburi.World, subscriber Subscriber[T]) {
eventBus.subscribers = append(eventBus.subscribers, subscriber)
}

// Find the index of a subscriber
func (e *EventType[T]) findSubscriber(w donburi.World, subscriber Subscriber[T]) (int, bool) {
if e.eventBusQuery.Count(w) != 0 {
eventBus := e.mustFindEventBus(w)
for i, s := range eventBus.subscribers {
if reflect.ValueOf(s).Pointer() == reflect.ValueOf(subscriber).Pointer() {
return i, true
}
}
}
return 0, false
}

// Unsubscribe removes a subscriber for the event.
func (e *EventType[T]) Unsubscribe(w donburi.World, subscriber Subscriber[T]) {
index, found := e.findSubscriber(w, subscriber)
if found {
eventBus := e.mustFindEventBus(w)
eventBus.subscribers[index] = eventBus.subscribers[len(eventBus.subscribers)-1]
eventBus.subscribers = eventBus.subscribers[:len(eventBus.subscribers)-1]
}
}

// Publish publishes an event.
func (e *EventType[T]) Publish(w donburi.World, event T) {
eventBus := e.mustFindEventBus(w)
Expand Down
30 changes: 30 additions & 0 deletions features/events/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,36 @@ func TestEvents(t *testing.T) {
}
}

func TestUnsubscribe(t *testing.T) {

w := donburi.NewWorld()

EnemyKilledEvent.Subscribe(w, HandleEnemyKilled)
EnemyKilledEvent.Publish(w, &EnemyKilled{EnemyID: 1})

events.ProcessAllEvents(w)

ev := lastReceivedEvent

if ev == nil {
t.Errorf("event should be received")
}

if ev.EnemyID != 1 {
t.Errorf("event should have value 1")
}

lastReceivedEvent = nil
EnemyKilledEvent.Unsubscribe(w, HandleEnemyKilled)
EnemyKilledEvent.Publish(w, &EnemyKilled{EnemyID: 1})

events.ProcessAllEvents(w)

if lastReceivedEvent != nil {
t.Errorf("event should not be received")
}
}

var (
lastReceivedEvent *EnemyKilled = nil
)
Expand Down

0 comments on commit 7bf72f0

Please sign in to comment.