signals

package module
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2023 License: GPL-2.0 Imports: 4 Imported by: 2

README

Go-Signals

A simple package for easily sending signals application-wide. Signals are a way to communicate between different parts of your application.

Installation

go get github.com/Nigel2392/go-signals

Synchronously sending signals

// Create a new signal in the global pool,
// Signals from the global pool are of type interface{}.
// if it was already created, we will fetch the old one
// from the pool.
var signal = signals.Get("mysignal")

var messages = make([]string, 0)

// Initialize a receiver
var receiver = signals.NewRecv(func(signal signals.Signal[any], value ...any) error {
	t.Logf("Received %v from %s", value, signal.Name())
	messages = append(messages, value[0].(string))
	return nil
})

// Connect a receiver to a signal
signal.Connect(receiver)

// Send a signal
var err = signal.Send("This is a signal message!")
if err != nil {
	t.Errorf("Expected no errors, got %s", err.Error())
}

// Disconnect a receiver from a signal.
signal.Disconnect(receiver)

Asynchronously sending signals

var signal = signals.Get("mysignal")
var errChan = signal.SendAsync("This is a signal message!")
for err := range errChan {
	if err != nil {
		fmt.Printf("Received error: %s\n", err.Error())
	}
}

Create a new signal pool

// Create a new signal pool of type string
var newSignalPool = signals.NewPool[string]()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Listen

func Listen(name string, r func(Signal[any], any) error)

Register a receiver to a signal.

This will register a receiver to a signal inside of the pool.

If the signal does not exist, it will be created.

This is a shorthand.

func NewRecv

func NewRecv[T any](cb func(Signal[T], T) error) *receiver[T]

Initialize a new receiver

func Send

func Send(name string, value any) error

This will send the signal to all receivers that are connected to the signal.

Returns an error, if any of the receivers return an error.

Types

type Error added in v1.0.2

type Error struct {
	Val    string
	Errors []error
}

Error type for signals.

func SignalError added in v1.0.2

func SignalError(e error) (Error, bool)

func (Error) Error added in v1.0.2

func (e Error) Error() string

func (Error) Len added in v1.0.2

func (e Error) Len() int

type Pool

type Pool[T any] struct {
	// contains filtered or unexported fields
}

Pool of signals.

Can be used to store, retrieve and delete signals.

Can also be used to send signals to receivers.

func NewPool

func NewPool[T any]() *Pool[T]

Return a new pool of signals.

func (*Pool[T]) CreateOrSend

func (m *Pool[T]) CreateOrSend(name string, value T) error

Create or send a signal inside of the signal pool.

This will send a signal to the receivers, if the signal already exists.

func (*Pool[T]) Delete

func (m *Pool[T]) Delete(signalName string)

Delete a signal from the pool.

func (*Pool[T]) Get

func (m *Pool[T]) Get(name string) Signal[T]

Get a signal by name.

** Will initialize a new signal if none exists. **

func (*Pool[T]) Listen

func (m *Pool[T]) Listen(name string, r func(Signal[T], T) error) (Receiver[T], error)

Register a receiver to a signal.

This will register a receiver to a signal inside of the pool.

If the signal does not exist, it will be created.

This is a shorthand.

func (*Pool[T]) Range

func (m *Pool[T]) Range(f func(value Signal[T]) bool)

Range over signals inside of the pool.

func (*Pool[T]) Send

func (m *Pool[T]) Send(name string, value T) error

Send a signal inside of the signal pool, from the signal with the given name to all receivers that are connected to the signal.

func (*Pool[T]) SendGlobal

func (m *Pool[T]) SendGlobal(value T) error

Send a signal globally, across all signals present in the pool.

This will send a signal to ALL receivers inside of this pool.

type Receiver

type Receiver[T any] interface {
	// Receives the signal and value from the signal.
	Receive(Signal[T], T) error

	// Disconnects the receiver from the signal.
	Disconnect() error

	// Sets the signal on the receiver instance for later use.
	Signal(...Signal[T]) Signal[T]

	// Return the unique ID of the receiver.
	ID() uint64
}

Receiver interface This will be registered to any signals that it wants to receive. The receiver will be called when the signal is sent.

type Signal

type Signal[T any] interface {
	// Return the name of the signal.
	Name() string
	// Send a message across the signal's receivers.
	Send(T) error
	// Send a message across the signal's receivers asynchronously.
	SendAsync(T) chan error
	// Connect a list of receivers to the signal.
	Connect(...Receiver[T]) error
	// Disconnect a list of receivers from a signal.
	Disconnect(...Receiver[T])
	// Listen for a signal.
	Listen(func(Signal[T], T) error) (Receiver[T], error)
	// Clear all receivers for the signal.
	Clear()
}

Signal interface.

Used for sending messages to receivers.

func Get

func Get(name string) Signal[any]

Get a signal by name.

Create a new one if it does not exist.

func New added in v1.0.6

func New[T any](name string) Signal[T]

Create a new signal.

Jump to

Keyboard shortcuts

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