events

package
v2.0.0-...-59fec89 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2021 License: Apache-2.0, BSD-2-Clause Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolCaller

func BoolCaller(handler interface{}, params ...interface{})

func ByteCaller

func ByteCaller(handler interface{}, params ...interface{})

func ByteSliceCaller

func ByteSliceCaller(handler interface{}, params ...interface{})

func ErrorCaller

func ErrorCaller(handler interface{}, params ...interface{})

func Int16Caller

func Int16Caller(handler interface{}, params ...interface{})

func Int16SliceCaller

func Int16SliceCaller(handler interface{}, params ...interface{})

func Int32Caller

func Int32Caller(handler interface{}, params ...interface{})

func Int32SliceCaller

func Int32SliceCaller(handler interface{}, params ...interface{})

func Int64Caller

func Int64Caller(handler interface{}, params ...interface{})

func Int64SliceCaller

func Int64SliceCaller(handler interface{}, params ...interface{})

func Int8Caller

func Int8Caller(handler interface{}, params ...interface{})

func Int8SliceCaller

func Int8SliceCaller(handler interface{}, params ...interface{})

func IntCaller

func IntCaller(handler interface{}, params ...interface{})

func IntSliceCaller

func IntSliceCaller(handler interface{}, params ...interface{})

func StringCaller

func StringCaller(handler interface{}, params ...interface{})

func StringSliceCaller

func StringSliceCaller(handler interface{}, params ...interface{})

func Uint16Caller

func Uint16Caller(handler interface{}, params ...interface{})

func Uint16SliceCaller

func Uint16SliceCaller(handler interface{}, params ...interface{})

func Uint32Caller

func Uint32Caller(handler interface{}, params ...interface{})

func Uint32SliceCaller

func Uint32SliceCaller(handler interface{}, params ...interface{})

func Uint64Caller

func Uint64Caller(handler interface{}, params ...interface{})

func Uint64SliceCaller

func Uint64SliceCaller(handler interface{}, params ...interface{})

func Uint8Caller

func Uint8Caller(handler interface{}, params ...interface{})

func Uint8SliceCaller

func Uint8SliceCaller(handler interface{}, params ...interface{})

func VoidCaller

func VoidCaller(handler interface{}, params ...interface{})

Types

type Closure

type Closure struct {
	ID  uint64
	Fnc interface{}
}

func NewClosure

func NewClosure(f interface{}) *Closure

type Event

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

Event represents an object that is triggered to notify code of "interesting updates" that may affect its behavior.

Example
// create event object (usually exposed through a public struct that holds all the different event types)
event := NewEvent(intStringCaller)

// we have to wrap a function in a closure to make it identifiable
closure1 := NewClosure(func(param1 int, param2 string) {
	fmt.Println("#1 " + param2 + ": " + strconv.Itoa(param1))
})

// multiple subscribers can attach to an event (closures can be inlined)
event.Attach(closure1)
event.Attach(NewClosure(func(param1 int, param2 string) {
	fmt.Println("#2 " + param2 + ": " + strconv.Itoa(param1))
}))

// trigger the event
event.Trigger(1, "Hello World")

// unsubscribe the first closure and trigger again
event.Detach(closure1)
event.Trigger(1, "Hello World")
Output:

#1 Hello World: 1
#2 Hello World: 1
#2 Hello World: 1

func NewEvent

func NewEvent(triggerFunc func(handler interface{}, params ...interface{})) *Event

NewEvent is the constructor of an Event.

func (*Event) Attach

func (ev *Event) Attach(closure *Closure)

Attach allows to register a Closure that is executed when the Event triggers.

func (*Event) AttachAfter

func (ev *Event) AttachAfter(closure *Closure)

AttachAfter allows to register a Closure that is executed after the Event triggered.

func (*Event) AttachBefore

func (ev *Event) AttachBefore(closure *Closure)

AttachBefore allows to register a Closure that is executed before the Event triggers.

func (*Event) Detach

func (ev *Event) Detach(closure *Closure)

Detach allows to unregister a Closure that was previously registered.

func (*Event) DetachAll

func (ev *Event) DetachAll()

DetachAll removes all registered callbacks.

func (*Event) Trigger

func (ev *Event) Trigger(params ...interface{})

Trigger calls the registered callbacks with the given parameters.

type Queue

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

Queue represents an Event

func NewQueue

func NewQueue() *Queue

NewQueue returns an empty Queue.

func (*Queue) Clear

func (q *Queue) Clear()

Clear removes all elements from the Queue.

func (*Queue) Queue

func (q *Queue) Queue(event *Event, params ...interface{})

Queue enqueues an Event to be triggered later (using the Trigger function).

func (*Queue) Trigger

func (q *Queue) Trigger()

Trigger triggers all queued Events and empties the Queue.

type ThresholdEvent

type ThresholdEvent struct {
	*Event
	// contains filtered or unexported fields
}

ThresholdEvent is a data structure that acts like a normal event but only triggers when the value that was reported using the Set method causes the corresponding identifier to reach a new threshold. It is stateful which means that it tracks the current level for all identifiers individually to not trigger the same event multiple times.

func NewThresholdEvent

func NewThresholdEvent(options ...ThresholdEventOption) (thresholdEvent *ThresholdEvent)

NewThresholdEvent is the constructor for the ThresholdEvent.

func ThresholdEventFromBytes

func ThresholdEventFromBytes(bytes []byte, options ...ThresholdEventOption) (thresholdEvent *ThresholdEvent, consumedBytes int, err error)

ThresholdEventFromBytes unmarshals a collection of BranchIDs from a sequence of bytes.

func ThresholdEventFromMarshalUtil

func ThresholdEventFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil, options ...ThresholdEventOption) (thresholdEvent *ThresholdEvent, err error)

ThresholdEventFromMarshalUtil unmarshals a ThresholdEvent using a MarshalUtil (for easier unmarshaling).

func (*ThresholdEvent) Bytes

func (t *ThresholdEvent) Bytes() []byte

Bytes returns a marshaled version of the ThresholdEvent.

func (*ThresholdEvent) Level

func (t *ThresholdEvent) Level(identifier ThresholdEventIdentifier) (level int)

Level returns the current level of the reached threshold for the given identity.

func (*ThresholdEvent) Set

func (t *ThresholdEvent) Set(identifier ThresholdEventIdentifier, newValue float64) (newLevel int, transition ThresholdEventTransition)

Set updates the value associated with the given identifier and triggers the Event if necessary.

type ThresholdEventCallbackTypecaster

type ThresholdEventCallbackTypecaster func(handler interface{}, identifier interface{}, newLevel int, transition ThresholdEventTransition)

ThresholdEventCallbackTypecaster defines the signature of the function that is used to convert the parameters to the types expected by the callbacks.

type ThresholdEventConfiguration

type ThresholdEventConfiguration struct {
	Thresholds         []float64
	CallbackTypecaster ThresholdEventCallbackTypecaster
	IdentifierParser   ThresholdEventIdentifierParser
}

ThresholdEventConfiguration represents a collection of optional parameters that are used by the ThresholdEvent.

func NewThresholdEventConfiguration

func NewThresholdEventConfiguration(options ...ThresholdEventOption) (configuration *ThresholdEventConfiguration)

NewThresholdEventConfiguration creates a ThresholdEventConfiguration from the given Options.

type ThresholdEventIdentifier

type ThresholdEventIdentifier marshalutil.SimpleBinaryMarshaler

ThresholdEventIdentifier is the type that is used to address the identifiers of the entities whose values we are tracking.

type ThresholdEventIdentifierParser

type ThresholdEventIdentifierParser func(marshalUtil *marshalutil.MarshalUtil) (identifier interface{}, err error)

ThresholdEventIdentifierParser defines the signature of the function that is used to parse the Identifiers.

type ThresholdEventOption

type ThresholdEventOption func(*ThresholdEventConfiguration)

ThresholdEventOption is the type of the optional parameters of the ThresholdEvent.

func WithCallbackTypeCaster

func WithCallbackTypeCaster(callbackTypeCaster ThresholdEventCallbackTypecaster) ThresholdEventOption

WithCallbackTypeCaster sets the method that is used to type cast the called callbacks to their correct types.

func WithIdentifierParser

func WithIdentifierParser(identifierParser ThresholdEventIdentifierParser) ThresholdEventOption

WithIdentifierParser sets the parser for the ThresholdEventIdentifier that is used to identify different entities.

func WithThresholds

func WithThresholds(thresholds ...float64) ThresholdEventOption

WithThresholds sets the thresholds that are supposed to be used for the Triggers.

type ThresholdEventTransition

type ThresholdEventTransition int

ThresholdEventTransition is the type of the values that are used to indicate in which direction a threshold was passed.

const (
	// ThresholdLevelMaintained indicates that the reached threshold did not change.
	ThresholdLevelMaintained ThresholdEventTransition = 0

	// ThresholdLevelIncreased indicates that the new value is larger than the passed threshold.
	ThresholdLevelIncreased ThresholdEventTransition = 1

	// ThresholdLevelDecreased indicates that the new value is smaller than the passed threshold.
	ThresholdLevelDecreased ThresholdEventTransition = -1
)

func (ThresholdEventTransition) String

func (t ThresholdEventTransition) String() string

String returns a human readable version of the ThresholdEventTransition.

Jump to

Keyboard shortcuts

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