ReactiveNotify<T> constructor

ReactiveNotify<T>(
  1. T create(), {
  2. List<ReactiveNotify>? related,
  3. Key? key,
})

Creates or returns existing instance of ReactiveNotify

Parameters:

  • create: Function that creates the initial state
  • related: Optional list of related states
  • key: Optional key for instance identity

Implementation

factory ReactiveNotify(T Function() create,
    {List<ReactiveNotify>? related, Key? key}) {
  key ??= UniqueKey();

  assert(() {
    log('''
📦 Creating ReactiveNotify<$T>
${related != null ? '🔗 With related types: ${related.map((r) => r.value.runtimeType).join(', ')}' : ''}
''', level: 5);
    return true;
  }());

  if (_instances.containsKey(key)) {
    final trace = StackTrace.current.toString().split('\n')[1];
    throw StateError('''
⚠️ Invalid Reference Structure Detected!
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Current Notifier: $T
Key: $key
Problem: Attempting to create a notifier with an existing key, which could lead to circular dependencies or duplicate instances.
Solution: Ensure that each notifier has a unique key or does not reference itself directly.
Location: $trace
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
''');
  }

  try {
    _instances[key] = ReactiveNotify._(create, related, key);
  } catch (e) {
    if (e is StateError) {
      rethrow;
    }
    final trace = StackTrace.current.toString().split('\n')[1];
    throw StateError('''
⚠️ ReactiveNotify Creation Failed!
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Type: $T
Error: $e

🔍 Check:
 - Related states configuration
 - Initial value creation
 - Type consistency
Location: $trace
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
''');
  }

  return _instances[key] as ReactiveNotify<T>;
}