reactive

package
v0.0.0-...-c635e59 Latest Latest
Warning

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

Go to latest
Published: May 20, 2024 License: Apache-2.0, BSD-2-Clause Imports: 13 Imported by: 13

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	// Variable is the variable that holds the current time.
	Variable[time.Time]

	// OnTick registers a callback that gets called when the clock ticks.
	OnTick(handler func(now time.Time)) (unsubscribe func())

	// Shutdown shuts down the Clock.
	Shutdown()
}

Clock is a reactive variable that automatically updates its value to the current time with the given granularity.

func NewClock

func NewClock(granularity time.Duration) Clock

NewClock creates a new Clock.

type Counter

type Counter[InputType comparable] interface {
	// Variable holds the counter value.
	Variable[int]

	// Monitor adds the given input value as an input to the counter and returns a function that can be used to
	// unsubscribe from the input value.
	Monitor(input ReadableVariable[InputType]) (unsubscribe func())
}

Counter is a Variable that derives its value from the number of times a set of monitored input values fulfill a certain condition.

func NewCounter

func NewCounter[InputType comparable](condition ...func(inputValue InputType) bool) Counter[InputType]

NewCounter creates a Counter that counts the number of times monitored input values fulfill a certain condition.

type DerivedSet

type DerivedSet[ElementType comparable] interface {
	Set[ElementType]

	InheritFrom(sources ...ReadableSet[ElementType]) (unsubscribe func())
}

DerivedSet is a reactive Set implementation that allows consumers to subscribe to its changes and that inherits its elements from other sets.

func NewDerivedSet

func NewDerivedSet[ElementType comparable]() DerivedSet[ElementType]

NewDerivedSet creates a new DerivedSet with the given elements.

type DerivedVariable

type DerivedVariable[Type comparable] interface {
	// Variable is the variable that holds the derived value.
	Variable[Type]

	// Unsubscribe unsubscribes the DerivedVariable from its input values.
	Unsubscribe()
}

DerivedVariable is a Variable that automatically derives its value from other input values.

func NewDerivedVariable

func NewDerivedVariable[Type, InputType1 comparable, InputValueType1 ReadableVariable[InputType1]](compute func(currentValue Type, inputValue1 InputType1) Type, input1 InputValueType1, initialValue ...Type) DerivedVariable[Type]

NewDerivedVariable creates a DerivedVariable that transforms an input value into a different one.

func NewDerivedVariable2

func NewDerivedVariable2[Type, InputType1, InputType2 comparable, InputValueType1 ReadableVariable[InputType1], InputValueType2 ReadableVariable[InputType2]](compute func(currentValue Type, inputValue1 InputType1, inputValue2 InputType2) Type, input1 InputValueType1, input2 InputValueType2, initialValue ...Type) DerivedVariable[Type]

NewDerivedVariable2 creates a DerivedVariable that transforms two input values into a different one.

func NewDerivedVariable3

func NewDerivedVariable3[Type, InputType1, InputType2, InputType3 comparable, InputValueType1 ReadableVariable[InputType1], InputValueType2 ReadableVariable[InputType2], InputValueType3 ReadableVariable[InputType3]](compute func(currentValue Type, inputValue1 InputType1, inputValue2 InputType2, inputValue3 InputType3) Type, input1 InputValueType1, input2 InputValueType2, input3 InputValueType3, initialValue ...Type) DerivedVariable[Type]

NewDerivedVariable3 creates a DerivedVariable that transforms three input values into a different one.

func NewDerivedVariable4

func NewDerivedVariable4[Type, InputType1, InputType2, InputType3, InputType4 comparable, InputValueType1 ReadableVariable[InputType1], InputValueType2 ReadableVariable[InputType2], InputValueType3 ReadableVariable[InputType3], InputValueType4 ReadableVariable[InputType4]](compute func(currentValue Type, inputValue1 InputType1, inputValue2 InputType2, inputValue3 InputType3, inputValue4 InputType4) Type, input1 InputValueType1, input2 InputValueType2, input3 InputValueType3, input4 InputValueType4, initialValue ...Type) DerivedVariable[Type]

NewDerivedVariable4 creates a DerivedVariable that transforms four input values into a different one.

type Event

type Event interface {
	// Variable holds the boolean value that indicates whether the event was triggered.
	Variable[bool]

	// Trigger triggers the event and returns true if the event was triggered for the first time.
	Trigger() bool

	// WasTriggered returns true if the event was triggered.
	WasTriggered() bool

	// OnTrigger registers a callback that is executed when the event is triggered.
	OnTrigger(callback func()) (unsubscribe func())
}

Event is a reactive component that can be triggered exactly once and that informs its subscribers about the trigger. It conforms to the Variable interface and exposes a boolean value that is set to true when the event was triggered.

func NewEvent

func NewEvent() Event

NewEvent creates a new Event instance.

type EvictionState

type EvictionState[Type EvictionStateSlotType] interface {
	// LastEvictedSlot returns a reactive variable that contains the index of the last evicted slot.
	LastEvictedSlot() Type

	// EvictionEvent returns the event that is triggered when the given slot was evicted. It returns a triggered event
	// as default if the slot was already evicted.
	EvictionEvent(slot Type) Event

	// Evict evicts the given slot and triggers the corresponding eviction events.
	Evict(slot Type)
}

EvictionState is a reactive component that implements a slot based eviction mechanism.

func NewEvictionState

func NewEvictionState[Type EvictionStateSlotType]() EvictionState[Type]

NewEvictionState creates a new EvictionState instance.

type EvictionStateSlotType

type EvictionStateSlotType interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64
}

EvictionStateSlotType represents a constraint for the slot type of EvictionState.

type ReadableSet

type ReadableSet[ElementType comparable] interface {
	// OnUpdate registers the given callback that is triggered when the value changes.
	OnUpdate(callback func(appliedMutations ds.SetMutations[ElementType]), triggerWithInitialZeroValue ...bool) (unsubscribe func())

	// SubtractReactive returns a new set that will automatically be updated to always hold all elements of the current
	// set minus the elements of the other sets.
	SubtractReactive(others ...ReadableSet[ElementType]) Set[ElementType]

	// WithElements is a utility function that allows to set up dynamic behavior based on the elements of the Set which
	// is torn down once the element is removed gi(or the returned teardown function is called). It accepts an optional
	// condition that has to be satisfied for the setup function to be called.
	WithElements(setup func(element ElementType) (teardown func()), condition ...func(ElementType) bool) (teardown func())

	// ReadableSet imports the read methods of the Set interface.
	ds.ReadableSet[ElementType]
}

ReadableSet is a reactive Set implementation that allows consumers to subscribe to its value.

func NewReadableSet

func NewReadableSet[ElementType comparable](elements ...ElementType) ReadableSet[ElementType]

NewReadableSet creates a new ReadableSet with the given elements.

type ReadableVariable

type ReadableVariable[Type comparable] interface {
	// Get returns the current value.
	Get() Type

	// Read executes the given function with the current value while read locking the variable.
	Read(readFunc func(currentValue Type))

	// WithValue is a utility function that allows to set up dynamic behavior based on the latest value of the
	// ReadableVariable which is torn down once the value changes again (or the returned teardown function is called).
	// It accepts an optional condition that has to be satisfied for the setup function to be called.
	WithValue(setup func(value Type) (teardown func()), condition ...func(Type) bool) (teardown func())

	// WithNonEmptyValue is a utility function that allows to set up dynamic behavior based on the latest (non-empty)
	// value of the ReadableVariable which is torn down once the value changes again (or the returned teardown function
	// is called).
	WithNonEmptyValue(setup func(value Type) (teardown func())) (teardown func())

	// OnUpdate registers the given callback that is triggered when the value changes.
	OnUpdate(consumer func(oldValue, newValue Type), triggerWithInitialZeroValue ...bool) (unsubscribe func())

	// OnUpdateOnce registers the given callback for the next update and then automatically unsubscribes it. It is
	// possible to provide an optional condition that has to be satisfied for the callback to be triggered.
	OnUpdateOnce(callback func(oldValue, newValue Type), optCondition ...func(oldValue Type, newValue Type) bool) (unsubscribe func())

	// OnUpdateWithContext registers the given callback that is triggered when the value changes. In contrast to the
	// normal OnUpdate method, this method provides the old and new value as well as a withinContext function that can
	// be used to create subscriptions that are automatically unsubscribed when the callback is triggered again.
	OnUpdateWithContext(callback func(oldValue, newValue Type, withinContext func(subscriptionFactory func() (unsubscribe func()))), triggerWithInitialZeroValue ...bool) (unsubscribe func())

	// LogUpdates configures the Variable to emit logs about updates with the given logger and log level. An optional
	// stringer function can be provided to log the value in a custom format.
	LogUpdates(logger VariableLogReceiver, logLevel slog.Level, variableName string, stringer ...func(Type) string) (unsubscribe func())
}

ReadableVariable represents a variable that can be read and that informs subscribed consumers about updates.

func NewReadableVariable

func NewReadableVariable[Type comparable](value Type) ReadableVariable[Type]

NewReadableVariable creates a new ReadableVariable instance with the given value.

type Set

type Set[ElementType comparable] interface {
	// WriteableSet imports the write methods of the Set interface.
	ds.WriteableSet[ElementType]

	// ReadableSet imports the read methods of the Set interface.
	ReadableSet[ElementType]
}

Set is a reactive Set implementation that allows consumers to subscribe to its changes.

func NewSet

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

NewSet creates a new Set with the given elements.

type SortedSet

type SortedSet[ElementType comparable] interface {
	// Set imports the methods of the Set interface.
	Set[ElementType]

	// Ascending returns a slice of all elements of the set in ascending order.
	Ascending() []ElementType

	// Descending returns a slice of all elements of the set in descending order.
	Descending() []ElementType

	// HeaviestElement returns the element with the heaviest weight.
	HeaviestElement() ReadableVariable[ElementType]

	// LightestElement returns the element with the lightest weight.
	LightestElement() ReadableVariable[ElementType]
}

SortedSet is a reactive Set implementation that allows consumers to subscribe to its changes and that keeps a sorted perception of its elements. If the ElementType implements a Less method, it will be used to break ties between elements with the same weight.

func NewSortedSet

func NewSortedSet[ElementType comparable, WeightType cmp.Ordered](weightVariable func(element ElementType) Variable[WeightType]) SortedSet[ElementType]

NewSortedSet creates a new SortedSet instance that sorts its elements by the given weightVariable. If the ElementType implements a Less method, it will be used to break ties between elements with the same weight.

type Variable

type Variable[Type comparable] interface {
	// Init is a convenience function that acts as a setter for the variable that can be chained with the constructor.
	Init(value Type) Variable[Type]

	// WritableVariable imports the write methods of the Variable.
	WritableVariable[Type]

	// ReadableVariable imports the read methods of the Variable.
	ReadableVariable[Type]
}

Variable represents a variable that can be read and written and that informs subscribed consumers about updates.

func NewVariable

func NewVariable[Type comparable](transformationFunc ...func(currentValue Type, newValue Type) Type) Variable[Type]

NewVariable creates a new Variable instance with an optional transformation function that can be used to rewrite the value before it is stored.

type VariableLogReceiver

type VariableLogReceiver interface {
	// OnLogLevelActive registers a callback that is triggered when the given log level is activated. The shutdown
	// function that is returned by the callback is automatically called when the log level is deactivated.
	OnLogLevelActive(logLevel slog.Level, setup func() (shutdown func())) (unsubscribe func())

	// LogAttrs emits a log message with the given log level and attributes.
	LogAttrs(msg string, level slog.Level, args ...slog.Attr)
}

VariableLogReceiver defines the interface that is required to receive log messages from a Variable.

type WaitGroup

type WaitGroup[T comparable] interface {
	// Event returns the event that is triggered when all elements are done.
	Event

	// Add adds the given elements to the wait group.
	Add(elements ...T)

	// Done marks the given elements as done and triggers the wait group if all elements are done.
	Done(elements ...T)

	// Wait waits until all elements are done.
	Wait()

	// PendingElements returns the currently pending elements.
	PendingElements() ReadableSet[T]

	// Debug subscribes to the PendingElements and logs the state of the WaitGroup to the console whenever it changes.
	Debug(optStringer ...func(T) string) (unsubscribe func())
}

WaitGroup is a reactive Event that waits for a set of elements to be done and that allows to inspect the pending elements.

func NewWaitGroup

func NewWaitGroup[T comparable](elements ...T) WaitGroup[T]

NewWaitGroup creates a new WaitGroup.

type WritableVariable

type WritableVariable[Type comparable] interface {
	// Set sets the new value and triggers the registered callbacks if the value has changed.
	Set(newValue Type) (previousValue Type)

	// Compute sets the new value by applying the given function to the current value and triggers the registered
	// callbacks if the value has changed.
	Compute(computeFunc func(currentValue Type) Type) (previousValue Type)

	// DefaultTo atomically sets the new value to the default value if the current value is the zero value and triggers
	// the registered callbacks if the value has changed. It returns the new value and a boolean flag that indicates if
	// the value was updated.
	DefaultTo(defaultValue Type) (newValue Type, updated bool)

	// InheritFrom inherits the value from the given ReadableVariable.
	InheritFrom(other ReadableVariable[Type]) (unsubscribe func())

	// DeriveValueFrom is a utility function that allows to derive a value from a newly created DerivedVariable.
	// It returns a teardown function that unsubscribes the DerivedVariable from its inputs.
	DeriveValueFrom(source DerivedVariable[Type]) (teardown func())

	// ToggleValue sets the value to the given value and returns a function that resets the value to its zero value.
	ToggleValue(value Type) (reset func())
}

WritableVariable represents a variable that can be written to.

Jump to

Keyboard shortcuts

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