eventbus

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2022 License: Apache-2.0 Imports: 2 Imported by: 3

README

lipence/eventbus

This package provides publish & subscribe service in go.

Here defines Event and Subscriber interface, which inspired by java api. Any object that implements the above interface can participate in any bus implementation. Meanwhile, we do not restrict the form of Publisher, which can be any object, or just a statement.

structure

As shown above, The Publisher produce Event and launch it by calling Post method on the eventBus, then the bus distributes events by calling OnEvent of each subscriber, and subscriber could handle different events.

You can use eventbus to:

  • Decouples complex transaction logic
  • Manage program lifecycle
  • Build plugins
  • ......

Definition

type Key string

Unique identity for each Event. Subscriber listens to events which hold the same Key.

type Priority uint8

Used to sort the subscribers before triggered: The lower the priority, the sooner subscriber will be triggered.

Interface

type Event interface {
    Key() Key
}

Event is produced by Publisher, which holds the unique key for identification and other params (accessible after type assertion). Publisher should launch it by calling Post on Bus.

type Bus interface {
    Post(Event) error
    PostAndClear(Event) error
    Register(Subscriber) error
    Unregister(Subscriber)
}

Bus holds associations of events with subscribers. We add/remove association by calling Register/Unregister, and launch event by calling Post. If an event should be called only once, wa calls PostAndClear.

type Subscriber interface {
    Events() map[Key]Priority
    OnEvent(Event) error
}

Subscriber acts as event handler. The map returned by Events declares desired event and its processing priority. OnEvent receives and processes any event declared in Events.

Usage

Please refer to files in example.

RoadMap

  • Hook Mode
  • Filter Mode
  • Sync Executor
  • Async Executor
  • Watcher

Changelog

  • v0.1.0 Initial Code

Contact

Kenta Lee ( kenta.li@cardinfolink.com )

License

lipence/eventbus source code is available under the Apache-2.0 License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bus

type Bus interface {
	Post(Event) error
	PostAndClear(Event) error
	Register(Subscriber) error
	Unregister(Subscriber)
}

func Sync

func Sync() Bus

type Event

type Event interface {
	Key() Key
}

type Initializer

type Initializer interface {
	Init() error
}

type Key

type Key string

func (Key) String added in v0.1.1

func (k Key) String() string

type Priority

type Priority uint8

type Subscriber

type Subscriber interface {
	Events() map[Key]Priority
	OnEvent(Event) error
}

type SyncBus

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

func NewSync

func NewSync() *SyncBus

func (*SyncBus) Post

func (b *SyncBus) Post(e Event) (err error)

func (*SyncBus) PostAndClear

func (b *SyncBus) PostAndClear(e Event) (err error)

func (SyncBus) Register

func (a SyncBus) Register(subscriber Subscriber) (err error)

func (SyncBus) Unregister

func (a SyncBus) Unregister(subscriber Subscriber)

func (SyncBus) UnregisterEvent

func (a SyncBus) UnregisterEvent(e Event)

Jump to

Keyboard shortcuts

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