watch

package
v1.16.109 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2023 License: MPL-2.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DeepCopyable

type DeepCopyable[T any] interface {
	DeepCopy() T
}

DeepCopyable describes a type that implements the DeepCopy method to get a copy of itself that is safe to pass around without worrying about receivers modifying the original.

type Map

type Map[K comparable, V DeepCopyable[V]] struct {
	M map[K]watchedVal[V]
}

Map safely stores and retrieves values by validating that there is a live watch for a key. InitWatch must be called to associate a key with its cancel function before any Set's are called.

func NewMap

func NewMap[K comparable, V DeepCopyable[V]]() Map[K, V]

func (Map[K, V]) CancelWatch

func (m Map[K, V]) CancelWatch(key K)

CancelWatch first calls the cancel function associated with the key then deletes the key from the map. No-op if key is not present.

func (Map[K, V]) DeepCopy

func (m Map[K, V]) DeepCopy() Map[K, V]

DeepCopy returns a copy of the Map that is safe to be passed around without worrying about receivers modifying the original or canceling its watches.

func (Map[K, V]) ForEachKey

func (m Map[K, V]) ForEachKey(f func(K) bool)

ForEachKey iterates through the map, calling f for each iteration. It is up to the caller to Get the value and nil-check if required. Stops iterating if f returns false. Order of iteration is non-deterministic.

func (Map[K, V]) ForEachKeyE

func (m Map[K, V]) ForEachKeyE(f func(K) error) error

ForEachKeyE iterates through the map, calling f for each iteration. It is up to the caller to Get the value and nil-check if required. If a non-nil error is returned by f, iterating stops and the error is returned. Order of iteration is non-deterministic.

func (Map[K, V]) Get

func (m Map[K, V]) Get(key K) (V, bool)

Get returns the underlying value for a key. If an entry has been set, returns (V, true). Otherwise, returns the zero value (V, false).

Note that even if InitWatch has been called for a key, unless Set has been called this function will return false.

func (Map[K, V]) InitWatch

func (m Map[K, V]) InitWatch(key K, cancel func())

InitWatch associates a cancel function with a key, allowing Set to be called for the key. The cancel function is allowed to be nil.

Any existing data for a key will be cancelled and overwritten.

func (Map[K, V]) IsWatched

func (m Map[K, V]) IsWatched(key K) bool

IsWatched returns true if InitWatch has been called for key and has not been cancelled by CancelWatch.

func (Map[K, V]) Len

func (m Map[K, V]) Len() int

func (Map[K, V]) Set

func (m Map[K, V]) Set(key K, val V) bool

Set stores V if K exists in the map. No-op if the key never was initialized with InitWatch or if the entry got cancelled by CancelWatch.

Jump to

Keyboard shortcuts

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