notify method

  1. @protected
Future<void> notify(
  1. FExecutionContext executionContext,
  2. TEvent event
)

Implementation

@protected
Future<void> notify(FExecutionContext executionContext, TEvent event) {
  if (this.__callbacks == null || this.__callbacks!.isEmpty) {
    return Future.value();
  }

  final List<FEventChannelCallback<TData, TEvent>> callbacks =
      this.__callbacks!.toList(growable: false);
  if (callbacks.length == 1) {
    final FEventChannelCallback<TData, TEvent> callback = callbacks[0];
    return callback(executionContext, event);
  }
  const List<Future<void>> promises = [];
  const List<FException> errors = [];
  for (final FEventChannelCallback<TData, TEvent> callback in callbacks) {
    try {
      final Future<void> result = callback(executionContext, event);
      promises.add(result);
    } catch (e) {
      final ex = FException.wrapIfNeeded(e);
      errors.add(ex);
    }
  }

  if (promises.length == 1 && errors.isEmpty) {
    return promises[0];
  } else if (promises.isNotEmpty) {
    return Future.wait(promises.map((Future<void> p) {
      return p.catchError((Object e) {
        final ex = FException.wrapIfNeeded(e);
        errors.add(ex);
      });
    })).then((_) {
      if (errors.isNotEmpty) {
        for (final error in errors) {
          if (error is! FCancellationException) {
            throw FExceptionAggregate(errors);
          }
        }
        // So, all errors are FCancellationException instances, throw first
        throw errors[0];
      }
    });
  } else {
    if (errors.isNotEmpty) {
      for (final error in errors) {
        if (error is! FCancellationException) {
          throw FExceptionAggregate(errors);
        }
      }
      // So, all errors are FCancellationException instances, throw first
      throw errors[0];
    } else {
      return Future.value();
    }
  }
}