Skip to content

Commit

Permalink
add eventbridge example (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
fogfish authored Nov 18, 2023
1 parent 5aaedfc commit eae5c21
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
27 changes: 27 additions & 0 deletions examples/eventbridge/dequeue/eventbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
package main

import (
"encoding/json"
"fmt"
"log/slog"

"github.com/fogfish/swarm"
"github.com/fogfish/swarm/broker/eventbridge"
"github.com/fogfish/swarm/internal/qtest"
"github.com/fogfish/swarm/queue"
"github.com/fogfish/swarm/queue/events"
)

type User struct {
Expand All @@ -32,6 +35,11 @@ type Like struct {
Text string `json:"text"`
}

type EventNote swarm.Event[*Note]

func (EventNote) HKT1(swarm.EventType) {}
func (EventNote) HKT2(*Note) {}

func main() {
qtest.NewLogger()

Expand All @@ -40,6 +48,7 @@ func main() {
go actor[User]("user").handle(queue.Dequeue[User](q))
go actor[Note]("note").handle(queue.Dequeue[Note](q))
go actor[Like]("like").handle(queue.Dequeue[Like](q))
go ebus(events.Dequeue[*Note, EventNote](q))

q.Await()
}
Expand All @@ -52,3 +61,21 @@ func (a actor[T]) handle(rcv <-chan *swarm.Msg[T], ack chan<- *swarm.Msg[T]) {
ack <- msg
}
}

func ebus(rcv <-chan *EventNote, ack chan<- *EventNote) {
for msg := range rcv {
prefix := ""
switch string(msg.Type) {
case "note:EventCreateNote":
prefix = "+ |"
case "note:EventUpdateNote":
prefix = "~ |"
case "note:EventRemoveNote":
prefix = "- |"
}

v, _ := json.MarshalIndent(msg, prefix, " ")
fmt.Printf("common note > \n %s\n", v)
ack <- msg
}
}
30 changes: 30 additions & 0 deletions examples/eventbridge/enqueue/eventbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/fogfish/swarm/broker/eventbridge"
"github.com/fogfish/swarm/internal/qtest"
"github.com/fogfish/swarm/queue"
"github.com/fogfish/swarm/queue/events"
)

type User struct {
Expand All @@ -30,6 +31,11 @@ type Like struct {
Text string `json:"text"`
}

type EventNote swarm.Event[*Note]

func (EventNote) HKT1(swarm.EventType) {}
func (EventNote) HKT2(*Note) {}

func main() {
qtest.NewLogger()

Expand All @@ -41,10 +47,34 @@ func main() {
user := swarm.LogDeadLetters(queue.Enqueue[*User](q))
note := swarm.LogDeadLetters(queue.Enqueue[*Note](q))
like := swarm.LogDeadLetters(queue.Enqueue[*Like](q))
ebus := swarm.LogDeadLetters(events.Enqueue[*Note, EventNote](q))

user <- &User{ID: "user", Text: "some text"}
note <- &Note{ID: "note", Text: "some text"}
like <- &Like{ID: "like", Text: "some text"}

//
// Single channel emits event
ebus <- &EventNote{
Type: "note:EventCreateNote",
Agent: "example",
Participant: "user",
Object: &Note{ID: "note", Text: "some text"},
}

ebus <- &EventNote{
Type: "note:EventUpdateNote",
Agent: "example",
Participant: "user",
Object: &Note{ID: "note", Text: "some text with changes"},
}

ebus <- &EventNote{
Type: "note:EventRemoveNote",
Agent: "example",
Participant: "user",
Object: &Note{ID: "note"},
}

q.Close()
}

0 comments on commit eae5c21

Please sign in to comment.