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:
Yegor Jbanov
2015-04-10 17:05:31 -07:00
parent 6c8398df9b
commit 4a961f4ecb
24 changed files with 160 additions and 127 deletions

View File

@ -50,7 +50,7 @@ export class Runner {
if (isPresent(bindings)) {
ListWrapper.push(sampleBindings, bindings);
}
return new Injector(sampleBindings).asyncGet(Sampler)
return Injector.resolveAndCreate(sampleBindings).asyncGet(Sampler)
.then( (sampler) => sampler.sample() );
}
}

View File

@ -18,7 +18,7 @@ import { Metric, MultiMetric, bind, Injector } from 'benchpress/common';
export function main() {
function createMetric(ids) {
return new Injector([
return Injector.resolveAndCreate([
ListWrapper.map(ids, (id) => bind(id).toValue(new MockMetric(id)) ),
MultiMetric.createBindings(ids)
]).asyncGet(MultiMetric);

View File

@ -45,7 +45,7 @@ export function main() {
}),
bind(WebDriverExtension).toValue(new MockDriverExtension(perfLogs, commandLog, perfLogFeatures))
];
return new Injector(bindings).get(PerflogMetric);
return Injector.resolveAndCreate(bindings).get(PerflogMetric);
}
describe('perflog metric', () => {

View File

@ -29,7 +29,7 @@ export function main() {
if (isPresent(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', () => {

View File

@ -42,7 +42,7 @@ export function main() {
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) => {

View File

@ -19,7 +19,7 @@ import { Reporter, MultiReporter, bind, Injector, MeasureValues } from 'benchpre
export function main() {
function createReporters(ids) {
return new Injector([
return Injector.resolveAndCreate([
ListWrapper.map(ids, (id) => bind(id).toValue(new MockReporter(id)) ),
MultiReporter.createBindings(ids)
]).asyncGet(MultiReporter);

View File

@ -68,7 +68,7 @@ export function main() {
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) => {

View File

@ -11,7 +11,7 @@ export function main() {
var validator;
function createValidator({size, metric}) {
validator = new Injector([
validator = Injector.resolveAndCreate([
RegressionSlopeValidator.BINDINGS,
bind(RegressionSlopeValidator.METRIC).toValue(metric),
bind(RegressionSlopeValidator.SAMPLE_SIZE).toValue(size)

View File

@ -11,7 +11,7 @@ export function main() {
var validator;
function createValidator(size) {
validator = new Injector([
validator = Injector.resolveAndCreate([
SizeValidator.BINDINGS,
bind(SizeValidator.SAMPLE_SIZE).toValue(size)
]).get(SizeValidator);

View File

@ -19,7 +19,7 @@ import { WebDriverExtension, bind, Injector, Options } from 'benchpress/common';
export function main() {
function createExtension(ids, caps) {
return new Injector([
return Injector.resolveAndCreate([
ListWrapper.map(ids, (id) => bind(id).toValue(new MockExtension(id)) ),
bind(Options.CAPABILITIES).toValue(caps),
WebDriverExtension.bindTo(ids)

View File

@ -38,7 +38,7 @@ export function main() {
perfRecords = [];
}
log = [];
extension = new Injector([
extension = Injector.resolveAndCreate([
ChromeDriverExtension.BINDINGS,
bind(WebDriverAdapter).toValue(new MockDriverAdapter(log, perfRecords, messageMethod))
]).get(ChromeDriverExtension);

View File

@ -34,7 +34,7 @@ export function main() {
perfRecords = [];
}
log = [];
extension = new Injector([
extension = Injector.resolveAndCreate([
IOsDriverExtension.BINDINGS,
bind(WebDriverAdapter).toValue(new MockDriverAdapter(log, perfRecords))
]).get(IOsDriverExtension);