Add makeAsync
This commit is contained in:
parent
0144ebe34e
commit
41df6967c4
5 changed files with 41 additions and 2 deletions
|
@ -1,3 +1,6 @@
|
||||||
|
# 1.0.2
|
||||||
|
* Added `makeAsync<T>`.
|
||||||
|
|
||||||
# 1.0.1
|
# 1.0.1
|
||||||
* Added `hasNamed`.
|
* Added `hasNamed`.
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:angel_container/angel_container.dart';
|
import 'package:angel_container/angel_container.dart';
|
||||||
import 'package:angel_container/mirrors.dart';
|
import 'package:angel_container/mirrors.dart';
|
||||||
|
|
||||||
void main() {
|
Future<void> main() async {
|
||||||
// Create a container instance.
|
// Create a container instance.
|
||||||
var container = new Container(const MirrorsReflector());
|
var container = new Container(const MirrorsReflector());
|
||||||
|
|
||||||
|
@ -21,6 +23,13 @@ void main() {
|
||||||
// Use `make` to create an instance.
|
// Use `make` to create an instance.
|
||||||
var truck = container.make<Truck>();
|
var truck = container.make<Truck>();
|
||||||
|
|
||||||
|
// You can also resolve injections asynchronously.
|
||||||
|
container.registerFactory<Future<int>>((_) async => 24);
|
||||||
|
print(await container.makeAsync<int>());
|
||||||
|
|
||||||
|
// Asynchronous resolution also works for plain objects.
|
||||||
|
await container.makeAsync<Truck>().then((t) => t.drive());
|
||||||
|
|
||||||
// Register a named singleton.
|
// Register a named singleton.
|
||||||
container.registerNamedSingleton('the_truck', truck);
|
container.registerNamedSingleton('the_truck', truck);
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import 'dart:async';
|
||||||
import 'exception.dart';
|
import 'exception.dart';
|
||||||
import 'reflector.dart';
|
import 'reflector.dart';
|
||||||
|
|
||||||
|
@ -54,6 +55,21 @@ class Container {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Instantiates an instance of [T], asynchronously.
|
||||||
|
///
|
||||||
|
/// It is similar to [make], but resolves an injection of either
|
||||||
|
/// `Future<T>` or `T`.
|
||||||
|
Future<T> makeAsync<T>() async {
|
||||||
|
if (has<Future<T>>()) {
|
||||||
|
return make<Future<T>>();
|
||||||
|
} else if (has<T>()) {
|
||||||
|
return Future<T>.value(make<T>());
|
||||||
|
} else {
|
||||||
|
throw new ReflectionException(
|
||||||
|
'No injection for Future<$T> or $T was found.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Instantiates an instance of [T].
|
/// Instantiates an instance of [T].
|
||||||
///
|
///
|
||||||
/// In contexts where a static generic type cannot be used, use
|
/// In contexts where a static generic type cannot be used, use
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
name: angel_container
|
name: angel_container
|
||||||
version: 1.0.1
|
version: 1.0.2
|
||||||
author: Tobe O <thosakwe@gmail.com>
|
author: Tobe O <thosakwe@gmail.com>
|
||||||
description: "A better IoC container and dependency injector for Angel, ultimately allowing Angel to be used without dart:mirrors."
|
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
|
homepage: https://github.com/angel-dart/container.git
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:angel_container/angel_container.dart';
|
import 'package:angel_container/angel_container.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
@ -10,6 +12,7 @@ void testReflector(Reflector reflector) {
|
||||||
setUp(() {
|
setUp(() {
|
||||||
container = new Container(reflector);
|
container = new Container(reflector);
|
||||||
container.registerSingleton(blaziken);
|
container.registerSingleton(blaziken);
|
||||||
|
container.registerFactory<Future<int>>((_) async => 46);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('get field', () {
|
test('get field', () {
|
||||||
|
@ -50,6 +53,14 @@ void testReflector(Reflector reflector) {
|
||||||
expect(container.make<Pokemon>(), blaziken);
|
expect(container.make<Pokemon>(), blaziken);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('make async returns async object', () async {
|
||||||
|
expect(container.makeAsync<int>(), completion(46));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('make async returns sync object', () async {
|
||||||
|
expect(container.makeAsync<Pokemon>(), completion(blaziken));
|
||||||
|
});
|
||||||
|
|
||||||
test('make on aliased singleton returns singleton', () {
|
test('make on aliased singleton returns singleton', () {
|
||||||
container.registerSingleton(blaziken, as: StateError);
|
container.registerSingleton(blaziken, as: StateError);
|
||||||
expect(container.make(StateError), blaziken);
|
expect(container.make(StateError), blaziken);
|
||||||
|
|
Loading…
Reference in a new issue