Skip to content

Commit 0c51a8d

Browse files
committed
feat: Clean up event after publishing
1 parent 885e910 commit 0c51a8d

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

domain/event/event_storage.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ func (storage *EventStorage) AddEvent(dataModel string, domainEvent IBaseDomainE
2525
func (storage *EventStorage) GetPendingEvents(dataModel string) []IBaseDomainEvent {
2626
return storage.pendingEvents[dataModel]
2727
}
28+
29+
func (storage *EventStorage) CleanupPendingEvents(dataModel string) {
30+
storage.pendingEvents[dataModel] = nil
31+
}

persistence/trigger/model_hooks.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import (
1111
// modelHooks get called whenever there is new create-or-update event triggered
1212
func modelHooks(pid uint, table string, rowID uint, action string) error {
1313
pendingEvents := event.EventSource.GetPendingEvents(table)
14-
15-
publisher := event.GetDomainEventPublisher()
16-
err := publisher.Publish(pendingEvents...)
17-
if err != nil {
18-
return err
14+
if len(pendingEvents) > 0 {
15+
publisher := event.GetDomainEventPublisher()
16+
err := publisher.Publish(pendingEvents...)
17+
if err != nil {
18+
return err
19+
}
20+
event.EventSource.CleanupPendingEvents(table)
1921
}
20-
2122
return nil
2223
}
2324

0 commit comments

Comments
 (0)