effect function

void Function() effect(
  1. BloomEffectCallback compute, {
  2. String? debugLabel,
  3. BloomEffectCallback? onDispose,
  4. @internal String? hotReloadScopeId,
})

Runs compute reactively and returns a function that stops the effect.

While Bloom DDC hot-reload tracking is active, the returned cleanup is also registered so the dev runtime can stop old module effects after updated code attempts its in-place patch or remount. Outside that dev mode this delegates directly to signals_core.

Implementation

void Function() effect(
  BloomEffectCallback compute, {
  String? debugLabel,
  BloomEffectCallback? onDispose,
  @internal String? hotReloadScopeId,
}) {
  final parentScope = currentBloomSignalScope;
  final signalScope = hotReloadScopeId == null
      ? parentScope
      : composeBloomSignalScope(hotReloadScopeId);
  final scopedCompute = signalScope == null
      ? compute
      : () => runWithBloomSignalScopeValue(signalScope, compute);
  final cleanup = s.effect(
    scopedCompute,
    debugLabel: debugLabel,
    onDispose: onDispose,
  );
  if (!isBrowserHotReloadActive()) return cleanup;

  final registry = getBrowserHotEffectRegistry();
  if (registry == null) return cleanup;

  var disposed = false;
  late final void Function() trackedCleanup;
  trackedCleanup = () {
    if (disposed) return;
    disposed = true;
    registry.remove(trackedCleanup);
    cleanup();
  };
  registry.add(trackedCleanup);
  return trackedCleanup;
}