ensureInitialized method

  1. @override
Future<void> ensureInitialized()
override

Ensures this Paginated is initialized.

Implementation

@override
Future<void> ensureInitialized() async {
  Log.debug('ensureInitialized()', '$runtimeType');

  if (_futures.isEmpty && !status.value.isSuccess) {
    for (var f in initial) {
      if (f is Future<Map<K, T>>) {
        _futures.add(f..then(items.addAll));
      } else {
        items.addAll(f);
      }
    }

    if (pagination != null) {
      _paginationSubscription = pagination!.changes.listen((event) {
        switch (event.op) {
          case OperationKind.added:
          case OperationKind.updated:
            items[event.key!] = event.value as T;
            break;

          case OperationKind.removed:
            items.remove(event.key);
            break;
        }
      });

      _futures.add(
        pagination!.around(key: initialKey, cursor: initialCursor),
      );
    }

    if (_futures.isEmpty) {
      status.value = RxStatus.success();
    } else {
      if (items.isNotEmpty) {
        status.value = RxStatus.loadingMore();
      } else {
        status.value = RxStatus.loading();
      }

      await Future.wait(_futures);
      status.value = RxStatus.success();
    }
  } else {
    await Future.wait(_futures);
  }
}