Help us shape the future of Go! Take the 2026 Go Developer Survey

eventsource

package
v0.20.1 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2025 License: MIT Imports: 2 Imported by: 2

Documentation

Overview

Package eventsource provides a pubsub.Topic that also stores the last published value in an atomic.Value.

Updating the value will result in a publish event.

Example
// Create a new event source.
e := New[int]()

// Subscribe to changes.
changes := e.SubscribeSync(nil)
go func() {
	for change := range changes {
		fmt.Println("change:", change.Msg)
		change.Ack()
	}
}()

// Publish a set a value.
err := e.PublishSync(1)
if err != nil {
	log.Fatal(err)
}

// Set and publish a value.
err = e.Store(2)
if err != nil {
	log.Fatal(err)
}

fmt.Println(e.Load())
Output:
change: 1
change: 2
2

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EventSource

type EventSource[T any] struct {
	*pubsub.Topic[T]
	// contains filtered or unexported fields
}

EventSource is a pubsub.Topic that also stores the last published value in an atomic.Value.

Updating the value will result in a publish event.

func New

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

func (*EventSource[T]) CompareAndSwap

func (e *EventSource[T]) CompareAndSwap(old, new T) bool

func (*EventSource[T]) Load

func (e *EventSource[T]) Load() T

func (*EventSource[T]) Store

func (e *EventSource[T]) Store(value T) error

Store will store a new value and synchronously publish it to all subscribers.

It will return any errors from the publish.

func (*EventSource[T]) Swap

func (e *EventSource[T]) Swap(value T) T

Jump to

Keyboard shortcuts

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