signal<T> function

Signal<T> signal<T>(
  1. T initialValue, {
  2. String? key,
})

Creates a reactive Signal container initialized to initialValue.

When hot-reload tracking is active and a non-null key is supplied, the signal's value survives in-page module re-executions across hot remounts.

If the stored value type does not match T, the signal cleanly resets to initialValue.

The browser-side registry backing keyed signals is bounded at kMaxSignalRegistryEntries entries with least-recently-used eviction, so a long dev session cannot grow it without limit; an evicted key simply resets to initialValue on its next remount. Zero overhead when no key is given or tracking is inactive.

Implementation

s.Signal<T> signal<T>(T initialValue, {String? key}) {
  if (key == null || !isBrowserHotReloadActive()) {
    return s.signal<T>(initialValue);
  }

  try {
    final registry = getBrowserSignalRegistry();
    if (registry == null) {
      return s.signal<T>(initialValue);
    }

    final sig = s.signal<T>(initialValue);

    if (registry.containsKey(key)) {
      final stored = registry[key];
      if (stored is T) {
        sig.value = stored;
      }
    }

    sig.subscribe((val) {
      storeBrowserSignalValue(registry, key, val);
    });

    return sig;
  } catch (_) {
    return s.signal<T>(initialValue);
  }
}