getOrCreate<T> method

HopeController<T> getOrCreate<T>({
  1. required List queryKey,
  2. required Future<T> fetcher(
    1. dynamic pageParam
    ),
  3. required HopeOptions options,
  4. dynamic getNextPageParam(
    1. T lastResult
    )?,
})

Returns existing controller or creates a new one for this key. Increments the reference count every time a widget subscribes.

Implementation

HopeController<T> getOrCreate<T>({
  required List<dynamic> queryKey,
  required Future<T> Function(dynamic pageParam) fetcher,
  required HopeOptions options,
  dynamic Function(T lastResult)? getNextPageParam,
}) {
  final key = _controllerKey(queryKey);

  if (_controllers.containsKey(key)) {
    _refCounts[key] = (_refCounts[key] ?? 0) + 1;
    return _controllers[key] as HopeController<T>;
  }

  final controller = HopeController<T>(
    queryKey: queryKey,
    fetcher: fetcher,
    options: options,
    cache: _cache,
    getNextPageParam: getNextPageParam,
  );

  _controllers[key] = controller;
  _refCounts[key] = 1;
  return controller;
}