eventsource

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2024 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:

package main

import (
	"fmt"
	"time"

	"github.com/alecthomas/types/eventsource"
)

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

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

	// Publish a value.
	e.Store(1)

	// Publish a value and wait for it to be received.
	e.Store(2)
	time.Sleep(time.Millisecond)
}

Index

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)

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