events

package
v0.0.0-...-3caf815 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2021 License: MIT Imports: 3 Imported by: 1

Documentation

Overview

Package events 实现了应用中的事件机制,用于业务逻辑解耦。

例如

eventManager := events.NewEventManager(events.NewMemoryEventStore(false))
eventManager.Listen(func(evt UserCreatedEvent) {
	t.Logf("user created: id=%s, name=%s", evt.ID, evt.UserName)

	if evt.ID != "111" {
		t.Error("test failed")
	}
})

eventManager.Publish(UserCreatedEvent{
	ID:       "111",
	UserName: "李逍遥",
})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EventManager

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

EventManager is a manager for event dispatch

func NewEventManager

func NewEventManager(store EventStore) *EventManager

NewEventManager create a eventManager

func (*EventManager) Call

func (em *EventManager) Call(evt interface{}, listener Listener)

Call trigger listener to execute

func (*EventManager) Listen

func (em *EventManager) Listen(listeners ...Listener)

Listen create a relation from event to listners

func (*EventManager) Publish

func (em *EventManager) Publish(evt interface{})

Publish a event

type EventStore

type EventStore interface {
	Listen(eventName string, listener Listener)
	Publish(eventName string, evt interface{})
	SetManager(*EventManager)
}

EventStore is a interface for event store

type Listener

type Listener interface{}

Listener is a event listener

type MemoryEventStore

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

MemoryEventStore is a event store for sync operations

func NewMemoryEventStore

func NewMemoryEventStore(async bool) *MemoryEventStore

NewMemoryEventStore create a sync event store

func (*MemoryEventStore) Listen

func (eventStore *MemoryEventStore) Listen(evtType string, listener Listener)

Listen add a listener to a event

func (*MemoryEventStore) Publish

func (eventStore *MemoryEventStore) Publish(evtType string, evt interface{})

Publish publish a event

func (*MemoryEventStore) SetManager

func (eventStore *MemoryEventStore) SetManager(manager *EventManager)

SetManager event manager

Jump to

Keyboard shortcuts

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