register<T> static method

Future<T> register<T>(
  1. CancellationToken? cancelToken,
  2. Future<T> future
)

Implementation

static Future<T> register<T>(
    CancellationToken? cancelToken, Future<T> future) {
  if (cancelToken != null && !cancelToken.isCanceled) {
    final Completer<T> completer = Completer<T>();
    cancelToken._addCompleter(completer);
    return Future.any(<Future<T>>[completer.future, future])
        .then<T>((T result) async {
      cancelToken._removeCompleter(completer);
      return result;
    }).catchError((Object error) {
      cancelToken._removeCompleter(completer);
      throw error;
    });
  } else {
    return future;
  }
}