Skip to content

Commit 1091f9f

Browse files
authored
Merge pull request #416 from flutter-it/rename_service_factory
Rename service factory
2 parents 4c84ca8 + 29d28e6 commit 1091f9f

File tree

7 files changed

+337
-328
lines changed

7 files changed

+337
-328
lines changed

.idea/libraries/Dart_SDK.xml

Lines changed: 0 additions & 19 deletions
This file was deleted.

.idea/modules.xml

Lines changed: 0 additions & 8 deletions
This file was deleted.

.idea/workspace.xml

Lines changed: 0 additions & 36 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [8.2.0]
2+
3+
Thanks to PR by @Hu-Wentao we now expose part of the internal state of get_it so tool authors have more options. While doing so internal renaming has happened, so where previously where the term "ServiceFactory" was uses we not use consistently `ObjectRegistration` and instead of variables with `factory` in the name we use `registration` this has not impact on the public API so it is not a breaking change.
4+
15
## [8.1.0]
26

37
* adding documentation https://github.com/flutter-it/get_it/issues/411

lib/get_it.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ import 'package:meta/meta.dart';
1111

1212
part 'get_it_impl.dart';
1313

14+
/// You will see a rather esoteric looking test `(const Object() is! T)` at several places.
15+
/// It tests if [T] is a real type and not Object or dynamic.
16+
17+
/// For each registered factory/singleton an [ObjectRegistration<T>] is created
18+
/// it holds either the instance of a Singleton or/and the creation functions
19+
/// for creating an instance when [get] is called
20+
///
21+
/// There are three different types:
22+
enum ObjectRegistrationType {
23+
alwaysNew, // factory which means on every call of [get] a new instance is created
24+
constant, // normal singleton
25+
lazy, // lazy
26+
cachedFactory, // cached factory
27+
}
28+
1429
/// If your singleton that you register wants to use the manually signalling
1530
/// of its ready state, it can implement this interface class instead of using
1631
/// the [signalsReady] parameter of the registration functions
@@ -109,6 +124,31 @@ class WaitingTimeOutException implements Exception {
109124
}
110125
}
111126

127+
abstract class ObjectRegistration<T extends Object> {
128+
ObjectRegistrationType get registrationType;
129+
130+
/// In case of a named registration the instance name is here stored for easy access
131+
String? get instanceName;
132+
133+
/// true if one of the async registration functions have been used
134+
bool get isAsync;
135+
136+
/// If an existing Object gets registered or an async/lazy Singleton has finished
137+
/// its creation, it is stored here
138+
Object? get instance;
139+
140+
/// the type that was used when registering, used for runtime checks
141+
Type get registeredWithType;
142+
143+
bool get isReady;
144+
145+
bool get isNamedRegistration => instanceName != null;
146+
147+
String get debugName => '$instanceName : $registeredWithType';
148+
149+
bool get canBeWaitedFor;
150+
}
151+
112152
/// Very simple and easy to use service locator
113153
/// You register your object creation factory or an instance of an object with [registerFactory],
114154
/// [registerSingleton] or [registerLazySingleton]
@@ -451,6 +491,12 @@ abstract class GetIt {
451491
bool useWeakReference = false,
452492
});
453493

494+
/// find the first registration that matches the type [T]/[instanceName] or the [instance]
495+
ObjectRegistration? findFirstObjectRegistration<T extends Object>({
496+
Object? instance,
497+
String? instanceName,
498+
});
499+
454500
/// Tests if an [instance] of an object or a Type ([T] or [type]) or a name [instanceName]
455501
/// is registered inside GetIt
456502
bool isRegistered<T extends Object>({

0 commit comments

Comments
 (0)