refactor(di): unified di injector and core injector

BREAKING CHANGES:

* InjectAsync and InjectLazy have been removed
* toAsyncFactory has been removed
This commit is contained in:
vsavkin
2015-06-26 15:59:18 -07:00
parent b688dee4c8
commit 22d3943831
49 changed files with 1211 additions and 1669 deletions

View File

@ -8,10 +8,10 @@ export class MultiMetric extends Metric {
static createBindings(childTokens): List<Binding> {
return [
bind(_CHILDREN)
.toAsyncFactory((injector) => PromiseWrapper.all(
ListWrapper.map(childTokens, (token) => injector.asyncGet(token))),
[Injector]),
bind(MultiMetric).toFactory((children) => new MultiMetric(children), [_CHILDREN])
.toFactory(
(injector: Injector) => ListWrapper.map(childTokens, (token) => injector.get(token)),
[Injector]),
bind(MultiMetric).toFactory(children => new MultiMetric(children), [_CHILDREN])
];
}
@ -52,4 +52,4 @@ function mergeStringMaps(maps): Object {
return result;
}
var _CHILDREN = new OpaqueToken('MultiMetric.children');
var _CHILDREN = new OpaqueToken('MultiMetric.children');

View File

@ -9,10 +9,10 @@ export class MultiReporter extends Reporter {
static createBindings(childTokens: List<any>): List<Binding> {
return [
bind(_CHILDREN)
.toAsyncFactory((injector) => PromiseWrapper.all(
ListWrapper.map(childTokens, (token) => injector.asyncGet(token))),
[Injector]),
bind(MultiReporter).toFactory((children) => new MultiReporter(children), [_CHILDREN])
.toFactory(
(injector: Injector) => ListWrapper.map(childTokens, (token) => injector.get(token)),
[Injector]),
bind(MultiReporter).toFactory(children => new MultiReporter(children), [_CHILDREN])
];
}

View File

@ -1,7 +1,7 @@
import {Injector, bind, Binding} from 'angular2/di';
import {isPresent, isBlank} from 'angular2/src/facade/lang';
import {List, ListWrapper} from 'angular2/src/facade/collection';
import {Promise} from 'angular2/src/facade/async';
import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
import {Sampler, SampleState} from './sampler';
import {ConsoleReporter} from './reporter/console_reporter';
@ -50,9 +50,31 @@ export class Runner {
if (isPresent(bindings)) {
sampleBindings.push(bindings);
}
return Injector.resolveAndCreate(sampleBindings)
.asyncGet(Sampler)
.then((sampler) => sampler.sample());
var inj = Injector.resolveAndCreate(sampleBindings);
var adapter = inj.get(WebDriverAdapter);
return PromiseWrapper
.all([adapter.capabilities(), adapter.executeScript('return window.navigator.userAgent;')])
.then((args) => {
var capabilities = args[0];
var userAgent = args[1];
// This might still create instances twice. We are creating a new injector with all the
// bindings.
// Only WebDriverAdapter is reused.
// TODO vsavkin consider changing it when toAsyncFactory is added back or when child
// injectors are handled better.
var injector = Injector.resolveAndCreate([
sampleBindings,
bind(Options.CAPABILITIES).toValue(capabilities),
bind(Options.USER_AGENT).toValue(userAgent),
bind(WebDriverAdapter).toValue(adapter)
]);
var sampler = injector.get(Sampler);
return sampler.sample();
});
}
}
@ -74,10 +96,4 @@ var _DEFAULT_BINDINGS = [
Validator.bindTo(RegressionSlopeValidator),
WebDriverExtension.bindTo([ChromeDriverExtension, FirefoxDriverExtension, IOsDriverExtension]),
Metric.bindTo(MultiMetric),
bind(Options.CAPABILITIES)
.toAsyncFactory((adapter) => adapter.capabilities(), [WebDriverAdapter]),
bind(Options.USER_AGENT)
.toAsyncFactory((adapter) => adapter.executeScript('return window.navigator.userAgent;'),
[WebDriverAdapter])
];

View File

@ -14,11 +14,11 @@ import {Options} from './common_options';
@ABSTRACT()
export class WebDriverExtension {
static bindTo(childTokens): List<Binding> {
return [
var res = [
bind(_CHILDREN)
.toAsyncFactory((injector) => PromiseWrapper.all(
ListWrapper.map(childTokens, (token) => injector.asyncGet(token))),
[Injector]),
.toFactory(
(injector: Injector) => ListWrapper.map(childTokens, (token) => injector.get(token)),
[Injector]),
bind(WebDriverExtension)
.toFactory(
(children, capabilities) => {
@ -35,6 +35,7 @@ export class WebDriverExtension {
},
[_CHILDREN, Options.CAPABILITIES])
];
return res;
}
gc(): Promise<any> { throw new BaseException('NYI'); }

View File

@ -18,15 +18,15 @@ import {Metric, MultiMetric, bind, Injector} from 'benchpress/common';
export function main() {
function createMetric(ids) {
return Injector.resolveAndCreate([
ListWrapper.map(ids, (id) => bind(id).toValue(new MockMetric(id))),
MultiMetric.createBindings(ids)
])
.asyncGet(MultiMetric);
var m = Injector.resolveAndCreate([
ListWrapper.map(ids, (id) => bind(id).toValue(new MockMetric(id))),
MultiMetric.createBindings(ids)
])
.get(MultiMetric);
return PromiseWrapper.resolve(m);
}
describe('multi metric', () => {
it('should merge descriptions', inject([AsyncTestCompleter], (async) => {
createMetric(['m1', 'm2'])
.then((m) => {

View File

@ -19,11 +19,12 @@ import {Reporter, MultiReporter, bind, Injector, MeasureValues} from 'benchpress
export function main() {
function createReporters(ids) {
return Injector.resolveAndCreate([
ListWrapper.map(ids, (id) => bind(id).toValue(new MockReporter(id))),
MultiReporter.createBindings(ids)
])
.asyncGet(MultiReporter);
var r = Injector.resolveAndCreate([
ListWrapper.map(ids, (id) => bind(id).toValue(new MockReporter(id))),
MultiReporter.createBindings(ids)
])
.get(MultiReporter);
return PromiseWrapper.resolve(r);
}
describe('multi reporter', () => {

View File

@ -52,7 +52,7 @@ export function main() {
it('should set SampleDescription.id', inject([AsyncTestCompleter], (async) => {
createRunner()
.sample({id: 'someId'})
.then((_) => injector.asyncGet(SampleDescription))
.then((_) => injector.get(SampleDescription))
.then((desc) => {
expect(desc.id).toBe('someId');
async.done();
@ -62,9 +62,8 @@ export function main() {
it('should merge SampleDescription.description', inject([AsyncTestCompleter], (async) => {
createRunner([bind(Options.DEFAULT_DESCRIPTION).toValue({'a': 1})])
.sample({id: 'someId', bindings: [bind(Options.SAMPLE_DESCRIPTION).toValue({'b': 2})]})
.then((_) => injector.asyncGet(SampleDescription))
.then((_) => injector.get(SampleDescription))
.then((desc) => {
expect(desc.description)
.toEqual(
{'forceGc': false, 'userAgent': 'someUserAgent', 'a': 1, 'b': 2, 'v': 11});
@ -76,7 +75,7 @@ export function main() {
inject([AsyncTestCompleter], (async) => {
createRunner()
.sample({id: 'someId'})
.then((_) => injector.asyncGet(SampleDescription))
.then((_) => injector.get(SampleDescription))
.then((desc) => {
expect(desc.metrics).toEqual({'m1': 'some metric'});
@ -125,10 +124,10 @@ export function main() {
.toValue({'a': 2}),
]
})
.then((_) => injector.asyncGet(SampleDescription))
.then((_) => injector.get(SampleDescription))
.then((desc) => {
expect(injector.get(SampleDescription).description['a']).toBe(2);
expect(desc.description['a']).toBe(2);
async.done();
});
@ -139,6 +138,7 @@ export function main() {
class MockWebDriverAdapter extends WebDriverAdapter {
executeScript(script): Promise<string> { return PromiseWrapper.resolve('someUserAgent'); }
capabilities() { return null; }
}
class MockValidator extends Validator {

View File

@ -19,12 +19,14 @@ import {WebDriverExtension, bind, Injector, Options} from 'benchpress/common';
export function main() {
function createExtension(ids, caps) {
return Injector.resolveAndCreate([
ListWrapper.map(ids, (id) => bind(id).toValue(new MockExtension(id))),
bind(Options.CAPABILITIES).toValue(caps),
WebDriverExtension.bindTo(ids)
])
.asyncGet(WebDriverExtension);
return PromiseWrapper.wrap(() => {
return Injector.resolveAndCreate([
ListWrapper.map(ids, (id) => bind(id).toValue(new MockExtension(id))),
bind(Options.CAPABILITIES).toValue(caps),
WebDriverExtension.bindTo(ids)
])
.get(WebDriverExtension);
});
}
describe('WebDriverExtension.bindTo', () => {
@ -44,7 +46,6 @@ export function main() {
async.done();
});
}));
});
}