event

package
v3.0.0-alpha1 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2019 License: OSL-3.0 Imports: 1 Imported by: 0

README

Events

Flamingo uses a builtin event router, which routes events for each request.

This means that events are request-scoped, so you can assume that fired events should not cross request boundaries (and are bound to the same go routine).

Event interfaces

An Event can be everything, usually a struct with a few fields.

LoginSucessEvent struct {
    UserId string
}

Events should not have the current context in them!

Firing events

An Event is fired using the EventRouter

type (
    IndexController struct {
       EventRouter    event.Router                 `inject:""`
    }
    
    MyEvent struct {
        Data string
    }
)

func (controller *IndexController) Get(ctx web.Context) web.Response {
    controller.EventRouter.Dispatch(ctx, &MyEvent{Data: "Hello"})
}

Subscribing to events

To listen to Events you need to create a "Subscriber". A Subscriber will get all Events and need to decide which Events it want to handle:

type (
    type EventSubscriber struct {}
)


//Notify should get called by flamingo Eventlogic
func (e *EventSubscriber) NotifyWithContext(ctx context.Context, event event.Event) {
    switch event := event.(type) {
    case *MyEvent:
        subscriber.OnMyEvent(event)  // call event handler and do something
    }
}

Currently Flamingo uses Dingo Multibindings to register Event Subscriber

func (m *Module) Configure(injector *dingo.Injector) {
    injector.BindMulti((*event.SubscriberWithContext)(nil)).To(application.EventSubscriber{})
}

There is also the interface SubscriberWithContextas well as an interface Subscriber that you can use when you don't need the current request context.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultRouter

type DefaultRouter struct {
	Subscriber            subscriberProvider            `inject:",optional"`
	SubscriberWithContext subscriberWithContextProvider `inject:",optional"`
}

DefaultRouter is a default event routing implementation

func (*DefaultRouter) Dispatch

func (d *DefaultRouter) Dispatch(ctx context.Context, event Event)

Dispatch calls the event's Dispatch method on each subscriber

type Event

type Event interface{}

Event defines some event

type Router

type Router interface {
	Dispatch(ctx context.Context, event Event)
}

Router routes events

type Subscriber

type Subscriber interface {
	Notify(Event)
}

Subscriber is notified of an event

type SubscriberWithContext

type SubscriberWithContext interface {
	NotifyWithContext(ctx context.Context, event Event)
}

SubscriberWithContext is notified of an event, and gets the current ctx passed

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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