Skip to content

Commit 9bd0893

Browse files
committed
feat: make CustomService interface optional
1 parent a7e8857 commit 9bd0893

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

lib/cognito/cognito_service.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ enum kSignInResult
1010
unknownError,
1111
}
1212

13-
abstract class AuthService extends CustomService {
13+
abstract class AuthService implements MyServiceInitializer {
1414

1515
Future<void> updateStoredPassword(String password);
1616
Future<void> updateStoredEmail(String email);

lib/my_service/my_service.dart

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,48 @@ class MyService {
2828

2929
MyService._();
3030

31-
static void register<T extends CustomService>(T Function() createInstance, {List<Type>? dependsOn})
31+
static void register<T extends Object>(T Function() createInstance, {List<Type>? dependsOn})
3232
{
3333
GetIt.instance.registerSingletonAsync<T>(() async
3434
{
3535
T instance = createInstance();
3636

37-
await instance.initialize();
37+
if (T is MyServiceInitializer)
38+
{
39+
await (instance as MyServiceInitializer).initialize();
40+
}
3841

3942
return instance;
4043
},
4144
dispose: (instance) async
4245
{
46+
if (instance is MyServiceInitializer)
47+
{
4348
await instance.dispose();
49+
}
4450
},
4551
dependsOn: dependsOn,
4652
);
4753
}
4854

49-
static Future<void> registerAndWaitUntilReady<T extends CustomService>(T Function() createInstance) async
55+
static Future<void> registerAndWaitUntilReady<T extends Object>(T Function() createInstance) async
5056
{
5157
T instance = createInstance();
5258

53-
await instance.initialize();
59+
if (T is MyServiceInitializer)
60+
{
61+
await (instance as MyServiceInitializer).initialize();
62+
}
5463

55-
GetIt.instance.registerSingleton<T>(instance);
64+
GetIt.instance.registerSingleton<T>(instance,
65+
dispose: (instance) async
66+
{
67+
if (instance is MyServiceInitializer)
68+
{
69+
await instance.dispose();
70+
}
71+
},
72+
);
5673
}
5774

5875

@@ -127,7 +144,7 @@ class MyService {
127144
}
128145

129146

130-
abstract class CustomService {
147+
abstract class MyServiceInitializer {
131148

132149
Future<void> initialize();
133150
Future<void> dispose();

0 commit comments

Comments
 (0)