enterScope method

Future<void> enterScope(
  1. Scope scope
)

Signals that the application has entered the given scope.

This will notify all registered ScopeHandlers to handle the scope entry. Throws a WeaverException if the scope is already entered or if no handler is available for the scope.

Implementation

Future<void> enterScope(final Scope scope) async {
  if (isInScope(scope.name)) {
    throw WeaverException(
      'Has already entered scope <${scope.name}>, '
      'cannot call method weaver.enterScope on this scope again',
    );
  }

  final noHandlerAvailableToHandle =
      _scopeHandlers.where((final handler) => handler.canHandleScope(scope.name)).isEmpty;
  if (noHandlerAvailableToHandle && !allowScopesWithoutHandler) {
    throw WeaverException(
      'Entered scope ${scope.name} but there is no scope handler to handle this scope. '
      'please register a ScopeHandler class that handles scope: ${scope.name} using addScopeHandler() method.',
    );
  }

  // make scope handlers to handle the change in scopes (new scope added)
  for (final scopeHandler in _scopeHandlers) {
    if (scopeHandler.canHandleScope(scope.name)) {
      await scopeHandler.handle(EnterScope(scope));
    }
  }

  notifyObservers();
}