Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Add dispose param to singleton registration methods #15

Merged
merged 5 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 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 @@ -150,10 +146,15 @@ class IocContainerBuilder {
);

///Add a singleton service to the container.
void addSingletonService<T>(T service) => addServiceDefinition(
void addSingletonService<T>(
T service, {
void Function(T service)? dispose,
}) =>
addServiceDefinition(
ServiceDefinition<T>(
(container) => service,
isSingleton: true,
dispose: dispose,
),
);

Expand All @@ -162,12 +163,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,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔

This looks correct. It seems like this was a actually a bug in the interface.

Sorry for the slow response, but I will get around to merging this soon.

Thanks 🙏🏼

),
);

Expand Down Expand Up @@ -204,12 +207,14 @@ class IocContainerBuilder {
void addSingletonAsync<T>(
jinyus marked this conversation as resolved.
Show resolved Hide resolved
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
8 changes: 7 additions & 1 deletion 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()
..addSingletonService(a)
..addSingletonService<A>(
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