summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-07-24 12:46:32 -0600
committermo khan <mo@mokhan.ca>2025-07-24 12:46:32 -0600
commitda928b0dbee3afd4042cd08ef45e6f03c41f47e0 (patch)
treed170ede08d6a4238117c8d5ceed0836c09dc01a3
parentc089c3f34e367692879826859bda839ef233a1b1 (diff)
refactor: start to whittle away at constructors
-rw-r--r--pkg/event/aggregator.go19
-rw-r--r--pkg/event/event.go3
-rw-r--r--pkg/event/subscription.go3
-rw-r--r--pkg/event/typed_aggregator.go15
4 files changed, 30 insertions, 10 deletions
diff --git a/pkg/event/aggregator.go b/pkg/event/aggregator.go
index 5b01877..0beaf86 100644
--- a/pkg/event/aggregator.go
+++ b/pkg/event/aggregator.go
@@ -1,16 +1,25 @@
package event
-type Event any
-type Subscription func(any)
+import "github.com/xlgmokha/x/pkg/x"
type Aggregator struct {
subscriptions map[Event][]Subscription
}
func New() *Aggregator {
- return &Aggregator{
- subscriptions: map[Event][]Subscription{},
- }
+ return x.New[*Aggregator](
+ WithoutSubscriptions(),
+ )
+}
+
+func WithoutSubscriptions() x.Option[*Aggregator] {
+ return WithSubscriptions(map[Event][]Subscription{})
+}
+
+func WithSubscriptions(subscriptions map[Event][]Subscription) x.Option[*Aggregator] {
+ return x.With(func(item *Aggregator) {
+ item.subscriptions = subscriptions
+ })
}
func (a *Aggregator) Subscribe(event Event, f Subscription) {
diff --git a/pkg/event/event.go b/pkg/event/event.go
new file mode 100644
index 0000000..a1ff812
--- /dev/null
+++ b/pkg/event/event.go
@@ -0,0 +1,3 @@
+package event
+
+type Event any
diff --git a/pkg/event/subscription.go b/pkg/event/subscription.go
new file mode 100644
index 0000000..0a76efc
--- /dev/null
+++ b/pkg/event/subscription.go
@@ -0,0 +1,3 @@
+package event
+
+type Subscription func(any)
diff --git a/pkg/event/typed_aggregator.go b/pkg/event/typed_aggregator.go
index c788756..4932531 100644
--- a/pkg/event/typed_aggregator.go
+++ b/pkg/event/typed_aggregator.go
@@ -1,17 +1,23 @@
package event
+import "github.com/xlgmokha/x/pkg/x"
+
type TypedAggregator[T any] struct {
aggregator *Aggregator
}
func NewAggregator[T any]() *TypedAggregator[T] {
- return NewWith[T](New())
+ return NewWith[T](x.New(WithoutSubscriptions()))
}
func NewWith[T any](aggregator *Aggregator) *TypedAggregator[T] {
- return &TypedAggregator[T]{
- aggregator: aggregator,
- }
+ return x.New[*TypedAggregator[T]](WithAggregator[T](aggregator))
+}
+
+func WithAggregator[T any](aggregator *Aggregator) x.Option[*TypedAggregator[T]] {
+ return x.With(func(item *TypedAggregator[T]) {
+ item.aggregator = aggregator
+ })
}
func (a *TypedAggregator[T]) SubscribeTo(event Event, f func(T)) {
@@ -20,7 +26,6 @@ func (a *TypedAggregator[T]) SubscribeTo(event Event, f func(T)) {
f(item)
}
})
-
}
func (a *TypedAggregator[T]) Publish(event Event, message T) {