Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

webmafia/bus

Repository files navigation

DEPRECATED

Please use webmafia/ebus instead.

Install

go get github.com/webmafia/bus

Usage

const (
	Created bus.Topic = iota
	Updated
	Deleted
)

type User struct {
	Id   int
	Name string
}

// Create a new event bus with a buffer of 32 events
b := bus.NewBus(context.Background(), 32)

// Subscribe for created users
bus.SubVal(b, Created, func(ctx context.Context, u *User) {
	log.Printf("User %d was created", u.Id)
})

// Subscribe for updated users
bus.SubVal(b, Updated, func(ctx context.Context, u *User) {
	log.Printf("User %d was updated", u.Id)
})

// Publish to Created topic. This will always do 1 allocation due to the pointer.
bus.SubVal(b, Created, &User{
	Id:   123,
	Name: "John Doe"
})