event

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultRegistry = new(Registry)

DefaultRegistry is the event registry used by package level functions.

Functions

func Fire

func Fire(ctx context.Context, id string, payload Data)

Fire fires an event on the DefaultRegistry.

func MatchSubscription

func MatchSubscription(topic string, subscription string) bool

MatchSubscription checks if topic matches the subscription. Topics are modelled after MQTT topics and are separated by using forward slashes. A '#' denotes a wildcard that matches the rest of the topic while '*' can be used as a wildcard that only matches the current field.

func Subscribe

func Subscribe(client, eventID string) <-chan *Event

Subscribe subscribes to events on the DefaultRegistry.

Types

type Data

type Data interface{}

Data is an opaque type for data attached to events fired by the event registry. The event data is not inspected by the registry but is likely used by any event subscribers. It's recommended that users make sure that the data attached to events is JSON serializable in case it's transmitted over network or file.

type Event

type Event struct {
	// ID is the ID (or topic) of the event. The ID is used by
	// the registry to notify subscriptions that have a matching
	// topic filter.
	ID string `json:"id"`
	// Data is an opaque interface that is set to any
	// event data. Note that Data must not be modified after
	// the event has been fired or received on a subscription
	// channel. Callers of Fire() should make sure to send a
	// copy of their data if further manipulation is planned.
	// Subscribers many only perform read access as no synchronization
	// is in place.
	Data Data `json:"data"`
	// Created is set to the current time when calling Fire().
	Created time.Time `json:"createdAt"`
}

Event is an event emitted by a event registry and published to any subscriber.

type Registry

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

func (*Registry) Fire

func (reg *Registry) Fire(ctx context.Context, id string, payload Data)

Fire fires a new event of the given ID to all subscribers.

func (*Registry) ListTypes

func (reg *Registry) ListTypes() []Type

func (*Registry) RegisterType

func (reg *Registry) RegisterType(eventType Type) (*TypeRef, error)

func (*Registry) Subscribe

func (reg *Registry) Subscribe(client, eventID string) <-chan *Event

Subscribe subscribes to events of the given eventID. All events fired on the registry that match the subscribed ID are published to the returned channel. Note that the order of events is not guaranteed.

func (*Registry) Unsubscribe

func (reg *Registry) Unsubscribe(client, eventID string)

Unsubscribe removes a previous subscription from client on eventID.

type Type

type Type struct {
	// ID is the name of the event type
	ID string

	// Description is a human readable descriptin of
	// the event type.
	Description string
}

Type describes an avaialble event type.

type TypeRef

type TypeRef struct {
	Type
	// contains filtered or unexported fields
}

func MustRegisterType

func MustRegisterType(t Type) *TypeRef

func (*TypeRef) Fire

func (ref *TypeRef) Fire(ctx context.Context, payload Data)

Jump to

Keyboard shortcuts

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