addEffect method

StreamSubscription<Snapshot<Set<E>>> addEffect(
  1. dynamic effect(
    1. Snapshot<Set<E>>
    )
)

Adds an effect that runs whenever the set changes.

An effect is a function that gets called when the set changes. It receives a Snapshot containing both the old and new set values.

Example:

final permissions = {'read', 'write'}.reactive;

// Add an effect to log changes
permissions.addEffect((snapshot) {
  final added = snapshot.newValue.difference(snapshot.oldValue);
  final removed = snapshot.oldValue.difference(snapshot.newValue);

  if (added.isNotEmpty) {
    print('Added permissions: $added');
  }
  if (removed.isNotEmpty) {
    print('Removed permissions: $removed');
  }
});

permissions.value.add('admin');
permissions.value = Set.from(permissions.value); // Effect will trigger

Note: To make modifications trigger effects, you must assign the modified set back to the reactive value, typically using value = Set.from(value).

Implementation

StreamSubscription<Snapshot<Set<E>>> addEffect(
    Function(Snapshot<Set<E>>) effect) {
  return stream.listen(effect);
}