Observing

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoWithBackup

func DoWithBackup[T Backuper[E], E any](elem T, fn func(T) (bool, error)) error

DoWithBackup calls the given function with the element and creates a backup of the element before calling the function. If the function returns an error, the element is restored from the backup. If the function returns false, the element is restored from the backup.

Parameters:

  • elem: The element to call the function with.
  • fn: The function to call with the element.

Returns:

  • error: An error if the function returns an error.

Types

type Backuper

type Backuper[T any] interface {
	// Backup creates a backup of the element.
	//
	// Returns:
	//   - Backuper: The backup of the element.
	Backup() T

	// Restore restores the element from the backup.
	//
	// Parameters:
	//   - backup: The backup to restore from.
	Restore(backup T)
}

Backuper is an interface that provides methods to create a backup of an element and restore the element from the backup.

type Observer

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

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

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

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

Notify implements the Observer interface.

type Subject

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

Subject is the subject that observers observe.

func NewSubject

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

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

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

Copy implements the Copier interface.

However, the obsevers are not copied.

func (*Subject[T]) DoRead

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

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

Get gets the state of the subject.

Returns:

  • T: The state of the subject.

func (*Subject[T]) ModifyState

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

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

NotifyAll notifies all observers of a change.

func (*Subject[T]) Set

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

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