events

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2021 License: MIT Imports: 3 Imported by: 4

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/events"
)

func main() {
	dispatcher := &events.SyncDispatcher{}

	// Subscribe to a string topic named foo.
	dispatcher.Subscribe(events.Listen("foo", func(ctx context.Context, event interface{}) error {
		fmt.Println(event)
		return nil
	}))

	// Subscribe to a struct topic.
	type Topic struct{}
	dispatcher.Subscribe(events.Listen(Topic{}, func(ctx context.Context, event interface{}) error {
		fmt.Println(event)
		return nil
	}))

	dispatcher.Dispatch(context.Background(), "foo", 100)
	dispatcher.Dispatch(context.Background(), Topic{}, "event")
}
Output:

100
event

Index

Examples

Constants

View Source
const OnReload event = "onReload"

OnReload is an event that triggers the configuration reloads. The event payload is OnReloadPayload.

Variables

This section is empty.

Functions

This section is empty.

Types

type ListenerFunc added in v0.8.0

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

ListenerFunc is a listener that can be constructed from one function Listen. It listens to the given topic and then execute the callback.

func Listen

func Listen(topic interface{}, callback func(ctx context.Context, event interface{}) error) *ListenerFunc

Listen creates a functional listener in one line.

func (*ListenerFunc) Listen added in v0.8.0

func (f *ListenerFunc) Listen() interface{}

Listen implements contract.Listener

func (*ListenerFunc) Process added in v0.8.0

func (f *ListenerFunc) Process(ctx context.Context, event interface{}) error

Process implements contract.Listener

type OnReloadPayload added in v0.8.0

type OnReloadPayload struct {
	// NewConf is the latest configuration after the reload.
	NewConf contract.ConfigAccessor
}

OnReload is an event that triggers the configuration reloads

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, topic interface{}, event interface{}) 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