evbus

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

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

Go to latest
Published: Jun 11, 2019 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DispatchMap

type DispatchMap sync.Map

DispatchMap is the shortcut when bus need to dispatch every event. Mapping is event type id(int) -> subscriber map(*SubscriberMap)

func NewDispatchMap

func NewDispatchMap() *DispatchMap

NewDispatchMap creates a new instance.

func (*DispatchMap) Access

func (m *DispatchMap) Access(key int32) (*SubscriberMap, bool)

Access the event type map.

func (*DispatchMap) AccessOrInit

func (m *DispatchMap) AccessOrInit(key int32, value *SubscriberMap) (*SubscriberMap, bool)

AccessOrInit explains itself, return false on initialized

func (*DispatchMap) Iterate

func (m *DispatchMap) Iterate(itfunc func(int32, *SubscriberMap) bool)

Iterate requires you to read sync.Map.Range

func (*DispatchMap) Remove

func (m *DispatchMap) Remove(key int32)

Remove the given key from the map.

func (*DispatchMap) SetValue

func (m *DispatchMap) SetValue(key int32, value *SubscriberMap)

SetValue setup the mapping

type Event

type Event struct {
	EventId uint32
	// a no type event will only dispatch to wildcard subscribers.
	EventType []int
	EventTime time.Time
	Payload   interface{}
}

Event represents each individual event

type EventBus

type EventBus struct {
	Events              sync.Pool
	EventCounter        uint32
	PendingCounter      uint32
	PendingThreshold    uint32
	Subscribers         *SubscriberMap
	WildcardSubscribers *SubscriberMap
	SubscriberCounter   int32
	DispatchList        *DispatchMap
	EventTypeList       *EventTypeMap
	EventTypeCounter    int32

	EachEventBackfileBase   time.Duration
	EachEventBackfireThresh time.Duration
	EventLoopBackfireBase   time.Duration
	EventLoopBackfireThresh time.Duration
	// contains filtered or unexported fields
}

EventBus is a bus for events dispatched to it.

func NewEventBus

func NewEventBus() *EventBus

NewEventBus returns a brand new event bus instance.

func (*EventBus) Finalize

func (evbus *EventBus) Finalize()

Finalize stops the event dispatching process after that, dont use it!

func (*EventBus) GetEventTypeId

func (evbus *EventBus) GetEventTypeId(typename string) (int32, error)

GetEventTypeId doesn't need to be access from outside

func (*EventBus) PublishEvent

func (evbus *EventBus) PublishEvent(event Event) error

PublishEvent will ignore the event id in parameter.

func (*EventBus) RegisterEventType

func (evbus *EventBus) RegisterEventType(typename string) (int32, error)

RegisterEventType register the given name, and return the event type id on succeed.

func (*EventBus) Subscribe

func (evbus *EventBus) Subscribe(sb Subscriber) int

Subscribe to some events. parameter sb.id will be ignored and the returned integer will be the final IDs.

func (*EventBus) Unsubscribe

func (evbus *EventBus) Unsubscribe(sbid int) error

Unsubscribe to events. The sbid must be provided.

type EventTypeMap

type EventTypeMap sync.Map

EventTypeMap is the map of name(string) -> event type id(int32)

func NewEventTypeMap

func NewEventTypeMap() *EventTypeMap

NewEventTypeMap creates a new instance.

func (*EventTypeMap) Access

func (m *EventTypeMap) Access(key string) (int32, bool)

Access the event type map.

func (*EventTypeMap) AccessOrInit

func (m *EventTypeMap) AccessOrInit(key string, value int32) (int32, bool)

AccessOrInit explains itself, return false on initialized

func (*EventTypeMap) Iterate

func (m *EventTypeMap) Iterate(itfunc func(string, int32) bool)

Iterate requires you to read sync.Map.Range

func (*EventTypeMap) Remove

func (m *EventTypeMap) Remove(key string)

Remove the given key from the map.

func (*EventTypeMap) SetValue

func (m *EventTypeMap) SetValue(key string, value int32)

SetValue setup the mapping

type Subscriber

type Subscriber struct {
	SubscriberId int
	// Priority ranged from -999 ~ 999.
	SubscriberPriority int
	// Not subscribing anything means subscribing everything.
	// Keep that in mind..
	SubscribedTypes []int
	MessageChannel  chan Event
	// The bool value returned in this functor will determine
	// whether this event is consumed, preventing further subscribers
	// to receive it. true -> consumed
	// Anyway, an event will be consumed after iterating through every subscribers resulted in
	// any matching subscriber found.
	// Warning: dont run time-consuming stuff in it, return as soon as possible!
	MessageFunctor func(Subscriber, Event) bool
	Extra          interface{}
}

Subscriber is entities listening on the event bus

type SubscriberMap

type SubscriberMap sync.Map

SubscriberMap is the map of subscriber id(int) -> subscriber(Subscriber)

func NewSubscriberMap

func NewSubscriberMap() *SubscriberMap

NewSubscriberMap creates a new instance.

func (*SubscriberMap) Access

func (m *SubscriberMap) Access(key int32) (Subscriber, bool)

Access the event type map.

func (*SubscriberMap) AccessOrInit

func (m *SubscriberMap) AccessOrInit(key int32, value Subscriber) (Subscriber, bool)

AccessOrInit explains itself, return false on initialized

func (*SubscriberMap) Iterate

func (m *SubscriberMap) Iterate(itfunc func(int32, Subscriber) bool)

Iterate requires you to read sync.Map.Range

func (*SubscriberMap) Remove

func (m *SubscriberMap) Remove(key int32)

Remove the given key from the map.

func (*SubscriberMap) SetValue

func (m *SubscriberMap) SetValue(key int32, value Subscriber)

SetValue setup the mapping

Jump to

Keyboard shortcuts

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