setValueInternal method

  1. @visibleForTesting
void setValueInternal(
  1. T val, {
  2. bool notifyListeners = true,
})

Internal setter for subclasses (Computed, etc.)

Implementation

@visibleForTesting
void setValueInternal(T val, {bool notifyListeners = true}) {
  // Fast Path (No Middleware)
  if (LevitReactiveMiddleware.bypassMiddleware ||
      !LevitReactiveMiddleware.hasSetMiddlewares) {
    if (_value == val) return;
    _value = val;
    _controller?.add(_value);
    if (notifyListeners) {
      super.notify();
    }
    return;
  }

  // Slow Path (Middleware / Interceptors)
  if (_value == val) return;

  final oldValue = _value;

  final change = LevitReactiveChange<T>(
    timestamp: DateTime.now(),
    valueType: T,
    oldValue: oldValue,
    newValue: val,
    stackTrace:
        _LevitReactiveCore.captureStackTrace ? StackTrace.current : null,
    restore: (v) {
      _value = v;
      _controller?.add(_value);
      if (notifyListeners) {
        super.notify();
      }
    },
  );

  // Core execution function
  void performSet(T v) {
    _value = v;
    _controller?.add(_value);
    if (notifyListeners) {
      super.notify();
    }
    // Record for batch
    if (_LevitReactiveCore.isBatching) {
      _LevitReactiveCore._recordBatchEntry(this, change);
    }
  }

  final wrapped =
      LevitStateMiddlewareChain.applyOnSet<T>(performSet, this, change);
  wrapped(val);
}