Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V4 updates #4

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Simple event bus written in go
# Installation

```shell
go get -u github.com/dcarbone/seb/v3
go get -u github.com/dcarbone/seb/v4
```

# Global Bus Usage
Expand All @@ -16,21 +16,25 @@ import (
"context"
"fmt"

"github.com/dcarbone/seb/v3"
"github.com/dcarbone/seb/v4"
)

func eventHandler(ev seb.Event) {
func eventHandler(ev *seb.Event[any]) {
fmt.Println("Event received:", ev)
}

func main() {
seb.AttachFunc("", eventHandler)

err := seb.Push(context.Background(), "topic-1", map[string]string{"hello": "dave"})
id, replaced, err := seb.AttachFunc("", eventHandler)
if err != nil {
panic(err.Error())
}
if replaced {
fmt.Printf("overwrote existing handler for %q\n", id)
}

fmt.Println("My handler registration ID is:", id)

seb.Push(context.Background(), "topic-1", map[string]string{"hello": "dave"})
// and so on.
}

Expand All @@ -45,25 +49,30 @@ import (
"context"
"fmt"

"github.com/dcarbone/seb/v3"
"github.com/dcarbone/seb/v4"
)

func eventHandler(ev seb.Event) {
func eventHandler(ev *seb.Event[map[string]string]) {
fmt.Println("Event received:", ev)
}

func main() {
bus := seb.New(
bus := seb.New[map[string]string](
// apply bus options here...
)

bus.AttachFunc("", eventHandler)

err := bus.Push(context.Background(), "topic-1", map[string]string{"hello": "dave"})
id, replaced, err := bus.AttachFunc("", eventHandler)
if err != nil {
panic(err.Error())
}

if replaced {
fmt.Printf("overwrote existing handler for %q\n", id)
}

fmt.Println("My handler registration ID is:", id)

bus.Push(context.Background(), "topic-1", map[string]string{"hello": "dave"})

// and so on.
}
```
Expand Down
33 changes: 33 additions & 0 deletions global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package seb

import (
"context"
)

var (
anyBus *Bus[any]
)

func init() {
anyBus = New[any]()
}

// AttachFunc attaches an event recipient func to the global bus
func AttachFunc(id string, fn EventFunc[any], topicFilters ...any) (string, bool, error) {
return anyBus.AttachFunc(id, fn, topicFilters...)
}

// AttachChannel attaches an event recipient channel to the global bus
func AttachChannel(id string, ch EventChannel[any], topicFilters ...any) (string, bool, error) {
return anyBus.AttachChannel(id, ch, topicFilters...)
}

// Push pushes an event onto the global bus
func Push[T any](ctx context.Context, topic string, data T) {
anyBus.Push(ctx, topic, data)
}

// PushTo attempts to push an event to a particular recipient
func PushTo[T any](ctx context.Context, to, topic string, data T) error {
return anyBus.PushTo(ctx, to, topic, data)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/dcarbone/seb/v3
module github.com/dcarbone/seb/v4

go 1.21
Loading
Loading