Skip to content

Latest commit

 

History

History
288 lines (197 loc) · 7.53 KB

README.md

File metadata and controls

288 lines (197 loc) · 7.53 KB

AtomicGo | event

Downloads Latest Release Tests Coverage Unit test count License: MIT Go report


Documentation | Contributing | Code of Conduct


AtomicGo

go get atomicgo.dev/event

event

import "atomicgo.dev/event"

Package event provides a generic and thread-safe event system for Go. It allows multiple listeners to subscribe to events carrying data of any type. Listeners can be added and notified when events are triggered, and the event can be closed to prevent further operations.

package main

import (
	"fmt"
	"time"

	"atomicgo.dev/event"
)

func delay() {
	time.Sleep(time.Millisecond * 10)
}

type Player struct {
	Name string
}

// Create a new event
var PlayerJoinEvent = event.New[Player]()

func main() {
	// Listen to the event as many times as you want
	PlayerJoinEvent.Listen(func(p Player) {
		fmt.Printf("Player %q joined the game\n", p.Name)
	})

	PlayerJoinEvent.Listen(func(_ Player) {
		// Do something else
	})

	// ...

	// Trigger the event somewhere - can be in a different function or package
	PlayerJoinEvent.Trigger(Player{Name: "Marvin"})
	delay() // delay for deterministic output
	PlayerJoinEvent.Trigger(Player{Name: "Bob"})
	delay() // delay for deterministic output
	PlayerJoinEvent.Trigger(Player{Name: "Alice"})

	// Keep the program alive
	time.Sleep(time.Second)

}

Output

Player "Marvin" joined the game
Player "Bob" joined the game
Player "Alice" joined the game

Index

Variables

ErrEventClosed is returned when an operation is attempted on a closed event.

var ErrEventClosed = errors.New("event is closed")

type Event

Event represents a generic, thread-safe event system that can handle multiple listeners. The type parameter T specifies the type of data that the event carries when triggered.

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

func New

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

New creates and returns a new Event instance for the specified type T.

func (*Event[T]) Close

func (e *Event[T]) Close()

Close closes the event system, preventing any new listeners from being added or events from being triggered. After calling Close, any subsequent calls to Trigger or Listen will return ErrEventClosed. Existing listeners are removed, and resources are cleaned up.

package main

import (
	"fmt"
	"time"

	"atomicgo.dev/event"
)

func delay() {
	time.Sleep(time.Millisecond * 10)
}

func main() {
	// Create a new event
	exampleEvent := event.New[int]()

	// Listen to the event
	exampleEvent.Listen(func(v int) {
		fmt.Println(v)
	})

	// Trigger the event
	exampleEvent.Trigger(1)
	delay() // delay for deterministic output
	exampleEvent.Trigger(2)
	delay() // delay for deterministic output
	exampleEvent.Trigger(3)

	// Time for listeners to process the event
	delay()

	// Close the event
	exampleEvent.Close()

	// Trigger the event again
	exampleEvent.Trigger(4)
	delay() // delay for deterministic output
	exampleEvent.Trigger(5)
	delay() // delay for deterministic output
	exampleEvent.Trigger(6)

	// Keep the program alive
	time.Sleep(time.Second)

}

Output

1
2
3

func (*Event[T]) Listen

func (e *Event[T]) Listen(f func(T)) error

Listen registers a new listener callback function for the event. The listener will be invoked with the event's data whenever Trigger is called. Returns ErrEventClosed if the event has been closed.

func (*Event[T]) Trigger

func (e *Event[T]) Trigger(value T) error

Trigger notifies all registered listeners by invoking their callback functions with the provided value. It runs each listener in a separate goroutine and waits for all listeners to complete. Returns ErrEventClosed if the event has been closed.

Generated by gomarkdoc


AtomicGo.dev  ·  with ❤️ by @MarvinJWendt | MarvinJWendt.com