strobe

package module
v0.0.0-...-b5a3a73 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2022 License: MIT Imports: 2 Imported by: 1

README

strobe

CI Documentation

Go channel fan-out. Send the same message to many channels simultaneously.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Strobe

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

Strobe is an emitter that allows broadcasting messages by channel fan-out.

Example
sb := New[string]()
w := &sync.WaitGroup{}

listenPrinter := func(l <-chan string) {
	fmt.Println(<-l)
	w.Done()
}

w.Add(3)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go listenPrinter(sb.Listener(ctx))
go listenPrinter(sb.Listener(ctx))
go listenPrinter(sb.Listener(ctx))

sb.Pulse("PING")
w.Wait()
Output:

PING
PING
PING

func New

func New[T any]() *Strobe[T]

New creates a new Strobe.

func (*Strobe[T]) Count

func (s *Strobe[T]) Count() int

Count the number of active listeners on this Strobe.

func (*Strobe[T]) Listener

func (s *Strobe[T]) Listener(ctx context.Context) <-chan T

Listener returns a channel which receives pulsed messages from the strobe. The channel will be closed when ctx is cancelled.

func (*Strobe[T]) Pulse

func (s *Strobe[T]) Pulse(message T)

Pulse sends a message to all listening channels created by Listen().

Jump to

Keyboard shortcuts

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