mint

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2024 License: MIT Imports: 2 Imported by: 1

Documentation

Overview

Package mint provides a tiny generic event emitter.

e := new(mint.Emitter) // create an emitter
mint.On(e, func(context.Context, MyEvent)) // subscribe to MyEvent
mint.Emit(e, context.Background(), MyEvent{ ... }) // emit values to consumers

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Emit

func Emit[T any](e *Emitter, ctx context.Context, v T) error

Emit Sequentially pushes value v to all consumers of type T. Receive order is indetermenistic. Cancelling ctx waits for active consumer to return and stops emitting further.

Using nil context will use context.Background() instead. error is always ctx.Err()

func On

func On[T any](e *Emitter, fn func(context.Context, T)) (off func() <-chan struct{})

On Registers a new consumer that receives all values which were emitted as T. So that On(e, func(context.Context, any)) will receive all values emitted with Emit[any](e, ...)

Reliance on a certain value to be present in the context signals that it should be included in T.

Call to off schedules consumer to stop once all concurrent Emits stop and returns a chan which will get closed once it is done. It is possible for consumer to receive values after a call to stop if other concurrent emits are ongoing.

func Use

func Use(e *Emitter, plugin func(context.Context, any) func())

Use allows to hook into event emitting process. Plugins are called sequentially in order they were added to Emitter. Plugin is a function that takes Emitted values and returns nil or a function that will be called after all consumers got the Emitted value. Returned functions are called in reverse order via `defer` statement.

Types

type Emitter

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

Emitter holds all active consumers and Emit hooks.

Jump to

Keyboard shortcuts

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