Skip to content

Commit ebfaee6

Browse files
authored
Merge pull request #327 from mondoktamas/master
Added hasScope api to easily verify if the scope already pushed before
2 parents 1dce5a1 + d9bba91 commit ebfaee6

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ Another example could be a shopping basket where you want to ensure that not a c
251251
/// As dispose functions can be async, you should await this function.
252252
Future<void> dropScope(String scopeName);
253253
254+
/// Tests if the scope by name [scopeName] is registered in GetIt
255+
bool hasScope(String scopeName);
256+
254257
/// Clears all registered types for the current scope
255258
/// If you provided dispose function when registering they will be called
256259
/// [dispose] if `false` it only resets without calling any dispose
@@ -516,7 +519,7 @@ In some cases, it's handy if you could pass changing values to factories when ca
516519
/// [factoryfunc] factory function for this type that accepts two parameters
517520
/// [instanceName] if you provide a value here your factory gets registered with that
518521
/// name instead of a type. This should only be necessary if you need to register more
519-
/// than one instance of one type.
522+
/// than one instance of one type.
520523
///
521524
/// example:
522525
/// getIt.registerFactoryParam<TestClassParam,String,int>((s,i)

lib/get_it.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ abstract class GetIt {
427427
/// As dispose functions can be async, you should await this function.
428428
Future<void> dropScope(String scopeName);
429429

430+
/// Tests if the scope by name [scopeName] is registered in GetIt
431+
bool hasScope(String scopeName);
432+
430433
/// Returns the name of the current scope if it has one otherwise null
431434
/// if you are already on the baseScope it returns 'baseScope'
432435
String? get currentScopeName;

lib/get_it_impl.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,12 @@ class _GetItImplementation implements GetIt {
897897
_scopes.remove(scope);
898898
}
899899

900+
/// Tests if the scope by name [scopeName] is registered in GetIt
901+
@override
902+
bool hasScope(String scopeName) {
903+
return _scopes.any((x) => x.name == scopeName);
904+
}
905+
900906
@override
901907
String? get currentScopeName => _currentScope.name;
902908

test/scope_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ abstract class TestBaseClass {}
1111

1212
class TestClass extends TestBaseClass {
1313
final String? id;
14+
1415
TestClass([this.id]) {
1516
constructorCounter++;
1617
}
18+
1719
void dispose() {
1820
disposeCounter++;
1921
}
@@ -27,6 +29,7 @@ class TestClassShadowChangHandler extends TestBaseClass
2729
TestClassShadowChangHandler(this.onShadowChange, [this.id]) {
2830
constructorCounter++;
2931
}
32+
3033
void dispose() {
3134
disposeCounter++;
3235
}
@@ -44,7 +47,9 @@ class TestClassShadowChangHandler extends TestBaseClass
4447

4548
class TestClass2 {
4649
final String? id;
50+
4751
TestClass2([this.id]);
52+
4853
void dispose() {
4954
disposeCounter++;
5055
}
@@ -677,6 +682,23 @@ void main() {
677682
expect(getIt<TestClass>(instanceName: 'scope3'), isNotNull);
678683
});
679684

685+
test(
686+
'has registered scope test',
687+
() async {
688+
final getIt = GetIt.instance;
689+
getIt.pushNewScope(scopeName: 'scope1');
690+
getIt.pushNewScope(scopeName: 'scope2');
691+
getIt.pushNewScope(scopeName: 'scope3');
692+
693+
expect(getIt.hasScope('scope2'), isTrue);
694+
expect(getIt.hasScope('scope4'), isFalse);
695+
696+
await getIt.dropScope('scope2');
697+
698+
expect(getIt.hasScope('scope2'), isFalse);
699+
},
700+
);
701+
680702
test('full reset no dispose', () async {
681703
final getIt = GetIt.instance;
682704
constructorCounter = 0;

0 commit comments

Comments
 (0)