Skip to content

popScopesTill #240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ccadieux opened this issue Dec 21, 2021 · 1 comment
Closed

popScopesTill #240

ccadieux opened this issue Dec 21, 2021 · 1 comment

Comments

@ccadieux
Copy link

ccadieux commented Dec 21, 2021

popScopesTill pops scopes including the one passed. This behaviour is a bit odd until generally means up to but not including.

I ended up having to write my own function to accomplish this behaviour. It would be nice if the library provided this even if it's just a toggle.

Future<void> popScopesTill<T extends Object>(
    {String scopeName = 'baseScope'}) async {
  while (GetIt.I.currentScopeName != scopeName) {
    await GetIt.I.popScope();
  }
}

Something like this maybe:

  @override
  Future<bool> popScopesTill(String scopeName, [bool inclusive = true]) async {
    assert(scopeName != _baseScopeName, "You can't pop the base scope");
    if (_scopes.firstWhereOrNull((x) => x.name == scopeName) == null) {
      return false;
    }
    String? _scopeName;
    do {
      _scopeName = _currentScope.name;
      await popScope();
    } while (inclusive ? _scopeName : _currentScope.name != scopeName);
    onScopeChanged?.call(false);
    return true;
  }
@escamoteur
Copy link
Collaborator

good point, included in next release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants