ds

package
v0.0.34 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2023 License: Apache-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 AsyncMap

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

a thread-safe map with subscribable events

func NewAsyncMap

func NewAsyncMap[T any](initial ...map[string]AsyncMapItem[T]) *AsyncMap[T]

func (*AsyncMap[T]) Add

func (q *AsyncMap[T]) Add(id string, data T)

adds an element, no effect if an element already exists

func (*AsyncMap[T]) Entries

func (q *AsyncMap[T]) Entries() []AsyncMapItem[T]

returns a list of all items currently in the map

func (*AsyncMap[T]) Get

func (q *AsyncMap[T]) Get(id string) *AsyncMapItem[T]

retrieves value by id

func (*AsyncMap[T]) Remove

func (q *AsyncMap[T]) Remove(id string)

remove the item

func (*AsyncMap[T]) Set

func (q *AsyncMap[T]) Set(id string, data T)

adds an element, overwriting if element already exists

func (*AsyncMap[T]) SetCompleted

func (q *AsyncMap[T]) SetCompleted(id string)

sets the item as completed

func (*AsyncMap[T]) SetFailed

func (q *AsyncMap[T]) SetFailed(id string)

sets the item as failed

func (*AsyncMap[T]) Subscribe

func (q *AsyncMap[T]) Subscribe() <-chan AsyncMapEvent[T]

returns the event channel

use Fanout if you want multiple subscribers

type AsyncMapEvent

type AsyncMapEvent[T any] struct {
	Action AsyncMapEventAction `json:"action"`
	Item   AsyncMapItem[T]     `json:"item"`
}

the event emitted to subscribers

type AsyncMapEventAction

type AsyncMapEventAction string

type of possible actions that may be emitted

const (
	AddedEventAction     AsyncMapEventAction = "added"
	CompletedEventAction AsyncMapEventAction = "completed"
	FailedEventAction    AsyncMapEventAction = "failed"
	RemovedEventAction   AsyncMapEventAction = "removed"
)

type AsyncMapItem

type AsyncMapItem[T any] struct {
	ID        string `json:"id"`
	Completed bool   `json:"completed"`
	Failed    bool   `json:"failed"`
	Data      T      `json:"data"`
}

an element of the map

type Fanout

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

thread-safe distributes events from an incoming channel into subscriber channels will be discarded, i.e. you will lose events if your subscriber can't keep up

func NewFanout

func NewFanout[T any](incoming <-chan T) *Fanout[T]

create a new Fanout, creates a goroutine to broadcast when an incoming event is received

func (*Fanout[T]) Broadcast

func (f *Fanout[T]) Broadcast(payload T)

send an event to all subscribers

func (*Fanout[T]) Subscribe

func (f *Fanout[T]) Subscribe(id string, delivery chan<- T)

add channel as a target for event delivery

func (*Fanout[T]) Unsubscribe

func (f *Fanout[T]) Unsubscribe(id string)

remove channel from deliveries

type Map

type Map[K comparable, V any] map[K]V

func NewMap

func NewMap[K comparable, V any](initial ...map[K]V) Map[K, V]

Instantiates a new map, optionally copying in the entries of the given map

func (Map[K, V]) Del

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

Deletes the value associated with the given key, returns true if a value was deleted

func (Map[K, V]) Entries

func (m Map[K, V]) Entries() []MapEntry[K, V]

Returns an array of key-value structs of the entries

func (Map[K, V]) ForEach

func (m Map[K, V]) ForEach(fn func(value V, key K) error) error

Calls a function for each element

func (Map[K, V]) Get

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

Retrieves the value associated with the given key or nil otherwise

func (Map[K, V]) Has

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

Checks if the given element exists

func (Map[K, V]) Keys

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

Returns the keys as an array

func (Map[K, V]) Len

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

Number of elements

func (Map[K, V]) Set

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

Sets the value associated with the given key, returns true if a value was overwritten

func (Map[K, V]) Values

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

Returns the values as an array

type MapEntry

type MapEntry[K comparable, V any] struct {
	Key   K
	Value V
}

type Set

type Set[T comparable] map[T]struct{}

func NewSet

func NewSet[T comparable](input ...T) Set[T]

Instantiates a new set, optionally filling with the given elements

func (Set[T]) Add

func (s Set[T]) Add(values ...T)

Add the given element to the set

func (Set[T]) Del

func (s Set[T]) Del(value T)

Removes the given element to the set

func (Set[T]) Difference

func (left Set[T]) Difference(right Set[T]) Set[T]

Creates a clone of the set and removes the elements from the given set

i.e. left - right

func (Set[T]) Entries

func (s Set[T]) Entries() []T

Returns all entries as an array

func (Set[T]) ForEach

func (s Set[T]) ForEach(fn func(element T) error) error

Calls a function for each element

func (Set[T]) Has

func (s Set[T]) Has(value T) bool

Checks if the given element exists

func (Set[T]) Intersection

func (left Set[T]) Intersection(right Set[T]) Set[T]

Creates a clone of the set and merges the elements of the given

i.e. left ∩ right

func (Set[T]) Len

func (s Set[T]) Len() int

Number of elements

func (Set[T]) Union

func (left Set[T]) Union(right Set[T]) Set[T]

Creates a clone of the set and merges the elements of the given

i.e. left ∪ right

Jump to

Keyboard shortcuts

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