StoreHook<T> constructor

StoreHook<T>(
  1. Store<T> _store,
  2. dynamic _onChange(), [
  3. String? _componentId,
  4. String? _componentType,
])

Create a store hook

Implementation

StoreHook(this._store, this._onChange, [this._componentId, this._componentType]) {
  // Track hook access for usage validation
  if (_componentId != null && _componentType != null) {
    _store.trackHookAccess(_componentId, _componentType);
  }

  // Create listener function that triggers component update with debouncing
  _listener = (T _) {
    // Prevent multiple rapid-fire updates by debouncing
    if (_updatePending) {
      return;
    }

    _updatePending = true;

    // Use microtask to batch multiple store updates together
    Future.microtask(() {
      if (_updatePending && _isSubscribed) {
        _updatePending = false;
        _onChange();
      }
    });
  };

  // Subscribe to store changes only once
  if (!_isSubscribed) {
    _store.subscribe(_listener);
    _isSubscribed = true;
  }
}