Future _runScopeExitHandlers()

in lib/service_scope.dart [241:268]


  Future _runScopeExitHandlers() {
    _cleaningUp = true;
    var errors = [];

    // We are running all on-scope-exit functions in reverse registration order.
    // Even if one fails, we continue cleaning up and report then the list of
    // errors (if there were any).
    return Future.forEach(_registeredEntries.reversed,
        (_RegisteredEntry registeredEntry) {
      if (registeredEntry.key != null) {
        _key2Values.remove(registeredEntry.key);
      }
      if (registeredEntry.scopeExitCallback != null) {
        return Future.sync(registeredEntry.scopeExitCallback!)
            .catchError((e, s) => errors.add(e));
      } else {
        return Future.value();
      }
    }).then((_) {
      _cleaningUp = true;
      _destroyed = true;
      if (errors.isNotEmpty) {
        throw Exception(
            'The following errors occured while running scope exit handlers'
            ': $errors');
      }
    });
  }