Skip to content

Commit

Permalink
Merge pull request #15 from jinyus/patch-1
Browse files Browse the repository at this point in the history
[Fix] Add dispose param to singleton registration methods

Merged! Thankyou! 🙏🏼
  • Loading branch information
MelbourneDeveloper authored Jan 24, 2024
2 parents 8151725 + b4b73d3 commit d0fac94
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
18 changes: 9 additions & 9 deletions lib/ioc_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ class ServiceDefinition<T> {
this.isSingleton = false,
this.dispose,
this.disposeAsync,
}) : assert(
!isSingleton || dispose == null,
'Singleton factories cannot have a dispose method',
),
assert(
}) : assert(
dispose == null || disposeAsync == null,
"Service definitions can't have both dispose and disposeAsync",
);
Expand Down Expand Up @@ -154,12 +150,14 @@ class IocContainerBuilder {
void addSingleton<T>(
T Function(
IocContainer container,
) factory,
) =>
) factory, {
void Function(T service)? dispose,
}) =>
addServiceDefinition<T>(
ServiceDefinition<T>(
(container) => factory(container),
isSingleton: true,
dispose: dispose,
),
);

Expand Down Expand Up @@ -196,12 +194,14 @@ class IocContainerBuilder {
void addSingletonAsync<T>(
Future<T> Function(
IocContainer container,
) factory,
) =>
) factory, {
Future<void> Function(T service)? disposeAsync,
}) =>
addServiceDefinition<Future<T>>(
ServiceDefinition<Future<T>>(
isSingleton: true,
(container) async => factory(container),
disposeAsync: (service) async => disposeAsync?.call(await service),
),
);
}
Expand Down
16 changes: 14 additions & 2 deletions test/ioc_container_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ extension TestIocContainerExtensions on IocContainer {
class A {
A(this.name);
final String name;
bool disposed = false;
void dispose() => disposed = true;
}

class B {
Expand Down Expand Up @@ -157,7 +159,10 @@ void main() {
test('With Scoping And Disposing', () async {
final a = A('a');
final builder = IocContainerBuilder()
..addSingleton((c) => a)
..addSingleton<A>(
(c) => a,
dispose: (a) => a.dispose(),
)
..add((i) => B(i.get<A>()))
..add<C>(
(i) => C(i.get<B>()),
Expand All @@ -177,6 +182,7 @@ void main() {
//This also works if we don't use await above
//await Future<void>.delayed(const Duration(milliseconds: 100));

expect(a.disposed, true);
expect(d.disposed, true);
expect(d.c.disposed, true);
expect(container<D>().disposed, false);
Expand Down Expand Up @@ -878,7 +884,7 @@ void main() {
);
});

test('Test addAsyncSingleton', () async {
test('Test addAsyncSingleton With Dispose', () async {
var futureCounter = 0;

final builder = IocContainerBuilder()
Expand All @@ -892,6 +898,7 @@ void main() {
return A('a');
},
),
disposeAsync: (service) async => service.dispose(),
);
final container = builder.toContainer();

Expand All @@ -911,5 +918,10 @@ void main() {

//Expect the future only ran once
expect(futureCounter, 1);

final scope = container.scoped(useExistingSingletons: true);
await scope.dispose();

expect(as[0].disposed, true);
});
}

0 comments on commit d0fac94

Please sign in to comment.