bus

package
v0.0.0-...-bcea9b7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 10, 2021 License: MIT Imports: 3 Imported by: 0

README

Usage

Bus it is a simple but powerful publish-subscribe event system. It requires object to register themselves with the event bus to receive events.

Example

package main 

import (
	"github.com/donutloop/toolkit/bus"
	"log"
)

type msg struct {
	Id      int64
	counter int
}

func main() {
	b := bus.New()

    b.AddEventListener(func(m *msg) error {
        m.counter++
        return nil
    })

    b.AddEventListener(func(m *msg) error {
        m.counter++
        return nil
    })

    b.Publish(new(msg))
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bus

type Bus interface {
	Dispatch(msg Msg) error
	AddHandler(handler HandlerFunc) error
	AddEventListener(handler HandlerFunc)
	Publish(msg Msg) error
}

It is a simple but powerful publish-subscribe event system. It requires object to register themselves with the event bus to receive events.

func New

func New() Bus

type ErrBadFuncError

type ErrBadFuncError string

ErrBadFuncError is raised via panic() when AddEventListener or AddHandler is called with an invalid listener function.

func (ErrBadFuncError) Error

func (bhf ErrBadFuncError) Error() string

type ErrHandlerNotFound

type ErrHandlerNotFound struct {
	Name string
}

func (*ErrHandlerNotFound) Error

func (e *ErrHandlerNotFound) Error() string

type ErrOverwrite

type ErrOverwrite struct {
	Name string
}

func (*ErrOverwrite) Error

func (e *ErrOverwrite) Error() string

type HandlerFunc

type HandlerFunc interface{}

The type of the function's first and only argument. declares the msg to listen for.

type InProcBus

type InProcBus struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*InProcBus) AddEventListener

func (b *InProcBus) AddEventListener(handler HandlerFunc)

AddListener registers a listener function that will be called when a matching msg is dispatched.

Example

Creates a bus and adds a listener to a message afterward it publishes a new message.

package main

import (
	"fmt"

	"github.com/donutloop/toolkit/bus"
)

func main() {
	type msg struct {
		ID   int64
		body string
	}

	b := bus.New()

	b.AddEventListener(func(m *msg) error {
		fmt.Println("db insert listener id", m.ID)
		return nil
	})

	m := new(msg)
	m.ID = 1
	m.body = "test"

	if err := b.Publish(m); err != nil {
		fmt.Printf("error: (%v) \n", err)
	}

}
Output:

db insert listener id 1

func (*InProcBus) AddHandler

func (b *InProcBus) AddHandler(handler HandlerFunc) error

AddHandler registers a handler function that will be called when a matching msg is dispatched.

Example

Creates a bus and adds a handler for a message afterward it dispatch a new message.

package main

import (
	"fmt"

	"github.com/donutloop/toolkit/bus"
)

func main() {
	type msg struct {
		ID   int64
		body string
	}

	b := bus.New()

	err := b.AddHandler(func(m *msg) error {
		fmt.Println("db insert listener id", m.ID)
		return nil
	})

	if err != nil {
		fmt.Printf("error: (%v) \n", err)
		return
	}

	m := new(msg)
	m.ID = 1
	m.body = "test"

	if err := b.Dispatch(m); err != nil {
		fmt.Printf("error: (%v) \n", err)
	}

}
Output:

db insert listener id 1

func (*InProcBus) Dispatch

func (b *InProcBus) Dispatch(msg Msg) error

Dispatch sends an msg to registered handler that were declared to accept values of a msg.

func (*InProcBus) Publish

func (b *InProcBus) Publish(msg Msg) error

Publish sends an msg to all registered listeners that were declared to accept values of a msg.

type Msg

type Msg interface{}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL