events

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: May 25, 2021 License: MIT Imports: 5 Imported by: 5 modules, 4 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package events provides a simple and effective implementation of event system.

Event is a battle proven way to decoupling services. Package event calls event listeners in a synchronous, sequential execution. The synchronous listener is only a "go" away from an asynchronous handler, but asynchronous listener can not be easily made synchronous.

The event listeners can also be used as hooks. If the event data is a pointer type, listeners may alter the data. This enables plugin/addon style decoupling.

Note: Package event focus on events within the system, not events outsource to eternal system. For that, use a message queue like kafka.

Example
package main

import (
	"context"
	"fmt"

	"github.com/DoNewsCode/core/contract"
	"github.com/DoNewsCode/core/events"
)

func main() {
	dispatcher := &events.SyncDispatcher{}
	// Subscribe to int event.
	dispatcher.Subscribe(events.Listen(events.From(0), func(ctx context.Context, event contract.Event) error {
		fmt.Println(event.Data())
		return nil
	}))
	// Subscribe to string event.
	dispatcher.Subscribe(events.Listen(events.From(""), func(ctx context.Context, event contract.Event) error {
		fmt.Println(event.Data())
		return nil
	}))
	dispatcher.Dispatch(context.Background(), events.Of(100))
	dispatcher.Dispatch(context.Background(), events.Of("event"))
}
Output:
100
event

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func From

func From(events ...interface{}) []contract.Event

From implements contract.Event for a number of events. It is particularly useful when constructing contract.Listener's Listen function.

func Listen

func Listen(events []contract.Event, callback func(ctx context.Context, event contract.Event) error) funcListener

Listen creates a functional listener in one line.

Types

type Event

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

Event is a thin wrapper for events. It implements contract.Event for any interface.

func Of

func Of(event interface{}) Event

Of wraps any struct, making it a valid contract.Event.

func (Event) Data

func (e Event) Data() interface{}

Data returns the enclosing data in the event.

func (Event) Type

func (e Event) Type() string

Type returns the type of the event as string.

type SyncDispatcher

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

SyncDispatcher is a contract.Dispatcher implementation that dispatches events synchronously. SyncDispatcher is safe for concurrent use.

func (*SyncDispatcher) Dispatch

func (d *SyncDispatcher) Dispatch(ctx context.Context, event contract.Event) error

Dispatch dispatches events synchronously. If any listener returns an error, abort the process immediately and return that error to caller.

func (*SyncDispatcher) Subscribe

func (d *SyncDispatcher) Subscribe(listener contract.Listener)

Subscribe subscribes the listener to the dispatcher.

Jump to

Keyboard shortcuts

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