Categorygithub.com/lana/go-dispatcher
modulepackage
0.2.0
Repository: https://github.com/lana/go-dispatcher.git
Documentation: pkg.go.dev

# README

GO Dispatcher

Build Status Codecov branch GoDoc Go Report Card License

A simple event dispatcher made in Go.

Install

Use go get.

$ go get github.com/lana/go-dispatcher

Then import the package into your own code:

import "github.com/lana/go-dispacher"

Usage

package main

import (
	"context"
	"log"
	
	"github.com/lana/go-dispatcher"
)

type User struct {
	Name  string
	Email string
}

var UserWasCreated dispatcher.EventType = "user-was-created"

type UserCreated struct {
	User User
}

func (uc UserCreated) Type() dispatcher.EventType {
	return UserWasCreated
}

func (uc UserCreated) Data() interface{} {
	return uc.User
}

func OnUserCreated(ctx context.Context, e dispatcher.Event) {
	log.Printf("A user was created: %v", e.Data())
}

func main() {
	d := dispatcher.New()
	d.On(UserWasCreated, OnUserCreated)
	
	if err := d.Dispatch(context.Bachground(), e); err != nil {
		log.Fatalf("failed to dispatch event: %v", err)
	}
}

License

This project is released under the MIT licence. See LICENSE for more details.

# Functions

New creates a new Dispatcher instance.

# Interfaces

Dispatcher is an event dispatcher.
Event defines the type and the data of an event.

# Type aliases

EventType defines the kind of the dispatched event.
ListenerFunc is a function that can receive events.