event_dispatcher

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2023 License: MIT Imports: 3 Imported by: 0

README

event-dispatcher

A simple, lightweight and flexible event dispatcher

To get started you can use the default dispatcher provided by this package or use your own an implement the Dispatcher interface. The EventDispatcher takes as argument a Provider interface

In this example we use our TreeProvider but you are free to give us your own provider. Simply implement the Provider interface

Below is a minimal example.

type MyTestEvent struct {
}

p := NewTreeProvider()
tlp.Add("my_package.my.test.event", ListenerFunc(func(event Event) (Event, error) {
    fmt.Println("I got called")
	
    return event, nil
}))

d := NewEventDispatcher(p)

d.Dispatch(MyTestEvent{})

If your listener returns an error then no other listeners will be called and the error will be reported back to the dispatcher.

The next Listener will get the event returned from the previous listener. If the Event implements StoppableEvent we will check the IsPropagationStopped function provided by the event. No further listeners will be called if that function returns true.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EventName

func EventName(event Event, separator string) string

EventName parses the name of the event struct and transform it from camelCase to lowercase string where each change is seperated by given separator

Types

type Dispatcher

type Dispatcher interface {
	Dispatch(event Event) error
}

Dispatcher interface only has the Dispatch function

func NewGoDispatcher

func NewGoDispatcher(provider Provider) Dispatcher

NewGoDispatcher returns an instance of Dispatcher Internally it uses the EventDispatcher

type Event

type Event interface{}

type EventDispatcher

type EventDispatcher struct {
	// contains filtered or unexported fields
}

EventDispatcher implements the Dispatcher interface and handles a default dispatching logic

func NewEventDispatcher

func NewEventDispatcher(provider Provider) *EventDispatcher

NewEventDispatcher returns a new EventDispatcher

func (*EventDispatcher) Dispatch

func (d *EventDispatcher) Dispatch(event Event) error

Dispatch a given event to all listeners provided by the listener If a listener returns an error no more listener gets called A listener gets the event returned by the previous listener if an Event is a type of StoppableEvent then no more listener get called

type GoDispatcher

type GoDispatcher struct {
	// contains filtered or unexported fields
}

func (*GoDispatcher) Dispatch

func (d *GoDispatcher) Dispatch(event Event) error

Dispatch uses the EventDispatcher Dispatch method but runs it in a go routine

type Listener

type Listener interface {
	// Handle should accept an event and return an event and possible error
	Handle(event Event) (Event, error)
}

Listener defines a listener

type ListenerFunc

type ListenerFunc func(event Event) (Event, error)

ListenerFunc is a wrapper for listener as functions and implements the Listener interface

func (ListenerFunc) Handle

func (f ListenerFunc) Handle(event Event) (Event, error)

Handle is the implementation for the Listener interface

type Provider

type Provider interface {
	// GetListenersForEvent is responsible to return a slice of Listener for a given Event
	GetListenersForEvent(event Event) []Listener
}

type StoppableEvent

type StoppableEvent interface {
	Event
	// IsPropagationStopped should tell the dispatcher that the propagation should be stopped
	IsPropagationStopped() bool
	// WithPropagationStopped returns a new event with the indicator that it should be stopped
	WithPropagationStopped() StoppableEvent
}

StoppableEvent defines a specific kind of Event

type TreeProvider

type TreeProvider struct {
	// contains filtered or unexported fields
}

func NewTreeProvider

func NewTreeProvider() *TreeProvider

NewTreeProvider returns a Provider which allows to listen to a set of events Eg. you got an Event mypackage.ModelTestEvent Now one can register to all events happening with "" or all events happening just in "mypackage" or all events of the model "mypackage.model" or explicit the event "mypackage.model.test" The provider will return from more global to more restrictive listeners

For the example above you would get the listeners in the following order listeners subscribed for "" listeners subscribed for "mypackage" listeners subscribed for "mypackage.model" listeners subscribed for "mypackage.model.test" each level returns their subscriber in the order they got them

func NewTreeProviderWithSeparator

func NewTreeProviderWithSeparator(separator string) *TreeProvider

NewTreeProviderWithSeparator same as NewTreeProvider but specifying a custom separator is possible

func (*TreeProvider) Add

func (t *TreeProvider) Add(listenTo string, listener Listener)

Add a listener to a "level". "" => all events

func (*TreeProvider) GetListenersForEvent

func (t *TreeProvider) GetListenersForEvent(event Event) []Listener

GetListenersForEvent implementing the interface

Jump to

Keyboard shortcuts

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