EventSinkSignalMixin<T> class abstract mixin

A mixin that implements the standard EventSink interface for a Signal holding an AsyncState value.

This mixin is designed specifically to interface with Stream consumers or other asynchronous data producers. It maps add to AsyncState.data(...), addError to AsyncState.error(...), and close to disposing the signal.

:::note This mixin only works with signals whose value is of type AsyncState<T>. :::

Example Usage

import 'package:signals/signals.dart';

class MyEventSinkSignal extends Signal<AsyncState<int>> with EventSinkSignalMixin<int> {
  MyEventSinkSignal(int initialValue) : super(AsyncState.data(initialValue));
}

void main() {
  final signal = MyEventSinkSignal(0);

  effect(() {
    final state = signal.value;
    state.map(
      data: (val) => print('Data received: $val'),
      error: (err, stack) => print('Error occurred: $err'),
      loading: () => print('Loading...'),
    );
  }); // Prints: "Data received: 0"

  // Treat it as an EventSink and add data
  signal.add(10); // Prints: "Data received: 10"

  // Push an error event
  signal.addError(Exception('Failure')); // Prints: "Error occurred: Exception: Failure"

  // Close/dispose the sink
  signal.close();
  print('Is disposed: ${signal.disposed}'); // Prints: "Is disposed: true"
}

:::caution Once close is called, the signal is disposed. Trying to call add or addError after closing will result in an exception. :::

Implemented types
Available extensions

Constructors

EventSinkSignalMixin()

Properties

autoDispose ↔ bool
Throws and error if read after dispose and can be disposed on last unsubscribe.
getter/setter pairinherited
debugLabel → String?
Debug label for Debug Mode Debug label for Debug Mode
no setterinherited
disposed ↔ bool
Check if the effect is disposed
getter/setter pairinherited
equalityCheck → SignalEquality<AsyncState<T>>
Optional method to check if to values are the same
no setterinherited
globalId → int
finalinherited
hashCode → int
The hash code for this object.
no setterinherited
internalValue → AsyncState<T>
@internal Internal getter for the raw value without subscription tracking.
no setterinherited
isInitialized → bool
Check if a signal value is set (does not subscribe)
no setterinherited
name → String?
finalinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
unwatched → void Function()?
finalinherited
value ↔ AsyncState<T>
Gets the current value of the signal.
getter/setter pairinherited
version ↔ int
Version numbers should always be >= 0, because the special value -1 is used by Nodes to signify potentially unused but recyclable nodes.
getter/setter pairinherited
watched → void Function()?
finalinherited

Methods

add(T event) → void
Adds a data event to the sink.
override
addError(Object error, [StackTrace? stackTrace]) → void
Adds an error to the sink.
override
call() → AsyncState<T>
Return the value when invoked
inherited
close() → void
Closes the sink.
override
dispose() → void
Dispose the signal
inherited
get() → AsyncState<T>
Helper method to get the current value
inherited
internalRefresh() → bool
@internal Refreshes the signal's value internally.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onDispose(void cleanup()) → void Function()
Add a cleanup function to be called when the signal is disposed
inherited
overrideWith(AsyncState<T> val) → Signal<AsyncState<T>>
Override the current signal with a new value as if it was created with it.
inherited
peek() → AsyncState<T>
In the rare instance that you have an effect that should write to another signal based on the previous value, but you don't want the effect to be subscribed to that signal, you can read a signals's previous value via signal.peek().
inherited
readonly() → ReadonlySignal<AsyncState<T>>
Returns a readonly signal
inherited
selectData<R>(R selector(T data)) → Computed<AsyncState<R>>

Available on Signal<AsyncState<T>>, provided by the AsyncSignalState extension

Select from data when available, preserving async state
set(AsyncState<T> val, {bool force = false}) → bool
Updates the signal's value by method call.
inherited
subscribe(void fn(AsyncState<T> value)) → void Function()
Subscribe to value changes with a dispose function
inherited
subscribeToNode(Node node) → void
@internal Subscribes this signal to notifications from a given dependency node.
inherited
toJson() → dynamic
Convert value to JSON
inherited
toString() → String
A string representation of this object.
inherited
unsubscribeFromNode(Node node) → void
@internal Unsubscribes this signal from notifications from a given dependency node.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited