Skip to content

Commit

Permalink
Merge pull request #15 from tonybka/feat/remove-event-after-publish
Browse files Browse the repository at this point in the history
feat: Clean up event after publishing
  • Loading branch information
tonybka authored Jun 22, 2024
2 parents 885e910 + 0c51a8d commit ee0a0c7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 4 additions & 0 deletions domain/event/event_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ func (storage *EventStorage) AddEvent(dataModel string, domainEvent IBaseDomainE
func (storage *EventStorage) GetPendingEvents(dataModel string) []IBaseDomainEvent {
return storage.pendingEvents[dataModel]
}

func (storage *EventStorage) CleanupPendingEvents(dataModel string) {
storage.pendingEvents[dataModel] = nil
}
13 changes: 7 additions & 6 deletions persistence/trigger/model_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (
// modelHooks get called whenever there is new create-or-update event triggered
func modelHooks(pid uint, table string, rowID uint, action string) error {
pendingEvents := event.EventSource.GetPendingEvents(table)

publisher := event.GetDomainEventPublisher()
err := publisher.Publish(pendingEvents...)
if err != nil {
return err
if len(pendingEvents) > 0 {
publisher := event.GetDomainEventPublisher()
err := publisher.Publish(pendingEvents...)
if err != nil {
return err
}
event.EventSource.CleanupPendingEvents(table)
}

return nil
}

Expand Down

0 comments on commit ee0a0c7

Please sign in to comment.