event

package
v0.0.0-...-4685c53 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package `event` provides events and subscriptions. Event messages are created with a key/value pair using `NewEvent`, or an error with `NewError`. Events can be emitted on a channel using the `Emit` method, which returns false if the channel is full.

In order to make an existing instance able to emit events, embed a `PubSub` instance within the instance, and set the `Cap` value to the capacity required. For example,

type Task struct {
	event.PubSub
}

func NewTaskWithCapacity(cap int) Task {
	task := new(Task)
	task.Cap = cap
	return task
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewError

func NewError(err error) Event

NewError is called to create an error event

func NewEvent

func NewEvent(key, value any) Event

NewEvent is called to create an event with an arbitary key and value

Types

type PubSub

type PubSub struct {
	sync.Mutex
	Cap uint
	// contains filtered or unexported fields
}

PubSub is a publish/subscribe instance for events

func (*PubSub) Emit

func (p *PubSub) Emit(e Event) bool

Emit can be called to send an event to all subscribers, and returns true if the event was sent to all channels

func (*PubSub) String

func (p *PubSub) String() string

Return string representation of a `PubSub` instance

func (*PubSub) Sub

func (p *PubSub) Sub() <-chan Event

Sub creates a new subscriber channel and returns it

func (*PubSub) Unsub

func (p *PubSub) Unsub(ch <-chan Event)

Unsub is called to unsubscribe a specific channel. Will panic if the channel is not subscribed

Jump to

Keyboard shortcuts

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