feat(di): provide two ways to create an injector, resolved and unresolved
Add two factory static functions to Injector: resolveAndCreate and fromResolvedBindings. We want to avoid resolution and flattening every time we create a new injector. This commit allows the user to cache resolved bindings and reuse them.
This commit is contained in:
parent
6c8398df9b
commit
4a961f4ecb
|
@ -43,7 +43,7 @@ class Car {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var inj = new Injector([
|
var inj = Injector.resolveAndCreate([
|
||||||
bind(Car).toClass(Car),
|
bind(Car).toClass(Car),
|
||||||
bind(Engine).toClass(Engine)
|
bind(Engine).toClass(Engine)
|
||||||
]);
|
]);
|
||||||
|
@ -86,7 +86,7 @@ To avoid bugs make sure the registered objects have side-effect-free constructor
|
||||||
Injectors are hierarchical.
|
Injectors are hierarchical.
|
||||||
|
|
||||||
```
|
```
|
||||||
var child = injector.createChild([
|
var child = injector.resolveAndCreateChild([
|
||||||
bind(Engine).toClass(TurboEngine)
|
bind(Engine).toClass(TurboEngine)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -99,21 +99,21 @@ var car = child.get(Car); // uses the Car binding from the parent injector and E
|
||||||
You can bind to a class, a value, or a factory. It is also possible to alias existing bindings.
|
You can bind to a class, a value, or a factory. It is also possible to alias existing bindings.
|
||||||
|
|
||||||
```
|
```
|
||||||
var inj = new Injector([
|
var inj = Injector.resolveAndCreate([
|
||||||
bind(Car).toClass(Car),
|
bind(Car).toClass(Car),
|
||||||
bind(Engine).toClass(Engine)
|
bind(Engine).toClass(Engine)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
var inj = new Injector([
|
var inj = Injector.resolveAndCreate([
|
||||||
Car, // syntax sugar for bind(Car).toClass(Car)
|
Car, // syntax sugar for bind(Car).toClass(Car)
|
||||||
Engine
|
Engine
|
||||||
]);
|
]);
|
||||||
|
|
||||||
var inj = new Injector([
|
var inj = Injector.resolveAndCreate([
|
||||||
bind(Car).toValue(new Car(new Engine()))
|
bind(Car).toValue(new Car(new Engine()))
|
||||||
]);
|
]);
|
||||||
|
|
||||||
var inj = new Injector([
|
var inj = Injector.resolveAndCreate([
|
||||||
bind(Car).toFactory((e) => new Car(e), [Engine]),
|
bind(Car).toFactory((e) => new Car(e), [Engine]),
|
||||||
bind(Engine).toFactory(() => new Engine())
|
bind(Engine).toFactory(() => new Engine())
|
||||||
]);
|
]);
|
||||||
|
@ -122,7 +122,7 @@ var inj = new Injector([
|
||||||
You can bind any token.
|
You can bind any token.
|
||||||
|
|
||||||
```
|
```
|
||||||
var inj = new Injector([
|
var inj = Injector.resolveAndCreate([
|
||||||
bind(Car).toFactory((e) => new Car(), ["engine!"]),
|
bind(Car).toFactory((e) => new Car(), ["engine!"]),
|
||||||
bind("engine!").toClass(Engine)
|
bind("engine!").toClass(Engine)
|
||||||
]);
|
]);
|
||||||
|
@ -131,7 +131,7 @@ var inj = new Injector([
|
||||||
If you want to alias an existing binding, you can do so using `toAlias`:
|
If you want to alias an existing binding, you can do so using `toAlias`:
|
||||||
|
|
||||||
```
|
```
|
||||||
var inj = new Injector([
|
var inj = Injector.resolveAndCreate([
|
||||||
bind(Engine).toClass(Engine),
|
bind(Engine).toClass(Engine),
|
||||||
bind("engine!").toAlias(Engine)
|
bind("engine!").toAlias(Engine)
|
||||||
]);
|
]);
|
||||||
|
@ -152,7 +152,7 @@ The `someFactory` function does not have to know that it creates an object for `
|
||||||
Injector can create binding on the fly if we enable default bindings.
|
Injector can create binding on the fly if we enable default bindings.
|
||||||
|
|
||||||
```
|
```
|
||||||
var inj = new Injector([], {defaultBindings: true});
|
var inj = Injector.resolveAndCreate([], {defaultBindings: true});
|
||||||
var car = inj.get(Car); //this works as if `bind(Car).toClass(Car)` and `bind(Engine).toClass(Engine)` were present.
|
var car = inj.get(Car); //this works as if `bind(Car).toClass(Car)` and `bind(Engine).toClass(Engine)` were present.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -226,7 +226,7 @@ class UserController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var inj = new Injector([
|
var inj = Injector.resolveAndCreate([
|
||||||
bind(UserList).toAsyncFactory(() => fetchUsersUsingHttp().then((u) => new UserList(u))),
|
bind(UserList).toAsyncFactory(() => fetchUsersUsingHttp().then((u) => new UserList(u))),
|
||||||
UserController
|
UserController
|
||||||
])
|
])
|
||||||
|
@ -252,7 +252,7 @@ class UserController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var inj = new Injector([
|
var inj = Injector.resolveAndCreate([
|
||||||
bind(UserList).toAsyncFactory(() => fetchUsersUsingHttp().then((u) => new UserList(u))),
|
bind(UserList).toAsyncFactory(() => fetchUsersUsingHttp().then((u) => new UserList(u))),
|
||||||
UserController
|
UserController
|
||||||
])
|
])
|
||||||
|
@ -276,7 +276,7 @@ class UserController {
|
||||||
constructor(ul:UserList){}
|
constructor(ul:UserList){}
|
||||||
}
|
}
|
||||||
|
|
||||||
var inj = new Injector([UserList, UserController]);
|
var inj = Injector.resolveAndCreate([UserList, UserController]);
|
||||||
var ctrl:UserController = inj.get(UserController);
|
var ctrl:UserController = inj.get(UserController);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -290,7 +290,7 @@ class UserController {
|
||||||
constructor(@InjectPromise(UserList) ul){}
|
constructor(@InjectPromise(UserList) ul){}
|
||||||
}
|
}
|
||||||
|
|
||||||
var inj = new Injector([UserList, UserController]);
|
var inj = Injector.resolveAndCreate([UserList, UserController]);
|
||||||
var ctrl:UserController = inj.get(UserController);
|
var ctrl:UserController = inj.get(UserController);
|
||||||
// UserController responsible for dealing with asynchrony.
|
// UserController responsible for dealing with asynchrony.
|
||||||
expect(ctrl.ul).toBePromise();
|
expect(ctrl.ul).toBePromise();
|
||||||
|
@ -306,7 +306,7 @@ class UserController {
|
||||||
constructor(ul:UserList){}
|
constructor(ul:UserList){}
|
||||||
}
|
}
|
||||||
|
|
||||||
var inj = new Injector([
|
var inj = Injector.resolveAndCreate([
|
||||||
bind(UserList).toAsyncFactory(() => fetchUsersUsingHttp().then((u) => new UserList(u))),
|
bind(UserList).toAsyncFactory(() => fetchUsersUsingHttp().then((u) => new UserList(u))),
|
||||||
UserController
|
UserController
|
||||||
]);
|
]);
|
||||||
|
@ -331,7 +331,7 @@ class UserController {
|
||||||
constructor(@InjectPromise(UserList) ul){}
|
constructor(@InjectPromise(UserList) ul){}
|
||||||
}
|
}
|
||||||
|
|
||||||
var inj = new Injector([
|
var inj = Injector.resolveAndCreate([
|
||||||
bind(UserList).toAsyncFactory(() => fetchUsersUsingHttp().then((u) => new UserList(u))),
|
bind(UserList).toAsyncFactory(() => fetchUsersUsingHttp().then((u) => new UserList(u))),
|
||||||
UserController
|
UserController
|
||||||
]);
|
]);
|
||||||
|
@ -369,14 +369,14 @@ If we need a transient dependency, something that we want a new instance of ever
|
||||||
We can create a child injector:
|
We can create a child injector:
|
||||||
|
|
||||||
```
|
```
|
||||||
var child = inj.createChild([MyClass]);
|
var child = inj.resolveAndCreateChild([MyClass]);
|
||||||
child.get(MyClass);
|
child.get(MyClass);
|
||||||
```
|
```
|
||||||
|
|
||||||
Or we can register a factory function:
|
Or we can register a factory function:
|
||||||
|
|
||||||
```
|
```
|
||||||
var inj = new Injector([
|
var inj = Injector.resolveAndCreate([
|
||||||
bind('MyClassFactory').toFactory(dep => () => new MyClass(dep), [SomeDependency])
|
bind('MyClassFactory').toFactory(dep => () => new MyClass(dep), [SomeDependency])
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -393,7 +393,7 @@ expect(instance1).not.toBe(instance2);
|
||||||
Most of the time we do not have to deal with keys.
|
Most of the time we do not have to deal with keys.
|
||||||
|
|
||||||
```
|
```
|
||||||
var inj = new Injector([
|
var inj = Injector.resolveAndCreate([
|
||||||
bind(Engine).toFactory(() => new TurboEngine()) //the passed in token Engine gets mapped to a key
|
bind(Engine).toFactory(() => new TurboEngine()) //the passed in token Engine gets mapped to a key
|
||||||
]);
|
]);
|
||||||
var engine = inj.get(Engine); //the passed in token Engine gets mapped to a key
|
var engine = inj.get(Engine); //the passed in token Engine gets mapped to a key
|
||||||
|
@ -404,7 +404,7 @@ Now, the same example, but with keys
|
||||||
```
|
```
|
||||||
var ENGINE_KEY = Key.get(Engine);
|
var ENGINE_KEY = Key.get(Engine);
|
||||||
|
|
||||||
var inj = new Injector([
|
var inj = Injector.resolveAndCreate([
|
||||||
bind(ENGINE_KEY).toFactory(() => new TurboEngine()) // no mapping
|
bind(ENGINE_KEY).toFactory(() => new TurboEngine()) // no mapping
|
||||||
]);
|
]);
|
||||||
var engine = inj.get(ENGINE_KEY); // no mapping
|
var engine = inj.get(ENGINE_KEY); // no mapping
|
||||||
|
|
|
@ -276,10 +276,10 @@ export function bootstrap(appComponentType: Type,
|
||||||
}
|
}
|
||||||
|
|
||||||
function _createAppInjector(appComponentType: Type, bindings: List<Binding>, zone: VmTurnZone): Injector {
|
function _createAppInjector(appComponentType: Type, bindings: List<Binding>, zone: VmTurnZone): Injector {
|
||||||
if (isBlank(_rootInjector)) _rootInjector = new Injector(_rootBindings);
|
if (isBlank(_rootInjector)) _rootInjector = Injector.resolveAndCreate(_rootBindings);
|
||||||
var mergedBindings = isPresent(bindings) ?
|
var mergedBindings = isPresent(bindings) ?
|
||||||
ListWrapper.concat(_injectorBindings(appComponentType), bindings) :
|
ListWrapper.concat(_injectorBindings(appComponentType), bindings) :
|
||||||
_injectorBindings(appComponentType);
|
_injectorBindings(appComponentType);
|
||||||
ListWrapper.push(mergedBindings, bind(VmTurnZone).toValue(zone));
|
ListWrapper.push(mergedBindings, bind(VmTurnZone).toValue(zone));
|
||||||
return _rootInjector.createChild(mergedBindings);
|
return _rootInjector.resolveAndCreateChild(mergedBindings);
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,7 +80,7 @@ export class DynamicComponentLoader {
|
||||||
|
|
||||||
_componentAppInjector(location, injector, services) {
|
_componentAppInjector(location, injector, services) {
|
||||||
var inj = isPresent(injector) ? injector : location.injector;
|
var inj = isPresent(injector) ? injector : location.injector;
|
||||||
return isPresent(services) ? inj.createChild(services) : inj;
|
return isPresent(services) ? inj.resolveAndCreateChild(services) : inj;
|
||||||
}
|
}
|
||||||
|
|
||||||
_instantiateAndHydrateView(protoView, injector, hostElementInjector, context) {
|
_instantiateAndHydrateView(protoView, injector, hostElementInjector, context) {
|
||||||
|
|
|
@ -153,7 +153,7 @@ export class AppView {
|
||||||
if (isPresent(componentDirective)) {
|
if (isPresent(componentDirective)) {
|
||||||
var injectables = componentDirective.annotation.injectables;
|
var injectables = componentDirective.annotation.injectables;
|
||||||
if (isPresent(injectables))
|
if (isPresent(injectables))
|
||||||
shadowDomAppInjector = appInjector.createChild(injectables);
|
shadowDomAppInjector = appInjector.resolveAndCreateChild(injectables);
|
||||||
else {
|
else {
|
||||||
shadowDomAppInjector = appInjector;
|
shadowDomAppInjector = appInjector;
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,7 +94,9 @@ export class Binding {
|
||||||
for (var i = 0; i < bindings.length; i++) {
|
for (var i = 0; i < bindings.length; i++) {
|
||||||
var unresolved = bindings[i];
|
var unresolved = bindings[i];
|
||||||
var resolved;
|
var resolved;
|
||||||
if (unresolved instanceof Type) {
|
if (unresolved instanceof ResolvedBinding) {
|
||||||
|
resolved = unresolved; // ha-ha! I'm easily amused
|
||||||
|
} else if (unresolved instanceof Type) {
|
||||||
resolved = bind(unresolved).toClass(unresolved).resolve();
|
resolved = bind(unresolved).toClass(unresolved).resolve();
|
||||||
} else if (unresolved instanceof Binding) {
|
} else if (unresolved instanceof Binding) {
|
||||||
resolved = unresolved.resolve();
|
resolved = unresolved.resolve();
|
||||||
|
|
|
@ -27,13 +27,39 @@ export class Injector {
|
||||||
_defaultBindings:boolean;
|
_defaultBindings:boolean;
|
||||||
_asyncStrategy: _AsyncInjectorStrategy;
|
_asyncStrategy: _AsyncInjectorStrategy;
|
||||||
_syncStrategy:_SyncInjectorStrategy;
|
_syncStrategy:_SyncInjectorStrategy;
|
||||||
constructor(bindings:List, {parent=null, defaultBindings=false}={}) {
|
|
||||||
|
/**
|
||||||
|
* Creates/looks up factory functions and dependencies from binding
|
||||||
|
* declarations and flattens bindings into a single [List].
|
||||||
|
*/
|
||||||
|
static resolve(bindings:List/*<ResolvedBinding|Binding|Type|List>*/):List<ResolvedBinding> {
|
||||||
var flatten = _flattenBindings(Binding.resolveAll(bindings), MapWrapper.create());
|
var flatten = _flattenBindings(Binding.resolveAll(bindings), MapWrapper.create());
|
||||||
this._bindings = this._createListOfBindings(flatten);
|
return _createListOfBindings(flatten);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolves bindings and creates an injector based on those bindings. This function is slower than the
|
||||||
|
* corresponding [fromResolvedBindings] because it needs to resolve bindings. Prefer [fromResolvedBindings]
|
||||||
|
* in performance-critical code that creates lots of injectors.
|
||||||
|
*/
|
||||||
|
static resolveAndCreate(bindings:List/*<ResolvedBinding|Binding|Type|List>*/, {defaultBindings=false}={}) {
|
||||||
|
return new Injector(Injector.resolve(bindings), null, defaultBindings);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an injector from previously resolved bindings. This bypasses a lot
|
||||||
|
* of computation and is the recommended way to construct injectors in
|
||||||
|
* performance-sensitive parts.
|
||||||
|
*/
|
||||||
|
static fromResolvedBindings(bindings:List<ResolvedBinding>, {defaultBindings=false}={}) {
|
||||||
|
return new Injector(bindings, null, defaultBindings);
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(bindings:List<ResolvedBinding>, parent:Injector, defaultBindings:boolean) {
|
||||||
|
this._bindings = bindings;
|
||||||
this._instances = this._createInstances();
|
this._instances = this._createInstances();
|
||||||
this._parent = parent;
|
this._parent = parent;
|
||||||
this._defaultBindings = defaultBindings;
|
this._defaultBindings = defaultBindings;
|
||||||
|
|
||||||
this._asyncStrategy = new _AsyncInjectorStrategy(this);
|
this._asyncStrategy = new _AsyncInjectorStrategy(this);
|
||||||
this._syncStrategy = new _SyncInjectorStrategy(this);
|
this._syncStrategy = new _SyncInjectorStrategy(this);
|
||||||
}
|
}
|
||||||
|
@ -50,15 +76,12 @@ export class Injector {
|
||||||
return this._getByKey(Key.get(token), true, false, false);
|
return this._getByKey(Key.get(token), true, false, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
createChild(bindings:List):Injector {
|
resolveAndCreateChild(bindings:List/*<ResolvedBinding|Binding|Type|List>*/):Injector {
|
||||||
return new Injector(bindings, {parent: this});
|
return new Injector(Injector.resolve(bindings), this, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
createChildFromResolved(bindings:List<ResolvedBinding>):Injector {
|
||||||
_createListOfBindings(flattenBindings):List {
|
return new Injector(bindings, this, false);
|
||||||
var bindings = ListWrapper.createFixedSize(Key.numberOfKeys + 1);
|
|
||||||
MapWrapper.forEach(flattenBindings, (v, keyId) => bindings[keyId] = v);
|
|
||||||
return bindings;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_createInstances():List {
|
_createInstances():List {
|
||||||
|
@ -244,6 +267,14 @@ class _AsyncInjectorStrategy {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function _createListOfBindings(flattenBindings):List {
|
||||||
|
var bindings = ListWrapper.createFixedSize(Key.numberOfKeys + 1);
|
||||||
|
MapWrapper.forEach(flattenBindings, (v, keyId) => bindings[keyId] = v);
|
||||||
|
return bindings;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function _flattenBindings(bindings:List, res:Map) {
|
function _flattenBindings(bindings:List, res:Map) {
|
||||||
ListWrapper.forEach(bindings, function (b) {
|
ListWrapper.forEach(bindings, function (b) {
|
||||||
if (b instanceof ResolvedBinding) {
|
if (b instanceof ResolvedBinding) {
|
||||||
|
|
|
@ -110,8 +110,8 @@ function _getAppBindings() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createTestInjector(bindings: List) {
|
export function createTestInjector(bindings: List) {
|
||||||
var rootInjector = new Injector(_getRootBindings());
|
var rootInjector = Injector.resolveAndCreate(_getRootBindings());
|
||||||
return rootInjector.createChild(ListWrapper.concat(_getAppBindings(), bindings));
|
return rootInjector.resolveAndCreateChild(ListWrapper.concat(_getAppBindings(), bindings));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -209,7 +209,7 @@ class TestNode extends TreeNode {
|
||||||
|
|
||||||
export function main() {
|
export function main() {
|
||||||
var defaultPreBuiltObjects = new PreBuiltObjects(null, null, null, null);
|
var defaultPreBuiltObjects = new PreBuiltObjects(null, null, null, null);
|
||||||
var appInjector = new Injector([]);
|
var appInjector = Injector.resolveAndCreate([]);
|
||||||
|
|
||||||
function humanize(tree, names:List) {
|
function humanize(tree, names:List) {
|
||||||
var lookupName = (item) =>
|
var lookupName = (item) =>
|
||||||
|
@ -236,7 +236,7 @@ export function main() {
|
||||||
function parentChildInjectors(parentBindings, childBindings, parentPreBuildObjects = null) {
|
function parentChildInjectors(parentBindings, childBindings, parentPreBuildObjects = null) {
|
||||||
if (isBlank(parentPreBuildObjects)) parentPreBuildObjects = defaultPreBuiltObjects;
|
if (isBlank(parentPreBuildObjects)) parentPreBuildObjects = defaultPreBuiltObjects;
|
||||||
|
|
||||||
var inj = new Injector([]);
|
var inj = Injector.resolveAndCreate([]);
|
||||||
|
|
||||||
var protoParent = new ProtoElementInjector(null, 0, parentBindings);
|
var protoParent = new ProtoElementInjector(null, 0, parentBindings);
|
||||||
var parent = protoParent.instantiate(null);
|
var parent = protoParent.instantiate(null);
|
||||||
|
@ -253,8 +253,8 @@ export function main() {
|
||||||
function hostShadowInjectors(hostBindings, shadowBindings, hostPreBuildObjects = null) {
|
function hostShadowInjectors(hostBindings, shadowBindings, hostPreBuildObjects = null) {
|
||||||
if (isBlank(hostPreBuildObjects)) hostPreBuildObjects = defaultPreBuiltObjects;
|
if (isBlank(hostPreBuildObjects)) hostPreBuildObjects = defaultPreBuiltObjects;
|
||||||
|
|
||||||
var inj = new Injector([]);
|
var inj = Injector.resolveAndCreate([]);
|
||||||
var shadowInj = inj.createChild([]);
|
var shadowInj = inj.resolveAndCreateChild([]);
|
||||||
|
|
||||||
var protoParent = new ProtoElementInjector(null, 0, hostBindings, true);
|
var protoParent = new ProtoElementInjector(null, 0, hostBindings, true);
|
||||||
var host = protoParent.instantiate(null);
|
var host = protoParent.instantiate(null);
|
||||||
|
@ -455,7 +455,7 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should instantiate directives that depend on app services", function () {
|
it("should instantiate directives that depend on app services", function () {
|
||||||
var appInjector = new Injector([
|
var appInjector = Injector.resolveAndCreate([
|
||||||
bind("service").toValue("service")
|
bind("service").toValue("service")
|
||||||
]);
|
]);
|
||||||
var inj = injector([NeedsService], appInjector);
|
var inj = injector([NeedsService], appInjector);
|
||||||
|
@ -487,7 +487,7 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should instantiate component directives that depend on app services in the shadow app injector", () => {
|
it("should instantiate component directives that depend on app services in the shadow app injector", () => {
|
||||||
var shadowAppInjector = new Injector([
|
var shadowAppInjector = Injector.resolveAndCreate([
|
||||||
bind("service").toValue("service")
|
bind("service").toValue("service")
|
||||||
]);
|
]);
|
||||||
var inj = injector([NeedsService], null, shadowAppInjector);
|
var inj = injector([NeedsService], null, shadowAppInjector);
|
||||||
|
@ -498,7 +498,7 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should not instantiate other directives that depend on app services in the shadow app injector", () => {
|
it("should not instantiate other directives that depend on app services in the shadow app injector", () => {
|
||||||
var shadowAppInjector = new Injector([
|
var shadowAppInjector = Injector.resolveAndCreate([
|
||||||
bind("service").toValue("service")
|
bind("service").toValue("service")
|
||||||
]);
|
]);
|
||||||
expect(() => {
|
expect(() => {
|
||||||
|
@ -507,7 +507,7 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return app services", function () {
|
it("should return app services", function () {
|
||||||
var appInjector = new Injector([
|
var appInjector = Injector.resolveAndCreate([
|
||||||
bind("service").toValue("service")
|
bind("service").toValue("service")
|
||||||
]);
|
]);
|
||||||
var inj = injector([], appInjector);
|
var inj = injector([], appInjector);
|
||||||
|
@ -687,7 +687,7 @@ export function main() {
|
||||||
|
|
||||||
it("should inject services of the dynamically-loaded component", () => {
|
it("should inject services of the dynamically-loaded component", () => {
|
||||||
var inj = injector([]);
|
var inj = injector([]);
|
||||||
var appInjector = new Injector([bind("service").toValue("Service")]);
|
var appInjector = Injector.resolveAndCreate([bind("service").toValue("Service")]);
|
||||||
inj.dynamicallyCreateComponent(NeedsService, null, appInjector);
|
inj.dynamicallyCreateComponent(NeedsService, null, appInjector);
|
||||||
expect(inj.getDynamicallyLoadedComponent().service).toEqual("Service");
|
expect(inj.getDynamicallyLoadedComponent().service).toEqual("Service");
|
||||||
});
|
});
|
||||||
|
@ -844,8 +844,8 @@ export function main() {
|
||||||
|
|
||||||
var parent = protoParent.instantiate(null);
|
var parent = protoParent.instantiate(null);
|
||||||
var child = protoChild.instantiate(parent);
|
var child = protoChild.instantiate(parent);
|
||||||
parent.instantiateDirectives(new Injector([]), null, null, preBuildObjects);
|
parent.instantiateDirectives(Injector.resolveAndCreate([]), null, null, preBuildObjects);
|
||||||
child.instantiateDirectives(new Injector([]), null, null, preBuildObjects);
|
child.instantiateDirectives(Injector.resolveAndCreate([]), null, null, preBuildObjects);
|
||||||
|
|
||||||
expectDirectives(parent.get(NeedsQuery).query, CountingDirective, [0,1]);
|
expectDirectives(parent.get(NeedsQuery).query, CountingDirective, [0,1]);
|
||||||
});
|
});
|
||||||
|
@ -856,8 +856,8 @@ export function main() {
|
||||||
|
|
||||||
var parent = protoParent.instantiate(null);
|
var parent = protoParent.instantiate(null);
|
||||||
var child = protoChild.instantiate(parent);
|
var child = protoChild.instantiate(parent);
|
||||||
parent.instantiateDirectives(new Injector([]), null, null, preBuildObjects);
|
parent.instantiateDirectives(Injector.resolveAndCreate([]), null, null, preBuildObjects);
|
||||||
child.instantiateDirectives(new Injector([]), null, null, preBuildObjects);
|
child.instantiateDirectives(Injector.resolveAndCreate([]), null, null, preBuildObjects);
|
||||||
|
|
||||||
child.unlink();
|
child.unlink();
|
||||||
|
|
||||||
|
@ -873,9 +873,9 @@ export function main() {
|
||||||
var child1 = protoChild1.instantiate(parent);
|
var child1 = protoChild1.instantiate(parent);
|
||||||
var child2 = protoChild2.instantiate(parent);
|
var child2 = protoChild2.instantiate(parent);
|
||||||
|
|
||||||
parent.instantiateDirectives(new Injector([]), null, null, preBuildObjects);
|
parent.instantiateDirectives(Injector.resolveAndCreate([]), null, null, preBuildObjects);
|
||||||
child1.instantiateDirectives(new Injector([]), null, null, preBuildObjects);
|
child1.instantiateDirectives(Injector.resolveAndCreate([]), null, null, preBuildObjects);
|
||||||
child2.instantiateDirectives(new Injector([]), null, null, preBuildObjects);
|
child2.instantiateDirectives(Injector.resolveAndCreate([]), null, null, preBuildObjects);
|
||||||
|
|
||||||
child1.unlink();
|
child1.unlink();
|
||||||
child1.link(parent);
|
child1.link(parent);
|
||||||
|
@ -893,9 +893,9 @@ export function main() {
|
||||||
var child1 = protoChild1.instantiate(parent);
|
var child1 = protoChild1.instantiate(parent);
|
||||||
var child2 = protoChild2.instantiate(parent);
|
var child2 = protoChild2.instantiate(parent);
|
||||||
|
|
||||||
parent.instantiateDirectives(new Injector([]), null, null, preBuildObjects);
|
parent.instantiateDirectives(Injector.resolveAndCreate([]), null, null, preBuildObjects);
|
||||||
child1.instantiateDirectives(new Injector([]), null, null, preBuildObjects);
|
child1.instantiateDirectives(Injector.resolveAndCreate([]), null, null, preBuildObjects);
|
||||||
child2.instantiateDirectives(new Injector([]), null, null, preBuildObjects);
|
child2.instantiateDirectives(Injector.resolveAndCreate([]), null, null, preBuildObjects);
|
||||||
|
|
||||||
child2.unlink();
|
child2.unlink();
|
||||||
child2.linkAfter(parent, null);
|
child2.linkAfter(parent, null);
|
||||||
|
@ -913,9 +913,9 @@ export function main() {
|
||||||
var parent = protoParent.instantiate(grandParent);
|
var parent = protoParent.instantiate(grandParent);
|
||||||
var child = protoChild.instantiate(parent);
|
var child = protoChild.instantiate(parent);
|
||||||
|
|
||||||
grandParent.instantiateDirectives(new Injector([]), null, null, preBuildObjects);
|
grandParent.instantiateDirectives(Injector.resolveAndCreate([]), null, null, preBuildObjects);
|
||||||
parent.instantiateDirectives(new Injector([]), null, null, preBuildObjects);
|
parent.instantiateDirectives(Injector.resolveAndCreate([]), null, null, preBuildObjects);
|
||||||
child.instantiateDirectives(new Injector([]), null, null, preBuildObjects);
|
child.instantiateDirectives(Injector.resolveAndCreate([]), null, null, preBuildObjects);
|
||||||
|
|
||||||
var queryList1 = grandParent.get(NeedsQuery).query;
|
var queryList1 = grandParent.get(NeedsQuery).query;
|
||||||
var queryList2 = parent.get(NeedsQuery).query;
|
var queryList2 = parent.get(NeedsQuery).query;
|
||||||
|
|
|
@ -41,7 +41,7 @@ export function main() {
|
||||||
|
|
||||||
describe("asyncGet", function () {
|
describe("asyncGet", function () {
|
||||||
it('should return a promise', function () {
|
it('should return a promise', function () {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
bind(UserList).toAsyncFactory(fetchUsers)
|
bind(UserList).toAsyncFactory(fetchUsers)
|
||||||
]);
|
]);
|
||||||
var p = injector.asyncGet(UserList);
|
var p = injector.asyncGet(UserList);
|
||||||
|
@ -49,7 +49,7 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return a promise when the binding is sync', function () {
|
it('should return a promise when the binding is sync', function () {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
SynchronousUserList
|
SynchronousUserList
|
||||||
]);
|
]);
|
||||||
var p = injector.asyncGet(SynchronousUserList);
|
var p = injector.asyncGet(SynchronousUserList);
|
||||||
|
@ -57,7 +57,7 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return a promise when the binding is sync (from cache)", function () {
|
it("should return a promise when the binding is sync (from cache)", function () {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
UserList
|
UserList
|
||||||
]);
|
]);
|
||||||
expect(injector.get(UserList)).toBeAnInstanceOf(UserList);
|
expect(injector.get(UserList)).toBeAnInstanceOf(UserList);
|
||||||
|
@ -65,7 +65,7 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return the injector', inject([AsyncTestCompleter], (async) => {
|
it('should return the injector', inject([AsyncTestCompleter], (async) => {
|
||||||
var injector = new Injector([]);
|
var injector = Injector.resolveAndCreate([]);
|
||||||
var p = injector.asyncGet(Injector);
|
var p = injector.asyncGet(Injector);
|
||||||
p.then(function (injector) {
|
p.then(function (injector) {
|
||||||
expect(injector).toBe(injector);
|
expect(injector).toBe(injector);
|
||||||
|
@ -75,7 +75,7 @@ export function main() {
|
||||||
|
|
||||||
it('should return a promise when instantiating a sync binding ' +
|
it('should return a promise when instantiating a sync binding ' +
|
||||||
'with an async dependency', inject([AsyncTestCompleter], (async) => {
|
'with an async dependency', inject([AsyncTestCompleter], (async) => {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
bind(UserList).toAsyncFactory(fetchUsers),
|
bind(UserList).toAsyncFactory(fetchUsers),
|
||||||
UserController
|
UserController
|
||||||
]);
|
]);
|
||||||
|
@ -88,7 +88,7 @@ export function main() {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
it("should create only one instance (async + async)", inject([AsyncTestCompleter], (async) => {
|
it("should create only one instance (async + async)", inject([AsyncTestCompleter], (async) => {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
bind(UserList).toAsyncFactory(fetchUsers)
|
bind(UserList).toAsyncFactory(fetchUsers)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -102,7 +102,7 @@ export function main() {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
it("should create only one instance (sync + async)", inject([AsyncTestCompleter], (async) => {
|
it("should create only one instance (sync + async)", inject([AsyncTestCompleter], (async) => {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
UserList
|
UserList
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -119,7 +119,7 @@ export function main() {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
it('should show the full path when error happens in a constructor', inject([AsyncTestCompleter], (async) => {
|
it('should show the full path when error happens in a constructor', inject([AsyncTestCompleter], (async) => {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
UserController,
|
UserController,
|
||||||
bind(UserList).toAsyncFactory(function () {
|
bind(UserList).toAsyncFactory(function () {
|
||||||
throw "Broken UserList";
|
throw "Broken UserList";
|
||||||
|
@ -136,7 +136,7 @@ export function main() {
|
||||||
|
|
||||||
describe("get", function () {
|
describe("get", function () {
|
||||||
it('should throw when instantiating an async binding', function () {
|
it('should throw when instantiating an async binding', function () {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
bind(UserList).toAsyncFactory(fetchUsers)
|
bind(UserList).toAsyncFactory(fetchUsers)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -145,7 +145,7 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw when instantiating a sync binding with an async dependency', function () {
|
it('should throw when instantiating a sync binding with an async dependency', function () {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
bind(UserList).toAsyncFactory(fetchUsers),
|
bind(UserList).toAsyncFactory(fetchUsers),
|
||||||
UserController
|
UserController
|
||||||
]);
|
]);
|
||||||
|
@ -156,7 +156,7 @@ export function main() {
|
||||||
|
|
||||||
it('should not throw when instantiating a sync binding with a resolved async dependency',
|
it('should not throw when instantiating a sync binding with a resolved async dependency',
|
||||||
inject([AsyncTestCompleter], (async) => {
|
inject([AsyncTestCompleter], (async) => {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
bind(UserList).toAsyncFactory(fetchUsers),
|
bind(UserList).toAsyncFactory(fetchUsers),
|
||||||
UserController
|
UserController
|
||||||
]);
|
]);
|
||||||
|
@ -168,7 +168,7 @@ export function main() {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
it('should resolve synchronously when an async dependency requested as a promise', function () {
|
it('should resolve synchronously when an async dependency requested as a promise', function () {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
bind(UserList).toAsyncFactory(fetchUsers),
|
bind(UserList).toAsyncFactory(fetchUsers),
|
||||||
AsyncUserController
|
AsyncUserController
|
||||||
]);
|
]);
|
||||||
|
@ -179,7 +179,7 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should wrap sync dependencies into promises if required', function () {
|
it('should wrap sync dependencies into promises if required', function () {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
bind(UserList).toFactory(() => new UserList()),
|
bind(UserList).toFactory(() => new UserList()),
|
||||||
AsyncUserController
|
AsyncUserController
|
||||||
]);
|
]);
|
||||||
|
|
|
@ -75,14 +75,14 @@ class NoAnnotations {
|
||||||
export function main() {
|
export function main() {
|
||||||
describe('injector', function () {
|
describe('injector', function () {
|
||||||
it('should instantiate a class without dependencies', function () {
|
it('should instantiate a class without dependencies', function () {
|
||||||
var injector = new Injector([Engine]);
|
var injector = Injector.resolveAndCreate([Engine]);
|
||||||
var engine = injector.get(Engine);
|
var engine = injector.get(Engine);
|
||||||
|
|
||||||
expect(engine).toBeAnInstanceOf(Engine);
|
expect(engine).toBeAnInstanceOf(Engine);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should resolve dependencies based on type information', function () {
|
it('should resolve dependencies based on type information', function () {
|
||||||
var injector = new Injector([Engine, Car]);
|
var injector = Injector.resolveAndCreate([Engine, Car]);
|
||||||
var car = injector.get(Car);
|
var car = injector.get(Car);
|
||||||
|
|
||||||
expect(car).toBeAnInstanceOf(Car);
|
expect(car).toBeAnInstanceOf(Car);
|
||||||
|
@ -90,7 +90,7 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should resolve dependencies based on @Inject annotation', function () {
|
it('should resolve dependencies based on @Inject annotation', function () {
|
||||||
var injector = new Injector([TurboEngine, Engine, CarWithInject]);
|
var injector = Injector.resolveAndCreate([TurboEngine, Engine, CarWithInject]);
|
||||||
var car = injector.get(CarWithInject);
|
var car = injector.get(CarWithInject);
|
||||||
|
|
||||||
expect(car).toBeAnInstanceOf(CarWithInject);
|
expect(car).toBeAnInstanceOf(CarWithInject);
|
||||||
|
@ -98,12 +98,12 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw when no type and not @Inject', function () {
|
it('should throw when no type and not @Inject', function () {
|
||||||
expect(() => new Injector([NoAnnotations])).toThrowError(
|
expect(() => Injector.resolveAndCreate([NoAnnotations])).toThrowError(
|
||||||
'Cannot resolve all parameters for NoAnnotations');
|
'Cannot resolve all parameters for NoAnnotations');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should cache instances', function () {
|
it('should cache instances', function () {
|
||||||
var injector = new Injector([Engine]);
|
var injector = Injector.resolveAndCreate([Engine]);
|
||||||
|
|
||||||
var e1 = injector.get(Engine);
|
var e1 = injector.get(Engine);
|
||||||
var e2 = injector.get(Engine);
|
var e2 = injector.get(Engine);
|
||||||
|
@ -112,7 +112,7 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should bind to a value', function () {
|
it('should bind to a value', function () {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
bind(Engine).toValue("fake engine")
|
bind(Engine).toValue("fake engine")
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -125,7 +125,7 @@ export function main() {
|
||||||
return new SportsCar(e);
|
return new SportsCar(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
Engine,
|
Engine,
|
||||||
bind(Car).toFactory(sportsCarFactory)
|
bind(Car).toFactory(sportsCarFactory)
|
||||||
]);
|
]);
|
||||||
|
@ -136,7 +136,7 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should bind to an alias', function() {
|
it('should bind to an alias', function() {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
Engine,
|
Engine,
|
||||||
bind(SportsCar).toClass(SportsCar),
|
bind(SportsCar).toClass(SportsCar),
|
||||||
bind(Car).toAlias(SportsCar)
|
bind(Car).toAlias(SportsCar)
|
||||||
|
@ -149,14 +149,14 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw when the aliased binding does not exist', function () {
|
it('should throw when the aliased binding does not exist', function () {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
bind('car').toAlias(SportsCar)
|
bind('car').toAlias(SportsCar)
|
||||||
]);
|
]);
|
||||||
expect(() => injector.get('car')).toThrowError('No provider for SportsCar! (car -> SportsCar)');
|
expect(() => injector.get('car')).toThrowError('No provider for SportsCar! (car -> SportsCar)');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support overriding factory dependencies', function () {
|
it('should support overriding factory dependencies', function () {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
Engine,
|
Engine,
|
||||||
bind(Car).toFactory((e) => new SportsCar(e), [Engine])
|
bind(Car).toFactory((e) => new SportsCar(e), [Engine])
|
||||||
]);
|
]);
|
||||||
|
@ -167,7 +167,7 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support optional dependencies', function () {
|
it('should support optional dependencies', function () {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
CarWithOptionalEngine
|
CarWithOptionalEngine
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -176,7 +176,7 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should flatten passed-in bindings", function () {
|
it("should flatten passed-in bindings", function () {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
[[Engine, Car]]
|
[[Engine, Car]]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -186,7 +186,7 @@ export function main() {
|
||||||
|
|
||||||
it("should use the last binding "+
|
it("should use the last binding "+
|
||||||
"when there are mutliple bindings for same token", function () {
|
"when there are mutliple bindings for same token", function () {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
bind(Engine).toClass(Engine),
|
bind(Engine).toClass(Engine),
|
||||||
bind(Engine).toClass(TurboEngine)
|
bind(Engine).toClass(TurboEngine)
|
||||||
]);
|
]);
|
||||||
|
@ -195,7 +195,7 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use non-type tokens', function () {
|
it('should use non-type tokens', function () {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
bind('token').toValue('value')
|
bind('token').toValue('value')
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -203,30 +203,30 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw when given invalid bindings', function () {
|
it('should throw when given invalid bindings', function () {
|
||||||
expect(() => new Injector(["blah"])).toThrowError('Invalid binding blah');
|
expect(() => Injector.resolveAndCreate(["blah"])).toThrowError('Invalid binding blah');
|
||||||
expect(() => new Injector([bind("blah")])).toThrowError('Invalid binding blah');
|
expect(() => Injector.resolveAndCreate([bind("blah")])).toThrowError('Invalid binding blah');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should provide itself', function () {
|
it('should provide itself', function () {
|
||||||
var parent = new Injector([]);
|
var parent = Injector.resolveAndCreate([]);
|
||||||
var child = parent.createChild([]);
|
var child = parent.resolveAndCreateChild([]);
|
||||||
|
|
||||||
expect(child.get(Injector)).toBe(child);
|
expect(child.get(Injector)).toBe(child);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw when no provider defined', function () {
|
it('should throw when no provider defined', function () {
|
||||||
var injector = new Injector([]);
|
var injector = Injector.resolveAndCreate([]);
|
||||||
expect(() => injector.get('NonExisting')).toThrowError('No provider for NonExisting!');
|
expect(() => injector.get('NonExisting')).toThrowError('No provider for NonExisting!');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should show the full path when no provider', function () {
|
it('should show the full path when no provider', function () {
|
||||||
var injector = new Injector([CarWithDashboard, Engine, Dashboard]);
|
var injector = Injector.resolveAndCreate([CarWithDashboard, Engine, Dashboard]);
|
||||||
expect(() => injector.get(CarWithDashboard)).
|
expect(() => injector.get(CarWithDashboard)).
|
||||||
toThrowError('No provider for DashboardSoftware! (CarWithDashboard -> Dashboard -> DashboardSoftware)');
|
toThrowError('No provider for DashboardSoftware! (CarWithDashboard -> Dashboard -> DashboardSoftware)');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw when trying to instantiate a cyclic dependency', function () {
|
it('should throw when trying to instantiate a cyclic dependency', function () {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
Car,
|
Car,
|
||||||
bind(Engine).toClass(CyclicEngine)
|
bind(Engine).toClass(CyclicEngine)
|
||||||
]);
|
]);
|
||||||
|
@ -239,7 +239,7 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should show the full path when error happens in a constructor', function () {
|
it('should show the full path when error happens in a constructor', function () {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
Car,
|
Car,
|
||||||
bind(Engine).toClass(BrokenEngine)
|
bind(Engine).toClass(BrokenEngine)
|
||||||
]);
|
]);
|
||||||
|
@ -255,7 +255,7 @@ export function main() {
|
||||||
it('should instantiate an object after a failed attempt', function () {
|
it('should instantiate an object after a failed attempt', function () {
|
||||||
var isBroken = true;
|
var isBroken = true;
|
||||||
|
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
Car,
|
Car,
|
||||||
bind(Engine).toFactory(() => isBroken ? new BrokenEngine() : new Engine())
|
bind(Engine).toFactory(() => isBroken ? new BrokenEngine() : new Engine())
|
||||||
]);
|
]);
|
||||||
|
@ -268,13 +268,13 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support null values', () => {
|
it('should support null values', () => {
|
||||||
var injector = new Injector([bind('null').toValue(null)]);
|
var injector = Injector.resolveAndCreate([bind('null').toValue(null)]);
|
||||||
expect(injector.get('null')).toBe(null);
|
expect(injector.get('null')).toBe(null);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("default bindings", function () {
|
describe("default bindings", function () {
|
||||||
it("should be used when no matching binding found", function () {
|
it("should be used when no matching binding found", function () {
|
||||||
var injector = new Injector([], {defaultBindings: true});
|
var injector = Injector.resolveAndCreate([], {defaultBindings: true});
|
||||||
|
|
||||||
var car = injector.get(Car);
|
var car = injector.get(Car);
|
||||||
|
|
||||||
|
@ -282,7 +282,7 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should use the matching binding when it is available", function () {
|
it("should use the matching binding when it is available", function () {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
bind(Car).toClass(SportsCar)
|
bind(Car).toClass(SportsCar)
|
||||||
], {defaultBindings: true});
|
], {defaultBindings: true});
|
||||||
|
|
||||||
|
@ -294,8 +294,8 @@ export function main() {
|
||||||
|
|
||||||
describe("child", function () {
|
describe("child", function () {
|
||||||
it('should load instances from parent injector', function () {
|
it('should load instances from parent injector', function () {
|
||||||
var parent = new Injector([Engine]);
|
var parent = Injector.resolveAndCreate([Engine]);
|
||||||
var child = parent.createChild([]);
|
var child = parent.resolveAndCreateChild([]);
|
||||||
|
|
||||||
var engineFromParent = parent.get(Engine);
|
var engineFromParent = parent.get(Engine);
|
||||||
var engineFromChild = child.get(Engine);
|
var engineFromChild = child.get(Engine);
|
||||||
|
@ -304,10 +304,10 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should not use the child bindings when resolving the dependencies of a parent binding", function () {
|
it("should not use the child bindings when resolving the dependencies of a parent binding", function () {
|
||||||
var parent = new Injector([
|
var parent = Injector.resolveAndCreate([
|
||||||
Car, Engine
|
Car, Engine
|
||||||
]);
|
]);
|
||||||
var child = parent.createChild([
|
var child = parent.resolveAndCreateChild([
|
||||||
bind(Engine).toClass(TurboEngine)
|
bind(Engine).toClass(TurboEngine)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -316,8 +316,8 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create new instance in a child injector', function () {
|
it('should create new instance in a child injector', function () {
|
||||||
var parent = new Injector([Engine]);
|
var parent = Injector.resolveAndCreate([Engine]);
|
||||||
var child = parent.createChild([
|
var child = parent.resolveAndCreateChild([
|
||||||
bind(Engine).toClass(TurboEngine)
|
bind(Engine).toClass(TurboEngine)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -329,8 +329,8 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should create child injectors without default bindings", function () {
|
it("should create child injectors without default bindings", function () {
|
||||||
var parent = new Injector([], {defaultBindings: true});
|
var parent = Injector.resolveAndCreate([], {defaultBindings: true});
|
||||||
var child = parent.createChild([]);
|
var child = parent.resolveAndCreateChild([]);
|
||||||
|
|
||||||
//child delegates to parent the creation of Car
|
//child delegates to parent the creation of Car
|
||||||
var childCar = child.get(Car);
|
var childCar = child.get(Car);
|
||||||
|
@ -342,7 +342,7 @@ export function main() {
|
||||||
|
|
||||||
describe("lazy", function () {
|
describe("lazy", function () {
|
||||||
it("should create dependencies lazily", function () {
|
it("should create dependencies lazily", function () {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
Engine,
|
Engine,
|
||||||
CarWithLazyEngine
|
CarWithLazyEngine
|
||||||
]);
|
]);
|
||||||
|
@ -352,7 +352,7 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should cache instance created lazily", function () {
|
it("should cache instance created lazily", function () {
|
||||||
var injector = new Injector([
|
var injector = Injector.resolveAndCreate([
|
||||||
Engine,
|
Engine,
|
||||||
CarWithLazyEngine
|
CarWithLazyEngine
|
||||||
]);
|
]);
|
||||||
|
|
|
@ -16,16 +16,16 @@ export function main() {
|
||||||
|
|
||||||
setupReflector();
|
setupReflector();
|
||||||
var bindings = [A, B, C, D, E];
|
var bindings = [A, B, C, D, E];
|
||||||
var injector = new Injector(bindings);
|
var injector = Injector.resolveAndCreate(bindings);
|
||||||
|
|
||||||
var D_KEY = Key.get(D);
|
var D_KEY = Key.get(D);
|
||||||
var E_KEY = Key.get(E);
|
var E_KEY = Key.get(E);
|
||||||
var childInjector = injector.
|
var childInjector = injector.
|
||||||
createChild([]).
|
resolveAndCreateChild([]).
|
||||||
createChild([]).
|
resolveAndCreateChild([]).
|
||||||
createChild([]).
|
resolveAndCreateChild([]).
|
||||||
createChild([]).
|
resolveAndCreateChild([]).
|
||||||
createChild([]);
|
resolveAndCreateChild([]);
|
||||||
|
|
||||||
var variousBindings = [
|
var variousBindings = [
|
||||||
A,
|
A,
|
||||||
|
@ -56,7 +56,7 @@ export function main() {
|
||||||
|
|
||||||
function instantiate() {
|
function instantiate() {
|
||||||
for (var i = 0; i < iterations; ++i) {
|
for (var i = 0; i < iterations; ++i) {
|
||||||
var child = injector.createChild([E]);
|
var child = injector.resolveAndCreateChild([E]);
|
||||||
child.get(E);
|
child.get(E);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ export function main() {
|
||||||
var iterations = getIntParameter('iterations');
|
var iterations = getIntParameter('iterations');
|
||||||
|
|
||||||
reflector.reflectionCapabilities = new ReflectionCapabilities();
|
reflector.reflectionCapabilities = new ReflectionCapabilities();
|
||||||
var appInjector = new Injector([]);
|
var appInjector = Injector.resolveAndCreate([]);
|
||||||
|
|
||||||
var bindings = [A, B, C];
|
var bindings = [A, B, C];
|
||||||
var proto = new ProtoElementInjector(null, 0, bindings);
|
var proto = new ProtoElementInjector(null, 0, bindings);
|
||||||
|
|
|
@ -50,7 +50,7 @@ export class Runner {
|
||||||
if (isPresent(bindings)) {
|
if (isPresent(bindings)) {
|
||||||
ListWrapper.push(sampleBindings, bindings);
|
ListWrapper.push(sampleBindings, bindings);
|
||||||
}
|
}
|
||||||
return new Injector(sampleBindings).asyncGet(Sampler)
|
return Injector.resolveAndCreate(sampleBindings).asyncGet(Sampler)
|
||||||
.then( (sampler) => sampler.sample() );
|
.then( (sampler) => sampler.sample() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ import { Metric, MultiMetric, bind, Injector } from 'benchpress/common';
|
||||||
|
|
||||||
export function main() {
|
export function main() {
|
||||||
function createMetric(ids) {
|
function createMetric(ids) {
|
||||||
return new Injector([
|
return Injector.resolveAndCreate([
|
||||||
ListWrapper.map(ids, (id) => bind(id).toValue(new MockMetric(id)) ),
|
ListWrapper.map(ids, (id) => bind(id).toValue(new MockMetric(id)) ),
|
||||||
MultiMetric.createBindings(ids)
|
MultiMetric.createBindings(ids)
|
||||||
]).asyncGet(MultiMetric);
|
]).asyncGet(MultiMetric);
|
||||||
|
|
|
@ -45,7 +45,7 @@ export function main() {
|
||||||
}),
|
}),
|
||||||
bind(WebDriverExtension).toValue(new MockDriverExtension(perfLogs, commandLog, perfLogFeatures))
|
bind(WebDriverExtension).toValue(new MockDriverExtension(perfLogs, commandLog, perfLogFeatures))
|
||||||
];
|
];
|
||||||
return new Injector(bindings).get(PerflogMetric);
|
return Injector.resolveAndCreate(bindings).get(PerflogMetric);
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('perflog metric', () => {
|
describe('perflog metric', () => {
|
||||||
|
|
|
@ -29,7 +29,7 @@ export function main() {
|
||||||
if (isPresent(columnWidth)) {
|
if (isPresent(columnWidth)) {
|
||||||
ListWrapper.push(bindings, bind(ConsoleReporter.COLUMN_WIDTH).toValue(columnWidth));
|
ListWrapper.push(bindings, bind(ConsoleReporter.COLUMN_WIDTH).toValue(columnWidth));
|
||||||
}
|
}
|
||||||
reporter = new Injector(bindings).get(ConsoleReporter);
|
reporter = Injector.resolveAndCreate(bindings).get(ConsoleReporter);
|
||||||
}
|
}
|
||||||
|
|
||||||
it('should print the sample id, description and table header', () => {
|
it('should print the sample id, description and table header', () => {
|
||||||
|
|
|
@ -42,7 +42,7 @@ export function main() {
|
||||||
return PromiseWrapper.resolve(null);
|
return PromiseWrapper.resolve(null);
|
||||||
})
|
})
|
||||||
];
|
];
|
||||||
return new Injector(bindings).get(JsonFileReporter);
|
return Injector.resolveAndCreate(bindings).get(JsonFileReporter);
|
||||||
}
|
}
|
||||||
|
|
||||||
it('should write all data into a file', inject([AsyncTestCompleter], (async) => {
|
it('should write all data into a file', inject([AsyncTestCompleter], (async) => {
|
||||||
|
|
|
@ -19,7 +19,7 @@ import { Reporter, MultiReporter, bind, Injector, MeasureValues } from 'benchpre
|
||||||
|
|
||||||
export function main() {
|
export function main() {
|
||||||
function createReporters(ids) {
|
function createReporters(ids) {
|
||||||
return new Injector([
|
return Injector.resolveAndCreate([
|
||||||
ListWrapper.map(ids, (id) => bind(id).toValue(new MockReporter(id)) ),
|
ListWrapper.map(ids, (id) => bind(id).toValue(new MockReporter(id)) ),
|
||||||
MultiReporter.createBindings(ids)
|
MultiReporter.createBindings(ids)
|
||||||
]).asyncGet(MultiReporter);
|
]).asyncGet(MultiReporter);
|
||||||
|
|
|
@ -68,7 +68,7 @@ export function main() {
|
||||||
ListWrapper.push(bindings, bind(Options.FORCE_GC).toValue(forceGc));
|
ListWrapper.push(bindings, bind(Options.FORCE_GC).toValue(forceGc));
|
||||||
}
|
}
|
||||||
|
|
||||||
sampler = new Injector(bindings).get(Sampler);
|
sampler = Injector.resolveAndCreate(bindings).get(Sampler);
|
||||||
}
|
}
|
||||||
|
|
||||||
it('should call the prepare and execute callbacks using WebDriverAdapter.waitFor', inject([AsyncTestCompleter], (async) => {
|
it('should call the prepare and execute callbacks using WebDriverAdapter.waitFor', inject([AsyncTestCompleter], (async) => {
|
||||||
|
|
|
@ -11,7 +11,7 @@ export function main() {
|
||||||
var validator;
|
var validator;
|
||||||
|
|
||||||
function createValidator({size, metric}) {
|
function createValidator({size, metric}) {
|
||||||
validator = new Injector([
|
validator = Injector.resolveAndCreate([
|
||||||
RegressionSlopeValidator.BINDINGS,
|
RegressionSlopeValidator.BINDINGS,
|
||||||
bind(RegressionSlopeValidator.METRIC).toValue(metric),
|
bind(RegressionSlopeValidator.METRIC).toValue(metric),
|
||||||
bind(RegressionSlopeValidator.SAMPLE_SIZE).toValue(size)
|
bind(RegressionSlopeValidator.SAMPLE_SIZE).toValue(size)
|
||||||
|
|
|
@ -11,7 +11,7 @@ export function main() {
|
||||||
var validator;
|
var validator;
|
||||||
|
|
||||||
function createValidator(size) {
|
function createValidator(size) {
|
||||||
validator = new Injector([
|
validator = Injector.resolveAndCreate([
|
||||||
SizeValidator.BINDINGS,
|
SizeValidator.BINDINGS,
|
||||||
bind(SizeValidator.SAMPLE_SIZE).toValue(size)
|
bind(SizeValidator.SAMPLE_SIZE).toValue(size)
|
||||||
]).get(SizeValidator);
|
]).get(SizeValidator);
|
||||||
|
|
|
@ -19,7 +19,7 @@ import { WebDriverExtension, bind, Injector, Options } from 'benchpress/common';
|
||||||
|
|
||||||
export function main() {
|
export function main() {
|
||||||
function createExtension(ids, caps) {
|
function createExtension(ids, caps) {
|
||||||
return new Injector([
|
return Injector.resolveAndCreate([
|
||||||
ListWrapper.map(ids, (id) => bind(id).toValue(new MockExtension(id)) ),
|
ListWrapper.map(ids, (id) => bind(id).toValue(new MockExtension(id)) ),
|
||||||
bind(Options.CAPABILITIES).toValue(caps),
|
bind(Options.CAPABILITIES).toValue(caps),
|
||||||
WebDriverExtension.bindTo(ids)
|
WebDriverExtension.bindTo(ids)
|
||||||
|
|
|
@ -38,7 +38,7 @@ export function main() {
|
||||||
perfRecords = [];
|
perfRecords = [];
|
||||||
}
|
}
|
||||||
log = [];
|
log = [];
|
||||||
extension = new Injector([
|
extension = Injector.resolveAndCreate([
|
||||||
ChromeDriverExtension.BINDINGS,
|
ChromeDriverExtension.BINDINGS,
|
||||||
bind(WebDriverAdapter).toValue(new MockDriverAdapter(log, perfRecords, messageMethod))
|
bind(WebDriverAdapter).toValue(new MockDriverAdapter(log, perfRecords, messageMethod))
|
||||||
]).get(ChromeDriverExtension);
|
]).get(ChromeDriverExtension);
|
||||||
|
|
|
@ -34,7 +34,7 @@ export function main() {
|
||||||
perfRecords = [];
|
perfRecords = [];
|
||||||
}
|
}
|
||||||
log = [];
|
log = [];
|
||||||
extension = new Injector([
|
extension = Injector.resolveAndCreate([
|
||||||
IOsDriverExtension.BINDINGS,
|
IOsDriverExtension.BINDINGS,
|
||||||
bind(WebDriverAdapter).toValue(new MockDriverAdapter(log, perfRecords))
|
bind(WebDriverAdapter).toValue(new MockDriverAdapter(log, perfRecords))
|
||||||
]).get(IOsDriverExtension);
|
]).get(IOsDriverExtension);
|
||||||
|
|
Loading…
Reference in New Issue