resolveDataSourceAsync method

Future<DataSource<T>> resolveDataSourceAsync([
  1. Duration? timeout
])

Implementation

Future<DataSource<T>> resolveDataSourceAsync([Duration? timeout]) async {
  var dataSource = resolveDataSource();
  if (dataSource != null) return dataSource;

  timeout ??= Duration(seconds: 2);

  var completer = Completer<DataSource<T>>();

  var listen = DataHandler.onRegister.listen((_) {
    if (!completer.isCompleted && resolveDataSource() != null) {
      completer.complete(resolveDataSource());
    }
  });

  Future.delayed(timeout, () {
    if (!completer.isCompleted) {
      completer.complete(resolveDataSource());
    }
  });

  return completer.future.then((value) {
    listen.cancel();
    return value;
  });
}