Skip to content

Latest commit

 

History

History
50 lines (36 loc) · 910 Bytes

README.md

File metadata and controls

50 lines (36 loc) · 910 Bytes

event

A Go event management package.

Installation

go get github.com/kodeyeen/event

Quickstart

package main

import (
	"context"
	"fmt"

	"github.com/kodeyeen/event"
)

// Declare event payload.
// It's ok to name it with Event suffix though it's not the event itself, but its payload.
type UserRegisteredEvent struct {
	Username string
}

func main() {
	// Create an event dispatcher.
	dispr := event.NewDispatcher()

	// Register an event listener.
	dispr.ListenFunc("user.registered", func(ctx context.Context, e event.Event) error {
		p := e.Payload().(*UserRegisteredEvent)

		fmt.Printf("New user: %s\n", p.Username)

		return nil
	})

	// Create an event to be dispatched
	userRegisteredEvt := event.WithPayload("user.registered", &UserRegisteredEvent{
		Username: "kodeyeen",
	})

	// Dispatch the event.
	dispr.HandleEvent(context.Background(), userRegisteredEvt)
}