value property

T get value

Gets the current value of this reactive instance.

This provides synchronous access to the wrapped value without subscribing to changes. Accessing this property does not trigger any notifications.

Example:

final reactive = Reactive(42);
print(reactive.value); // 42

Implementation

T get value => _value;
set value (T newValue)

Updates the value and notifies listeners if the value has changed.

This setter compares the new value with the current value using the equality operator. If they are different, it:

  1. Calls middleware beforeUpdate hooks
  2. Updates the internal value
  3. Calls middleware afterUpdate hooks
  4. Checks if any middleware prevents emission with shouldEmit
  5. If allowed, creates a Snapshot and either:
    • Adds it to the buffer if in a batch update
    • Emits it to the stream immediately otherwise

No notifications are sent if the new value equals the current value.

Example:

final counter = Reactive(0);
counter.value = 1; // Triggers notification
counter.value = 1; // No notification (value unchanged)

Implementation

set value(T newValue) {
  T oldValue = _value;
  if (newValue != oldValue) {
    for (var middleware in _middlewares) {
      middleware.beforeUpdate(oldValue, newValue);
    }

    _value = newValue;
    for (var middleware in _middlewares) {
      middleware.afterUpdate(oldValue, newValue);
    }

    bool shouldPublish = _middlewares
        .map((e) => e.shouldEmit(oldValue, newValue))
        .fold(true, (value, element) => value && element);

    if (shouldPublish) {
      Snapshot<T> snapshot = Snapshot(oldValue, newValue);
      if (_isBatchUpdate) {
        _bufferedEvents.add(snapshot);
      } else {
        _controller.add(snapshot);
      }
    }
  }
}