ZenQuery<T> constructor

ZenQuery<T>({
  1. required Object queryKey,
  2. required Future<T> fetcher(),
  3. ZenQueryConfig? config,
  4. T? initialData,
  5. ZenScope? scope,
  6. bool autoDispose = true,
  7. bool registerInCache = true,
})

Implementation

ZenQuery({
  required Object queryKey,
  required this.fetcher,
  ZenQueryConfig? config,
  this.initialData,
  this.scope,
  this.autoDispose = true,
  bool registerInCache = true,
})  : queryKey = QueryKey.normalize(queryKey), // Normalize on init
      config = ZenQueryConfig.defaults.merge(config),
      _registerInCache = registerInCache {
  // Set initial data if provided
  if (initialData != null) {
    data.value = initialData;
    status.value = ZenQueryStatus.success;
    // Don't set _lastFetchTime here - initial data should be considered stale
    // so that the first fetch() actually fetches fresh data
    _lastFetchTime = null;
  }

  // Register in cache (scope-aware or global)
  if (_registerInCache) {
    if (scope != null) {
      _registerInScope();
    } else {
      // Register in global cache (existing behavior)
      ZenQueryCache.instance.register(this);
    }
  }

  // Setup background refetch if enabled
  _setupBackgroundRefetch();
}