Skip to content

Commit

Permalink
fix(resolver): use own property 'inject' in autoinject and parameter …
Browse files Browse the repository at this point in the history
…decorators
  • Loading branch information
doktordirk committed May 18, 2018
1 parent 6205549 commit 724ff08
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 40 deletions.
14 changes: 3 additions & 11 deletions src/injection.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,16 @@ import {_emptyParameters} from './container';
*/
export function autoinject(potentialTarget?: any): any {
let deco = function(target) {
let previousInject = target.inject ? target.inject.slice() : null; //make a copy of target.inject to avoid changing parent inject
let previousInject = target.hasOwnProperty('inject') ? target.inject : null;
let autoInject: any = metadata.getOwn(metadata.paramTypes, target) || _emptyParameters;
if (!previousInject) {
target.inject = autoInject;
} else {
for (let i = 0; i < autoInject.length; i++) {
//check if previously injected.
if (previousInject[i] && previousInject[i] !== autoInject[i]) {
const prevIndex = previousInject.indexOf(autoInject[i]);
if (prevIndex > -1) {
previousInject.splice(prevIndex, 1);
}
previousInject.splice((prevIndex > -1 && prevIndex < i) ? i - 1 : i, 0, autoInject[i]);
} else if (!previousInject[i]) {//else add
for (let i = 0; i++; i < autoInject.length) {
if (!previousInject[i]) {
previousInject[i] = autoInject[i];
}
}
target.inject = previousInject;
}
};

Expand Down
5 changes: 4 additions & 1 deletion src/resolvers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {protocol} from 'aurelia-metadata';
import {protocol, metadata} from 'aurelia-metadata';
import {Container} from './container';

/**
Expand Down Expand Up @@ -315,6 +315,9 @@ export class NewInstance {
}

export function getDecoratorDependencies(target, name) {
if (!target.hasOwnProperty('inject')) {
Object.defineProperty(target, 'inject', {value: undefined, writable: true});
}
let dependencies = target.inject;
if (typeof dependencies === 'function') {
throw new Error('Decorator ' + name + ' cannot be used with "inject()". Please use an array instead.');
Expand Down
Loading

0 comments on commit 724ff08

Please sign in to comment.