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
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 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
Click to show internal directories.
Click to hide internal directories.