addScopeHandler method

Future<void> addScopeHandler(
  1. ScopeHandler newHandler
)

Adds a ScopeHandler to this Weaver instance.

The handler will be immediately notified to handle the current scope state.

Implementation

Future<void> addScopeHandler(final ScopeHandler newHandler) async {
  final hasConflictWithAnotherHandler =
      _scopeHandlers.firstWhereOrNull((final handler) => handler.canHandleScope(newHandler.scopeName)) != null;

  if (hasConflictWithAnotherHandler && !allowReassignment) {
    throw WeaverException('Cannot add ScopeHandler with name ${newHandler.scopeName}, since one is already added');
  } else if (allowReassignment) {
    _scopeHandlers.removeWhere((final currentHandler) => currentHandler.scopeName == newHandler.scopeName);
  }

  _scopeHandlers.add(newHandler);
  await newHandler.handle(HandlerAddedToWeaver());
}