Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Commit

Permalink
Add extra dependencies on single injections
Browse files Browse the repository at this point in the history
  • Loading branch information
pinicarus committed Jul 24, 2016
1 parent 8761709 commit 4988aa1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const Container = class Container {
const args = params(arguments, [
{type: String, default: null},
{type: Function},
{type: Policy,default: defaultPolicy},
{type: Policy, default: defaultPolicy},
], true);

let name = args[0];
Expand All @@ -85,17 +85,30 @@ const Container = class Container {
/**
* Inject a functor with registered values.
*
* @param {Function} functor - The functor to inject.
* @param {Function} functor - The functor to inject.
* @param {Object<String, *>} values - Extra dependencies use for injection.
*
* @returns {Function} The injected functor.
* @throws {TypeError} Whenever the functor does not inherit from Function.
*/
inject(functor) {
inject() {
const args = params(arguments, [
{type: Function},
{type: Object, default: null},
], true);

return new Injector().inject(this[_container], args[0]);
let container = this;
const functor = args[0];
const values = args[1];

if (values) {
container = container.createChild();
for (const key in values) {
container.registerValue(key, values[key]);
}
}

return new Injector().inject(container[_container], functor);
}
};

Expand Down
11 changes: 11 additions & 0 deletions test/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,15 @@ describe("Container", function () {
assert(!(instance instanceof constructor));
assert.deepEqual(instance, {b: 2});
});

it("should resolve with extra dependencies", function () {
const container = new Container();

container.registerValue("a", 1);

const functor = container.inject((a, b) => [a, b], {b: 2});

assert.deepEqual(functor(), [1, 2]);
assert.throws(() => container.inject((a, b) => [a, b]));
});
});

0 comments on commit 4988aa1

Please sign in to comment.