RWSafe

package
v0.3.22 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Locker added in v0.2.39

type Locker[T uc.Enumer] struct {
	// contains filtered or unexported fields
}

Locker is a thread-safe locker that allows multiple goroutines to wait for a condition.

func NewLocker added in v0.2.39

func NewLocker[T uc.Enumer]() *Locker[T]

NewLocker creates a new Locker.

Use Locker.Set for observer boolean predicates.

Parameters:

  • keys: The keys to initialize the locker.

Returns:

  • *Locker[T]: A new Locker.

Behaviors:

  • All the predicates are initialized to true.

func (*Locker[T]) ChangeValue added in v0.3.18

func (l *Locker[T]) ChangeValue(key T, value bool) bool

ChangeValue changes the value of a subject.

Parameters:

  • key: The key to change the value.
  • value: The new value.

Returns:

  • bool: True if the key exists, false otherwise.

func (*Locker[T]) DoFunc added in v0.3.18

func (l *Locker[T]) DoFunc(f func(map[T]bool) bool) bool

DoFunc is a function that executes a function while waiting for the condition to be false.

Parameters:

  • f: The function to execute that takes a map of conditions as a parameter and returns true if the function should exit, false otherwise.

Returns:

  • bool: True if the function should exit, false otherwise.

func (*Locker[T]) Get added in v0.3.17

func (l *Locker[T]) Get(key T) (bool, bool)

Get returns the value of a predicate.

Parameters:

  • key: The key to get the value.

Returns:

  • bool: The value of the predicate.
  • bool: True if the key exists, false otherwise.

func (*Locker[T]) SetSubject added in v0.3.18

func (l *Locker[T]) SetSubject(key T, value bool, broadcast bool)

SetSubject adds a new subject to the locker.

Parameters:

  • key: The key to add.
  • subject: The subject to add.
  • broadcast: A flag indicating whether the subject should broadcast or signal.

Behaviors:

  • If the subject is nil, it will not be added.
  • It overwrites the existing subject if the key already exists.

type Observer added in v0.3.18

type Observer[T any] interface {
	// Notify notifies the observer of a change.
	//
	// Parameters:
	//   - change: The change that occurred.
	Notify(change T)
}

Observer is the interface that wraps the Notify method.

type ReactiveObserver added in v0.3.18

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

ReactiveObserver is a type that acts as a simple observer that calls a function when a change occurs.

func NewReactiveObserver added in v0.3.18

func NewReactiveObserver[T any](event func(T)) *ReactiveObserver[T]

NewReactiveObserver creates a new ReactiveObserver.

Parameters:

  • event: The event to call when a change occurs.

Returns:

  • *ReactiveObserver[T]: A new ReactiveObserver.

func (*ReactiveObserver[T]) Notify added in v0.3.18

func (r *ReactiveObserver[T]) Notify(change T)

Notify implements the Observer interface.

type SafeMap added in v0.3.17

type SafeMap[T comparable, U any] struct {
	// contains filtered or unexported fields
}

SafeMap is a thread-safe map.

func NewSafeMap added in v0.3.17

func NewSafeMap[T comparable, U any]() *SafeMap[T, U]

NewSafeMap creates a new SafeMap.

Returns:

  • *SafeMap[T, U]: A new SafeMap.

func (*SafeMap[T, U]) Clear added in v0.3.17

func (sm *SafeMap[T, U]) Clear()

Clear removes all elements from the map.

func (*SafeMap[T, U]) Copy added in v0.3.17

func (sm *SafeMap[T, U]) Copy() uc.Copier

Copy implements the Copier interface.

func (*SafeMap[T, U]) Delete added in v0.3.17

func (sm *SafeMap[T, U]) Delete(key T)

Delete removes a key from the map.

Parameters:

  • key: The key to remove.

func (*SafeMap[T, U]) Get added in v0.3.17

func (sm *SafeMap[T, U]) Get(key T) (U, bool)

Get retrieves a value from the map.

Parameters:

  • key: The key to retrieve the value.

Returns:

  • U: The value associated with the key.
  • bool: A boolean indicating if the key exists in the map.

func (*SafeMap[T, U]) GetMap added in v0.3.19

func (sm *SafeMap[T, U]) GetMap() map[T]U

GetMap returns the underlying map.

Returns:

  • map[T]U: The underlying map.

func (*SafeMap[T, U]) Iterator added in v0.3.17

func (sm *SafeMap[T, U]) Iterator() ui.Iterater[*uc.Pair[T, U]]

Iterator implements the Iterable interface.

func (*SafeMap[T, U]) Len added in v0.3.17

func (sm *SafeMap[T, U]) Len() int

Len returns the number of elements in the map.

Returns:

  • int: The number of elements in the map.

func (*SafeMap[T, U]) Scan added in v0.3.17

func (sm *SafeMap[T, U]) Scan(f ScanFunc[T, U]) (bool, error)

Scan applies a read-only function to all elements in the map.

Parameters:

  • f: The function to apply to all elements.

Returns:

  • bool: A boolean indicating if the scan completed successfully.
  • error: An error if the scan failed.

func (*SafeMap[T, U]) Set added in v0.3.17

func (sm *SafeMap[T, U]) Set(key T, val U)

Set sets a value in the map.

Parameters:

  • key: The key to set the value.
  • val: The value to set.

type ScanFunc added in v0.3.17

type ScanFunc[T, U any] func(key T, value U) (bool, error)

ScanFunc is a function that can be applied to all elements in the map.

Parameters:

  • key: The key of the element.
  • value: The value of the element.

Returns:

  • bool: A boolean indicating if the scan should continue.
  • error: An error if the scan should stop.

type Subject added in v0.3.18

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

Subject is the subject that observers observe.

func NewSubject added in v0.3.18

func NewSubject[T any](state T) *Subject[T]

NewSubject creates a new subject.

Parameters:

  • state: The initial state of the subject.

Returns:

*Subject[T]: A new subject.

func (*Subject[T]) Attach added in v0.3.18

func (s *Subject[T]) Attach(o Observer[T])

Attach attaches an observer to the subject.

Parameters:

  • o: The observer to attach.

func (*Subject[T]) Copy added in v0.3.18

func (s *Subject[T]) Copy() uc.Copier

Copy implements the Copier interface.

However, the obsevers are not copied.

func (*Subject[T]) DoRead added in v0.3.18

func (s *Subject[T]) DoRead(f func(T))

DoRead is a method of the Subject type. It is used to perform a read operation on the value stored in the Subject. Through the function parameter, the caller can access the value in a read-only manner.

Parameters:

  • f: A function that takes a value of type T as a parameter and returns nothing.

func (*Subject[T]) Get added in v0.3.18

func (s *Subject[T]) Get() T

Get gets the state of the subject.

Returns:

  • T: The state of the subject.

func (*Subject[T]) ModifyState added in v0.3.18

func (s *Subject[T]) ModifyState(f func(T) T)

ModifyState modifies the state of the subject.

Parameters:

  • f: The function to modify the state of the subject.

func (*Subject[T]) NotifyAll added in v0.3.18

func (s *Subject[T]) NotifyAll()

NotifyAll notifies all observers of a change.

func (*Subject[T]) Set added in v0.3.18

func (s *Subject[T]) Set(state T)

Set sets the state of the subject.

Parameters:

  • state: The new state of the subject.

func (*Subject[T]) SetObserver added in v0.3.18

func (s *Subject[T]) SetObserver(action func(T))

SetObserver sets the observer of the subject.

Parameters:

  • action: The action to set as the observer.

Jump to

Keyboard shortcuts

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