Skip to content

Commit

Permalink
Merge pull request #49 from thefill/feature/UpdateMainReadmeFile
Browse files Browse the repository at this point in the history
Fix docs, fix injection issue
  • Loading branch information
thefill committed May 21, 2019
2 parents fed25f3 + 55eb994 commit 4b4b884
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 11 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class Attack {
}
}

jetli.set('attack', Attack, false);
jetli.set('attack', Attack, true);
console.log('No initialisation at this point');

const fighter1 = jetli.get('attack');
Expand Down Expand Up @@ -254,7 +254,6 @@ class Attack {

class AttackMock {
constructor(){
super();
console.log(`Attack mocked!`);
}
punch(){
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jetli",
"version": "1.0.0",
"version": "1.0.1",
"description": "Dependency injector",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
46 changes: 46 additions & 0 deletions src/classes/jetli/jetli.class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,52 @@ describe('Jetli class', () => {
});
});

it('should prevent circular dependency when classes inject via init method', () => {
// tslint:disable-next-line
class ServiceA implements IInjection {
protected service: ServiceB;
protected id: number;

public init(jetliInstance) {
this.service = jetliInstance.get(ServiceB);
this.id = this.service.getNumber();
}

public getNumber() {
return 123;
}

public getId() {
return this.id;
}
}

// tslint:disable-next-line
class ServiceB implements IInjection {
protected service: ServiceA;
protected id: number;

public init(jetliInstance) {
this.service = jetliInstance.get(ServiceA);
this.id = this.service.getNumber();
}

public getNumber() {
return 321;
}

public getId() {
return this.id;
}
}

const serviceA = jetli.get(ServiceA);
const serviceB = jetli.get(ServiceB);

expect(serviceA.getId()).toEqual(serviceB.getNumber());
expect(serviceB.getId()).toEqual(serviceA.getNumber());
});

describe('should allow to retrieve dependency with string key via get method', () => {
Object.keys(primitiveTypes).forEach((typeName) => {
it(`for ${typeName}`, () => {
Expand Down
12 changes: 6 additions & 6 deletions src/classes/jetli/jetli.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,17 @@ export class Jetli implements IJetli {
dependency = Dependency;
}

// if has init property and init is an callable method
if (dependency.init && typeof dependency.init === 'function') {
// initialise injectable
dependency.init(this);
}

this.initialisedDependencies[key] = {
dependency: dependency,
args: args
};
delete this.dependencies[key];

// if has init property and init is an callable method
if (dependency.init && typeof dependency.init === 'function') {
// initialise injectable
dependency.init(this);
}

return dependency;
}
Expand Down
3 changes: 1 addition & 2 deletions src/docs-partials/readme.body.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class Attack {
}
}

jetli.set('attack', Attack, false);
jetli.set('attack', Attack, true);
console.log('No initialisation at this point');

const fighter1 = jetli.get('attack');
Expand Down Expand Up @@ -236,7 +236,6 @@ class Attack {

class AttackMock {
constructor(){
super();
console.log(`Attack mocked!`);
}
punch(){
Expand Down

0 comments on commit 4b4b884

Please sign in to comment.