owl-blogs/app/event_bus.go

52 lines
1.1 KiB
Go
Raw Permalink Normal View History

2023-07-22 18:34:17 +00:00
package app
import "owl-blogs/domain/model"
2023-08-11 13:14:38 +00:00
type Subscriber interface{}
type EntryCreatedSubscriber interface {
NotifyEntryCreated(entry model.Entry)
2023-07-22 18:34:17 +00:00
}
2023-08-11 13:14:38 +00:00
type EntryUpdatedSubscriber interface {
NotifyEntryUpdated(entry model.Entry)
}
type EntryDeletedSubscriber interface {
NotifyEntryDeleted(entry model.Entry)
}
type EventBus struct {
subscribers []Subscriber
2023-07-22 18:34:17 +00:00
}
func NewEventBus() *EventBus {
2023-08-11 13:14:38 +00:00
return &EventBus{subscribers: make([]Subscriber, 0)}
2023-07-22 18:34:17 +00:00
}
2023-08-11 13:14:38 +00:00
func (b *EventBus) Subscribe(subscriber Subscriber) {
2023-07-22 18:34:17 +00:00
b.subscribers = append(b.subscribers, subscriber)
}
2023-08-11 13:14:38 +00:00
func (b *EventBus) NotifyCreated(entry model.Entry) {
for _, subscriber := range b.subscribers {
if sub, ok := subscriber.(EntryCreatedSubscriber); ok {
go sub.NotifyEntryCreated(entry)
}
}
}
func (b *EventBus) NotifyUpdated(entry model.Entry) {
for _, subscriber := range b.subscribers {
if sub, ok := subscriber.(EntryUpdatedSubscriber); ok {
go sub.NotifyEntryUpdated(entry)
}
}
}
func (b *EventBus) NotifyDeleted(entry model.Entry) {
2023-07-22 18:34:17 +00:00
for _, subscriber := range b.subscribers {
2023-08-11 13:14:38 +00:00
if sub, ok := subscriber.(EntryDeletedSubscriber); ok {
go sub.NotifyEntryDeleted(entry)
}
2023-07-22 18:34:17 +00:00
}
}