effectScope function

EffectStop<EffectScope> effectScope(
  1. void fn()
)

Creates a new effect scope that can be used to group and cleanup multiple effects.

An effect scope allows you to create multiple effects that can all be stopped together by calling stop on the returned EffectStop. This is useful for organizing related effects and ensuring they are all properly cleaned up.

Example:

final count = signal(0);
final name = signal('');

// Create a scope containing multiple effects
final stop = effectScope(() {
  effect(() => print('Count is: ${count()}'));
  effect(() => print('Name is: ${name()}'));
});

count(1); // Prints: Count is: 1
name('Alice'); // Prints: Name is: Alice

stop(); // All effects in scope are stopped
count(2); // Nothing prints
name('Bob'); // Nothing prints

Implementation

EffectStop<EffectScope> effectScope(void Function() fn) {
  final scope = _EffectScope();
  system.runEffectScope(scope, fn);
  return EffectStop(scope);
}