emit method

  1. @override
void emit(
  1. T val
)
override

Assigns a new value and notifies listeners if the value changed.

This method only notifies listeners if the new value is different from the current value (using != operator).

count.emit(5);        // Notifies if count.val != 5
count.emit(count.val + 1); // Increment and notify

The new value is also published to the stream.

Throws an assertion error in debug mode if called on a disposed signal.

Implementation

@override
void emit(T val) {
  final registry = NeuronDebugRegistry.instance;
  final bool logMiddleware = registry.isEnabled;
  final List<Map<String, dynamic>> steps = [];

  T processedValue = val;
  for (final middleware in middlewares) {
    final before = processedValue;
    processedValue = middleware.process(super.value, processedValue);
    if (logMiddleware) {
      steps.add({
        'middleware': middleware.runtimeType.toString(),
        'before': before,
        'after': processedValue,
      });
    }
  }

  if (logMiddleware) {
    registry.recordMiddlewareEvent('signal_middleware', {
      'signalId': registry.idForNotifier(this) ??
          debugLabel ??
          runtimeType.toString(),
      'controller': (registry.idForNotifier(this) ?? '').split('.').first,
      'oldValue': NeuronDebugEncoder.encodeValue(super.value),
      'newValue': NeuronDebugEncoder.encodeValue(processedValue),
      'steps': steps,
    });
  }
  super.emit(processedValue);
}