-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae7ea50
commit 88825ab
Showing
8 changed files
with
177 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package game | ||
|
||
type EventTag int | ||
|
||
const ( | ||
EventStateChanged EventTag = iota | ||
) | ||
|
||
type Event struct { | ||
Tag EventTag | ||
Data interface{} | ||
} | ||
|
||
type Subscription struct { | ||
Events chan Event | ||
} | ||
|
||
type EventPublisher interface { | ||
Publish(tag EventTag, data interface{}) | ||
} | ||
|
||
type EventSubscriber interface { | ||
Subscribe(tag EventTag) *Subscription | ||
} | ||
|
||
type EventManager struct { | ||
subscriptions []*Subscription | ||
} | ||
|
||
func NewEventManager() *EventManager { | ||
return &EventManager{ | ||
subscriptions: make([]*Subscription, 0, 1), | ||
} | ||
} | ||
|
||
func (m *EventManager) Send(event Event) { | ||
for _, sub := range m.subscriptions { | ||
sub.Events <- event | ||
} | ||
} | ||
|
||
func (m *EventManager) Subscribe() *Subscription { | ||
subscription := &Subscription{ | ||
Events: make(chan Event, 10), | ||
} | ||
m.subscriptions = append(m.subscriptions, subscription) | ||
return subscription | ||
} | ||
|
||
func (m *EventManager) Count() int { | ||
return len(m.subscriptions) | ||
} | ||
|
||
func (m *EventManager) Close() { | ||
for _, sub := range m.subscriptions { | ||
close(sub.Events) | ||
} | ||
m.subscriptions = nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package game | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/brianvoe/gofakeit/v6" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestEventManager(t *testing.T) { | ||
const subscribersCount = 3 | ||
manager := NewEventManager() | ||
|
||
subs := make([]Subscription, subscribersCount) | ||
for i := range subs { | ||
subs[i] = *manager.Subscribe() | ||
} | ||
|
||
require.Equal(t, subscribersCount, manager.Count()) | ||
|
||
event := Event{} | ||
err := gofakeit.Struct(&event) | ||
require.NoError(t, err) | ||
|
||
manager.Send(event) | ||
|
||
for i := range subs { | ||
require.Equal(t, event, <-subs[i].Events) | ||
} | ||
|
||
manager.Close() | ||
require.Empty(t, manager.subscriptions) | ||
|
||
for i := range subs { | ||
_, ok := <-subs[i].Events | ||
require.False(t, ok) | ||
} | ||
|
||
require.Zero(t, manager.Count()) | ||
} |
Oops, something went wrong.