Skip to content

Commit 71b9b34

Browse files
authored
Merge pull request #17 from jhionan/master
feature(unregister): fixes some appointments from PR #15
2 parents 8ea2a1c + 0843558 commit 71b9b34

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

lib/get_it.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,22 @@ class GetIt {
136136
_factoriesByName[instanceName] = serviceFactory;
137137
}
138138
}
139+
140+
/// Unregister by Type [T] or by name [instanceName]
141+
/// if you need to dispose any resources you can do it using [disposingFunction] function
142+
/// that provides a instance of your class to be disposed
143+
void unregister<T>([String instanceName, Function(T) disposingFunction]) {
144+
assert(
145+
_factoriesByName.containsKey(instanceName) || _factories.containsKey(T),
146+
'Nor Type ${T.toString()} or instance Name must not be null');
147+
if (instanceName == null) {
148+
disposingFunction(get<T>());
149+
_factories.remove(T);
150+
} else {
151+
disposingFunction(get(instanceName));
152+
_factoriesByName.remove(instanceName);
153+
}
154+
}
139155
}
140156

141157
enum _ServiceFactoryType { alwaysNew, constant, lazy }

test/get_it_test.dart

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ import 'package:test/test.dart';
33
import 'package:get_it/get_it.dart';
44

55
int constructorCounter;
6+
int disposeCounter;
67

78
abstract class TestBaseClass {}
89

910
class TestClass extends TestBaseClass {
1011
TestClass() {
1112
constructorCounter++;
1213
}
14+
15+
dispose() {
16+
disposeCounter++;
17+
}
1318
}
1419

1520
void main() {
@@ -51,7 +56,6 @@ void main() {
5156
GetIt.I.reset();
5257
});
5358

54-
5559
test('register constant', () {
5660
var getIt = GetIt.instance;
5761
constructorCounter = 0;
@@ -92,9 +96,8 @@ void main() {
9296
GetIt.I.reset();
9397
});
9498

95-
96-
test('register lazy singleton two instances of GetIt', () {
97-
GetIt.allowMultipleInstances=true;
99+
test('register lazy singleton two instances of GetIt', () {
100+
GetIt.allowMultipleInstances = true;
98101
var secondGetIt = GetIt.asNewInstance();
99102

100103
constructorCounter = 0;
@@ -115,11 +118,9 @@ void main() {
115118
expect(instance1, isNot(instanceSecondGetIt));
116119
expect(constructorCounter, 2);
117120

118-
119121
GetIt.I.reset();
120122
});
121123

122-
123124
test('trying to access not registered type', () {
124125
var getIt = GetIt.instance;
125126

@@ -192,4 +193,52 @@ void main() {
192193
expect(() => getIt('not there'), throwsA(TypeMatcher<Exception>()));
193194
GetIt.I.reset();
194195
});
196+
197+
test('unregister by type', () {
198+
var getIt = GetIt.instance;
199+
disposeCounter = 0;
200+
constructorCounter = 0;
201+
202+
getIt.registerSingleton<TestClass>(TestClass());
203+
204+
var instance1 = getIt.get<TestClass>();
205+
206+
expect(instance1 is TestClass, true);
207+
208+
var instance2 = getIt.get<TestClass>();
209+
210+
expect(instance1, instance2);
211+
212+
expect(constructorCounter, 1);
213+
214+
getIt.unregister<TestClass>(disposal: (testClass) {
215+
testClass.dispose();
216+
});
217+
218+
expect(disposeCounter, 1);
219+
220+
expect(() => getIt.get<TestClass>(), throwsA(TypeMatcher<Exception>()));
221+
});
222+
223+
test('unregister by name', () {
224+
var getIt = GetIt.instance;
225+
disposeCounter = 0;
226+
constructorCounter = 0;
227+
228+
getIt.registerSingleton<TestClass>(TestClass(), 'instanceName');
229+
230+
var instance1 = getIt.get('instanceName');
231+
232+
expect(instance1 is TestClass, true);
233+
234+
getIt.unregister<TestClass>(
235+
instanceName: 'instanceName',
236+
disposal: (testClass) {
237+
testClass.dispose();
238+
});
239+
240+
expect(disposeCounter, 1);
241+
242+
expect(() => getIt('instanceName'), throwsA(TypeMatcher<Exception>()));
243+
});
195244
}

0 commit comments

Comments
 (0)