pubsub

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: 1 Imported by: 3

Documentation

Overview

Package pubsub provides a simple publish/subscribe mechanism.

Example:

package main

import (
	"fmt"
	"time"

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

func main() {
	// Create a new topic.
	t := pubsub.New[int]()

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

	// Publish a value.
	t.Publish(1)

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Topic

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

func New

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

New creates a new topic that can be used to publish and subscribe to messages.

func (*Topic[T]) Close

func (s *Topic[T]) Close() error

Close the topic, blocking until all subscribers have been closed.

func (*Topic[T]) Publish

func (s *Topic[T]) Publish(t T)

func (*Topic[T]) Subscribe

func (s *Topic[T]) Subscribe(c chan T) chan T

Subscribe a channel to the topic.

The channel will be closed when the topic is closed.

func (*Topic[T]) Unsubscribe

func (s *Topic[T]) Unsubscribe(c chan T)

Unsubscribe a channel from the topic, closing the channel.

Jump to

Keyboard shortcuts

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