diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-24 12:58:17 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-24 12:58:17 -0600 |
| commit | 586f2ba82c98cd46b339f8322e4eab0fa5594f71 (patch) | |
| tree | 7761c14ce70fff105621c6275179b7f0660ea71a | |
| parent | 95c1a8f301745897e4dcd0f6b8367e6f85dd2080 (diff) | |
refactor: rename NewAggregator to New
| -rw-r--r-- | pkg/event/typed_aggregator.go | 16 | ||||
| -rw-r--r-- | pkg/event/typed_aggregator_test.go | 6 |
2 files changed, 13 insertions, 9 deletions
diff --git a/pkg/event/typed_aggregator.go b/pkg/event/typed_aggregator.go index 5572372..1fecb0f 100644 --- a/pkg/event/typed_aggregator.go +++ b/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) + } + } +} diff --git a/pkg/event/typed_aggregator_test.go b/pkg/event/typed_aggregator_test.go index 259c172..9781f55 100644 --- a/pkg/event/typed_aggregator_test.go +++ b/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) { |
