Skip to content

Commit

Permalink
Add @PostInjection, 0.1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulBGD committed Nov 16, 2021
1 parent 0d2e0ee commit 50197fb
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@technove/inject",
"description": "Dependency injection for TypeScript",
"version": "0.1.7",
"version": "0.1.8",
"author": "PaulBGD",
"license": "MIT",
"scripts": {
Expand Down
50 changes: 50 additions & 0 deletions src/container.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Container } from "./container";
import { Key } from "./key";
import { Service } from "./decorators/service";
import { Inject } from "./decorators/inject";
import { PostInjection } from "./decorators/post-injection";

describe("Container", () => {
it("should be able to create a container", () => {
Expand Down Expand Up @@ -186,4 +187,53 @@ describe("Container", () => {
}
expect(() => container.get(B)).to.throw("found missing a @Inject");
});

it("calls after initialization", () => {
let ran = false;

class A {
@PostInjection()
private afterInit() {
ran = true;
}
}

const container = new Container();
container.get(A);
expect(ran).to.be.equal(true);

class Dependency {
public val = 5;
}

let retrievedValue = 0;
class B {
@Inject()
private dep!: Dependency;

@PostInjection()
private afterInit() {
retrievedValue = this.dep.val;
}
}

container.get(B);
expect(retrievedValue).to.be.equal(5);
});

it("handles after initialization with promise", async () => {
let val = 0;
class A {
@PostInjection()
private async init() {
await new Promise((resolve) => setTimeout(resolve, 5));
val = await Promise.resolve(5);
}
}

const container = new Container();
await container.load(A);

expect(val).to.be.equal(5);
});
});
20 changes: 18 additions & 2 deletions src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,27 @@ export class Container {
return val;
});

if (!anyPromises) {
const postInitialization = () => {
let anyPromises = false;

const mapped = injectData.postInjection.map((func) => {
const val = func.call(service);
anyPromises ||= val instanceof Promise;
return val;
});

if (anyPromises) {
return Promise.all(mapped).then(() => service);
}

return service;
};

if (!anyPromises) {
return postInitialization();
}

return Promise.all(mapped).then(() => service);
return Promise.all(mapped).then(() => postInitialization());
};

return create(mappedParams);
Expand Down
22 changes: 22 additions & 0 deletions src/decorators/post-injection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { getInjectedData, InjectedData } from "../injector";

export const PostInjection: () => MethodDecorator =
() => (target, propertyKey) => {
if (typeof propertyKey !== "string") {
throw new Error("Only accepts string keys");
}

const data: InjectedData = getInjectedData(target);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const func = (target as any)[propertyKey];

if (typeof func !== "function") {
throw new Error("Expected function");
}

if (func.length !== 0) {
throw new Error("Expected function to have 0 parameters");
}

data.postInjection.push(func);
};
2 changes: 2 additions & 0 deletions src/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ParameterProvider, Provider } from "./types";
export interface InjectedData {
properties: Provider[];
parameters: ParameterProvider<unknown>[];
postInjection: (() => Promise<void> | unknown)[];
}

const INJECT_KEY = "__inject__";
Expand All @@ -15,6 +16,7 @@ export function getInjectedData(Service: Object): InjectedData {
const data: InjectedData = {
properties: [],
parameters: new Array(Service.constructor.length),
postInjection: [],
};
Reflect.defineMetadata(INJECT_KEY, data, Service);
return data;
Expand Down

0 comments on commit 50197fb

Please sign in to comment.