Skip to content

Commit

Permalink
Remove embedded type (#2)
Browse files Browse the repository at this point in the history
Co-authored-by: theskyinflames-macos <theskyinflames-macos@MacBook-MacBook-Pro-de-theskyinflames-macos.local>
  • Loading branch information
theskyinflames and theskyinflames-macos authored Feb 8, 2023
1 parent 4d3ea4b commit 7dae170
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions publisher/internal/domain/subscribers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,57 @@ import (

"github.com/google/uuid"
"github.com/theskyinflames/cqrs-eda/pkg/ddd"
"github.com/theskyinflames/cqrs-eda/pkg/events"
)

// Subscriber is self-described
type Subscriber struct {
ddd.AggregateBasic
ab ddd.AggregateBasic
net.Conn
}

// NewSubscriber is a constructor
func NewSubscriber(ID uuid.UUID, conn net.Conn) Subscriber {
return Subscriber{
AggregateBasic: ddd.NewAggregateBasic(ID),
Conn: conn,
ab: ddd.NewAggregateBasic(ID),
Conn: conn,
}
}

// ID returns the underlying ddd.AggregateBasic ID
func (s Subscriber) ID() uuid.UUID {
return s.ab.ID()
}

// Subscribers is the set of subscribers
type Subscribers struct {
ddd.AggregateBasic
ab ddd.AggregateBasic
mux *sync.RWMutex
subscribers map[uuid.UUID]Subscriber
}

// NewSubscribers is a constructor
func NewSubscribers(ID uuid.UUID) Subscribers {
return Subscribers{
AggregateBasic: ddd.NewAggregateBasic(ID),
mux: &sync.RWMutex{},
subscribers: make(map[uuid.UUID]Subscriber),
ab: ddd.NewAggregateBasic(ID),
mux: &sync.RWMutex{},
subscribers: make(map[uuid.UUID]Subscriber),
}
}

// Events returns subscribers events
func (s Subscribers) Events() []events.Event {
return s.ab.Events()
}

// Add adds a new subscriber
func (s *Subscribers) Add(subscriber Subscriber) {
s.mux.Lock()
defer s.mux.Unlock()

s.subscribers[subscriber.ID()] = subscriber

s.RecordEvent(NewSubscriberAddedEvent(subscriber.ID()))
s.ab.RecordEvent(NewSubscriberAddedEvent(subscriber.ID()))
}

// Remove removes a subscriber
Expand All @@ -57,7 +68,7 @@ func (s *Subscribers) Remove(conn net.Conn) {
for _, subscriber := range s.subscribers {
if subscriber.Conn.RemoteAddr().String() == conn.RemoteAddr().String() {
delete(s.subscribers, subscriber.ID())
s.RecordEvent(NewSubscriberRemovedEvent(subscriber.ID()))
s.ab.RecordEvent(NewSubscriberRemovedEvent(subscriber.ID()))
}
}
}
Expand Down

0 comments on commit 7dae170

Please sign in to comment.