Git Product home page Git Product logo

eventbus-go's Introduction

EventBus

EventBus (eventbus-go) is a small, lightweight, zero-dependency library for sending events/messages between components/objects in an application without them being directly coupled. Components are only coupled to the eventbus rather than to each other.

EventBus uses the type system for mapping events to handlers. Events are modeled as types. This provides a lot of flexibility for passing arbitrary data on events throughout an application.

Example:

package main

import (
	"fmt"

	"github.com/jkratz55/eventbus-go"
)

type Status string

const (
	StatusUp       Status = "UP"
	StatusDegraded Status = "DEGRADED"
	StatusDown     Status = "DOWN"
)

type ApplicationHealthChangedEvent struct {
	NewStatus string
}

func main() {

	// Create a handler for the event. This just needs to be an implementation
	// of eventbus.Handler interface, which can be a type or a eventbus.HandlerFunc.
	handlerFn := eventbus.HandlerFunc[ApplicationHealthChangedEvent](func(event ApplicationHealthChangedEvent) {
		fmt.Println("Application status changed to", event.NewStatus)
		// todo: do something useful with the event
	})

	// Register the handler for the event type ApplicationHealthChangedEvent
	eventbus.Subscribe[ApplicationHealthChangedEvent](handlerFn)

	// Publish some events
	eventbus.MustPublish(ApplicationHealthChangedEvent{NewStatus: string(StatusDegraded)})
	eventbus.MustPublish(ApplicationHealthChangedEvent{NewStatus: string(StatusUp)})
}

The above code will generate the following output:

Application status changed to DEGRADED
Application status changed to UP

In some cases an event may not need to carry additional data, in that case we can use an empty struct.

package main

import (
	"fmt"

	"github.com/jkratz55/eventbus-go"
)

type ApplicationConfigChangedEvent struct{}

func main() {

	// Create a handler for the event. This just needs to be an implementation
	// of eventbus.Handler interface, which can be a type or a eventbus.HandlerFunc.
	handlerFn := eventbus.HandlerFunc[ApplicationConfigChangedEvent](func(event ApplicationConfigChangedEvent) {
		fmt.Println("Application config changed")
		// todo: do something useful like reload or update based on new config
	})

	// Register the handler for the event type ApplicationHealthChangedEvent
	eventbus.Subscribe[ApplicationConfigChangedEvent](handlerFn)

	// Publish some events
	eventbus.MustPublish(ApplicationConfigChangedEvent{})
	eventbus.MustPublish(ApplicationConfigChangedEvent{})
}

The recommended approach is to model your events as explicit as possible using good names like CustomerCreated or CustomerDeleted. However, the following is valid code, it's just as clear to understand at glance, and as more conditions are added ensuring everywhere the event is handled is updated can be a challenge.

package main

import (
	"fmt"

	"github.com/jkratz55/eventbus-go"
)

type DatabaseStatusEvent int

const (
	DatabaseUp DatabaseStatusEvent = iota
	DatabaseDown
	DatabaseDegraded
)

func main() {

	// Create a handler for the event. This just needs to be an implementation
	// of eventbus.Handler interface, which can be a type or a eventbus.HandlerFunc.
	handlerFn := eventbus.HandlerFunc[DatabaseStatusEvent](func(event DatabaseStatusEvent) {
		fmt.Println("database status event")
		switch event {
		case DatabaseUp:
			fmt.Println("Database is up")
		case DatabaseDown:
			fmt.Println("Database is down")
		case DatabaseDegraded:
			fmt.Println("Database is degraded")
		}
	})

	// Register the handler for the event type ApplicationHealthChangedEvent
	eventbus.Subscribe[DatabaseStatusEvent](handlerFn)

	// Publish some events
	eventbus.MustPublish(DatabaseDegraded)
	eventbus.MustPublish(DatabaseDown)
	eventbus.MustPublish(DatabaseUp)
}

eventbus-go's People

Contributors

jkratz55 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.