mirror of
https://github.com/SecurityBrewery/catalyst.git
synced 2025-12-06 07:12:46 +01:00
43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package bus
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
|
|
"github.com/arangodb/go-driver"
|
|
emitter "github.com/emitter-io/go/v2"
|
|
)
|
|
|
|
const channelDatabaseUpdate = "databaseupdate"
|
|
|
|
type DatabaseUpdateType string
|
|
|
|
const (
|
|
DatabaseEntryRead DatabaseUpdateType = "read"
|
|
DatabaseEntryCreated DatabaseUpdateType = "created"
|
|
DatabaseEntryUpdated DatabaseUpdateType = "updated"
|
|
)
|
|
|
|
type DatabaseUpdateMsg struct {
|
|
IDs []driver.DocumentID `json:"ids"`
|
|
Type DatabaseUpdateType `json:"type"`
|
|
}
|
|
|
|
func (b *Bus) PublishDatabaseUpdate(ids []driver.DocumentID, databaseUpdateType DatabaseUpdateType) error {
|
|
return b.jsonPublish(&DatabaseUpdateMsg{
|
|
IDs: ids,
|
|
Type: databaseUpdateType,
|
|
}, channelDatabaseUpdate, b.config.databaseUpdateBusKey)
|
|
}
|
|
|
|
func (b *Bus) SubscribeDatabaseUpdate(f func(msg *DatabaseUpdateMsg)) error {
|
|
return b.safeSubscribe(b.config.databaseUpdateBusKey, channelDatabaseUpdate, func(c *emitter.Client, m emitter.Message) {
|
|
var msg DatabaseUpdateMsg
|
|
if err := json.Unmarshal(m.Payload(), &msg); err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
go f(&msg)
|
|
})
|
|
}
|