lossy

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: May 17, 2025 License: MPL-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package lossy contains wait/notify and pub/sub implementations that can lose data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewNotifier

func NewNotifier() (Notifier, Waiter)

NewNotifier creates a new pair of Notifier/Waiter.

It provides a lossy wait/notify implementation.

They shares a channel, which is closed by [Notifier.Notify]. After the channel is closed, a new channel is shared instead, so further notification is lost before you wait again.

func NewPubSub

func NewPubSub[T any]() (Pub[T], Sub[T])

NewPubSub creates a new pair of Pub/Sub.

It provides a lossy publish/subscribe implementation.

They shares an unresolved action.Data, which is resolved (or rejected) by publishing. After the Data is resolved, you lost further publishing before subscribing again.

Types

type Notifier

type Notifier interface {
	// Notifies current waiting waiters.
	Notify()
}

Notifier is a channel-based lossy notifier, coupled with Waiter.

It notifies current waiters, and prepares for next notification.

See [Waiter.Wait] for detailed info.

type Pub

type Pub[T any] interface {
	// Publishs v to current subscribers.
	V(v T)
	// Publish an error to current subscribers.
	E(e error)
}

Pub is a action.Data based lossy publisher.

Take a look at [Sub.Sub] for detailed info.

type Sub

type Sub[T any] interface {
	// Subscribes single value. Returned Data is resolved by next Pub.V or
	// rejected by Pub.E. You'll lose further values before you subscribe again.
	Sub() action.Data[T]
}

Sub is a action.Data based lossy subscriber.

It receives only one value once you called Sub().

type Waiter

type Waiter interface {
	// Waits for next notification. Returned channel is closed by next
	// Notifier.Notify call. You'll lose further notifications before
	// you wait again.
	Wait() <-chan struct{}

	// A cancellable wait.
	WaitCtx(context.Context) error
}

Waiter is a channel-bases lossy waiter, coupled with Notifier.

It receives only one notification once you call Wait().

Jump to

Keyboard shortcuts

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