diff --git a/angel_container/CHANGELOG.md b/angel_container/CHANGELOG.md index 87af614f..df7a4070 100644 --- a/angel_container/CHANGELOG.md +++ b/angel_container/CHANGELOG.md @@ -1,3 +1,6 @@ +# 1.0.2 +* Added `makeAsync`. + # 1.0.1 * Added `hasNamed`. diff --git a/angel_container/example/main.dart b/angel_container/example/main.dart index 47108b08..fb831581 100644 --- a/angel_container/example/main.dart +++ b/angel_container/example/main.dart @@ -1,7 +1,9 @@ +import 'dart:async'; + import 'package:angel_container/angel_container.dart'; import 'package:angel_container/mirrors.dart'; -void main() { +Future main() async { // Create a container instance. var container = new Container(const MirrorsReflector()); @@ -21,6 +23,13 @@ void main() { // Use `make` to create an instance. var truck = container.make(); + // You can also resolve injections asynchronously. + container.registerFactory>((_) async => 24); + print(await container.makeAsync()); + + // Asynchronous resolution also works for plain objects. + await container.makeAsync().then((t) => t.drive()); + // Register a named singleton. container.registerNamedSingleton('the_truck', truck); diff --git a/angel_container/lib/src/container.dart b/angel_container/lib/src/container.dart index a5b35332..27d0530c 100644 --- a/angel_container/lib/src/container.dart +++ b/angel_container/lib/src/container.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'exception.dart'; import 'reflector.dart'; @@ -54,6 +55,21 @@ class Container { return false; } + /// Instantiates an instance of [T], asynchronously. + /// + /// It is similar to [make], but resolves an injection of either + /// `Future` or `T`. + Future makeAsync() async { + if (has>()) { + return make>(); + } else if (has()) { + return Future.value(make()); + } else { + throw new ReflectionException( + 'No injection for Future<$T> or $T was found.'); + } + } + /// Instantiates an instance of [T]. /// /// In contexts where a static generic type cannot be used, use diff --git a/angel_container/pubspec.yaml b/angel_container/pubspec.yaml index e06a4572..c08f575d 100644 --- a/angel_container/pubspec.yaml +++ b/angel_container/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_container -version: 1.0.1 +version: 1.0.2 author: Tobe O description: "A better IoC container and dependency injector for Angel, ultimately allowing Angel to be used without dart:mirrors." homepage: https://github.com/angel-dart/container.git diff --git a/angel_container/test/common.dart b/angel_container/test/common.dart index 52b8a6d2..9ffc38c7 100644 --- a/angel_container/test/common.dart +++ b/angel_container/test/common.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:angel_container/angel_container.dart'; import 'package:test/test.dart'; @@ -10,6 +12,7 @@ void testReflector(Reflector reflector) { setUp(() { container = new Container(reflector); container.registerSingleton(blaziken); + container.registerFactory>((_) async => 46); }); test('get field', () { @@ -50,6 +53,14 @@ void testReflector(Reflector reflector) { expect(container.make(), blaziken); }); + test('make async returns async object', () async { + expect(container.makeAsync(), completion(46)); + }); + + test('make async returns sync object', () async { + expect(container.makeAsync(), completion(blaziken)); + }); + test('make on aliased singleton returns singleton', () { container.registerSingleton(blaziken, as: StateError); expect(container.make(StateError), blaziken);