Commit 586f2ba
Changed files (2)
pkg
pkg/event/typed_aggregator.go
@@ -6,7 +6,7 @@ type TypedAggregator[T any] struct {
aggregator *Aggregator
}
-func NewAggregator[T any]() *TypedAggregator[T] {
+func New[T any]() *TypedAggregator[T] {
return x.New[*TypedAggregator[T]](
WithAggregator[T](
x.New(
@@ -27,13 +27,17 @@ func WithAggregator[T any](aggregator *Aggregator) x.Option[*TypedAggregator[T]]
}
func (a *TypedAggregator[T]) SubscribeTo(event Event, f func(T)) {
- a.aggregator.Subscribe(event, func(message any) {
- if item, ok := message.(T); ok {
- f(item)
- }
- })
+ a.aggregator.Subscribe(event, a.mapFrom(f))
}
func (a *TypedAggregator[T]) Publish(event Event, message T) {
a.aggregator.Publish(event, message)
}
+
+func (a *TypedAggregator[T]) mapFrom(f func(T)) Subscription {
+ return func(message any) {
+ if item, ok := message.(T); ok {
+ f(item)
+ }
+ }
+}
pkg/event/typed_aggregator_test.go
@@ -13,7 +13,7 @@ func TestTypedAggregator(t *testing.T) {
t.Run("Publish", func(t *testing.T) {
t.Run("without any subscribers", func(t *testing.T) {
- aggregator := NewAggregator[announcement]()
+ aggregator := New[announcement]()
aggregator.Publish("announcement", announcement{
message: "Business, Business, Business... Numbers!",
@@ -22,7 +22,7 @@ func TestTypedAggregator(t *testing.T) {
t.Run("with a single subscription", func(t *testing.T) {
called := false
- aggregator := NewAggregator[announcement]()
+ aggregator := New[announcement]()
aggregator.SubscribeTo("announcement", func(payload announcement) {
called = true
@@ -35,7 +35,7 @@ func TestTypedAggregator(t *testing.T) {
})
t.Run("with multiple subscribers", func(t *testing.T) {
- aggregator := NewAggregator[announcement]()
+ aggregator := New[announcement]()
called := map[int]bool{}
aggregator.SubscribeTo("announcement", func(payload announcement) {