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

Tests and Fixes for Future instances #8

Merged
merged 4 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.3.1

* Fixed Documentation links.
* Some Futures instances behaviors fix.

## 0.3.0

* Migrating from flutter_test and flutter_lints to test and lints.
Expand Down
6 changes: 3 additions & 3 deletions lib/src/core/bean/dart_ddi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ abstract class DDI {
///
/// - **Order of Execution:** Decorators are applied in the order they are provided.
/// - **Instaces Already Gets:** No changes any Instances that have been get.
void addDecorator<BeanT extends Object>(
FutureOr<void> addDecorator<BeanT extends Object>(
List<BeanT Function(BeanT)> decorators,
{Object? qualifier});

Expand All @@ -212,7 +212,7 @@ abstract class DDI {
/// - **Around Constructor:** Will not work with Singletons Scope.
/// - **Order of Execution:** Interceptor are applied in the order they are provided.
/// - **Instaces Already Gets:** No changes any Instances that have been get.
void addInterceptor<BeanT extends Object>(
FutureOr<void> addInterceptor<BeanT extends Object>(
List<DDIInterceptor<BeanT> Function()> interceptors,
{Object? qualifier});

Expand All @@ -221,7 +221,7 @@ abstract class DDI {
/// When using this method, consider the following:
///
/// - **Instaces Already Gets:** No changes any Instances that have been get.
void refreshObject<BeanT extends Object>(
FutureOr<void> refreshObject<BeanT extends Object>(
BeanT register, {
Object? qualifier,
});
Expand Down
37 changes: 31 additions & 6 deletions lib/src/core/bean/dart_ddi_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class _DDIImpl implements DDI {

if (clazz is PostConstruct) {
clazz.onPostConstruct();
} else if (clazz is Future<PostConstruct>) {
_runFutureOrPostConstruct(clazz);
}

_beans[effectiveQualifierName] = FactoryClazz<BeanT>(
Expand Down Expand Up @@ -191,6 +193,8 @@ class _DDIImpl implements DDI {

if (register is PostConstruct) {
register.onPostConstruct();
} else if (register is Future<PostConstruct>) {
_runFutureOrPostConstruct(register);
}

_beans[effectiveQualifierName] = FactoryClazz<BeanT>(
Expand Down Expand Up @@ -244,6 +248,8 @@ class _DDIImpl implements DDI {

if (applicationClazz is PostConstruct) {
applicationClazz.onPostConstruct();
} else if (applicationClazz is Future<PostConstruct>) {
_runFutureOrPostConstruct(applicationClazz);
}

factoryClazz.clazzInstance = applicationClazz;
Expand Down Expand Up @@ -276,6 +282,8 @@ class _DDIImpl implements DDI {

if (dependentClazz is PostConstruct) {
dependentClazz.onPostConstruct();
} else if (dependentClazz is Future<PostConstruct>) {
_runFutureOrPostConstruct(dependentClazz);
}

if (factoryClazz.interceptors case final inter?) {
Expand Down Expand Up @@ -360,7 +368,7 @@ class _DDIImpl implements DDI {
_destroy<BeanT>(effectiveQualifierName);
}

void _destroy<BeanT>(effectiveQualifierName) {
void _destroy<BeanT>(Object effectiveQualifierName) {
if (_beans[effectiveQualifierName] case final factoryClazz?
when factoryClazz.destroyable) {
//Only destroy if destroyable was registered with true
Expand All @@ -371,8 +379,10 @@ class _DDIImpl implements DDI {
}
}

if (clazz is PreDestroy) {
clazz.onPreDestroy();
if (clazz is FutureOr<PreDestroy>) {
_runFutureOrPreDestroy(clazz, effectiveQualifierName);
//Should return because the _runFutureOrPreDestroy apply the remove
return;
}
}

Expand Down Expand Up @@ -462,7 +472,7 @@ class _DDIImpl implements DDI {
}

@override
void addDecorator<BeanT extends Object>(
FutureOr<void> addDecorator<BeanT extends Object>(
List<BeanT Function(BeanT)> decorators, {
Object? qualifier,
}) {
Expand Down Expand Up @@ -502,7 +512,7 @@ class _DDIImpl implements DDI {
}

@override
void addInterceptor<BeanT extends Object>(
FutureOr<void> addInterceptor<BeanT extends Object>(
List<DDIInterceptor<BeanT> Function()> interceptors, {
Object? qualifier,
}) {
Expand All @@ -520,7 +530,7 @@ class _DDIImpl implements DDI {
}

@override
void refreshObject<BeanT extends Object>(
FutureOr<void> refreshObject<BeanT extends Object>(
BeanT register, {
Object? qualifier,
}) {
Expand All @@ -541,4 +551,19 @@ class _DDIImpl implements DDI {
factoryClazz.clazzInstance =
_executarDecorators(register, factoryClazz.decorators);
}

void _runFutureOrPostConstruct(Future<PostConstruct> register) async {
final PostConstruct clazz = await register;

clazz.onPostConstruct();
}

Future<void> _runFutureOrPreDestroy(
FutureOr<PreDestroy> register, Object effectiveQualifierName) async {
final PreDestroy clazz = await register;

clazz.onPreDestroy();

_beans.remove(effectiveQualifierName);
}
}
2 changes: 1 addition & 1 deletion lib/src/exception/bean_not_found.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ class BeanNotFound implements Exception {

@override
String toString() {
return 'No Instance with Type $cause is found.';
return 'No Instance found with Type $cause.';
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: dart_ddi
description: "A Dependency Injection package, with Qualifier, Decorators, Interceptors, Events and more. Inspired by Java CDI and get_it."
version: 0.3.0
version: 0.3.1
homepage: https://github.com/Willian199/dart_ddi

environment:
Expand Down
Loading
Loading