GithubHelp home page GithubHelp logo

event-driven's Introduction

Implementing an Event Driven System in Go

refer: event-driven-system

Define Base Events

// Event [T] is the base event
type Event[T any] struct {
	handlers []interface{ Handle(T) }
}

// Register adds an event handler for this event
func (e *Event[T]) Register(handler interface{ Handle(T) }) {
	e.handlers = append(e.handlers, handler)
}

// Trigger sends out an event with the payload
func (e *Event[T]) Trigger(payload T) {
	for _, handler := range e.handlers {
		handler.Handle(payload)
	}
}
  • Functions are first-class values
// EventHandleFunc
type HandleFunc[T any] func(T)

// implement Handle interface
func (h HandleFunc[T]) Handle(payload T) {
	h(payload)
}

// RegisterFunc adds an event handler func for this event
func (e *Event[T]) RegisterFunc(fn HandleFunc[T]) {
	e.handlers = append(e.handlers, fn)
}

Implement Events

// SamplePayload is the data for sampleEvent
type SamplePayload struct {
	Name string
}

// In sampleEvent SampleEventPayload is pass by value
type sampleEvent struct {
	Event[SamplePayload]
}

// In sampleRefEvent SampleEventPayload is pass by reference
type sampleRefEvent struct {
	Event[*SamplePayload]
}

// Define event instances
var (
    SampleEvent sampleEvent
    SampleRefEvent sampleRefEvent
)

Register handler for events

type sampleNotifier struct {
}

// implement the Handle interface
func (sampleNotifier) Handle(payload event.SamplePayload) {
	fmt.Println("sampleNotifier Handle", payload.Name, fmt.Sprintf("%p", &payload))
}

// Register events in init()
func init() {
	notifier := sampleNotifier{}
	event.SampleEvent.Register(notifier)

	event.SampleEvent.RegisterFunc(func(payload event.SamplePayload) {
		fmt.Println("FuncHandle", payload.Name, fmt.Sprintf("%p", payload))
	})
}

Triggering events

func Publish() {
	event.SampleEvent.Trigger(event.SamplePayload{
		Name: "Sample",
	})
}

func main() {
	Publish()
}

Examples

event-driven's People

Contributors

ryanpenn avatar

Stargazers

 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.