Skip to content

Commit

Permalink
Past container to PostInjection
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulBGD committed Nov 17, 2021
1 parent c7a622a commit cc5dece
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 8 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.11",
"version": "0.1.12",
"author": "PaulBGD",
"license": "MIT",
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions src/container.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,13 @@ describe("Container", () => {
private dep!: Dependency;

@PostInjection()
private afterInit() {
retrievedValue = this.dep.val;
private afterInit(container: Container) {
retrievedValue = container.get(Dependency).val + this.dep.val;
}
}

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

it("handles after initialization with promise", async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class Container {
let anyPromises = false;

const mapped = injectData.postInjection.map((func) => {
const val = func.call(service);
const val = func.call(service, this);
anyPromises ||= val instanceof Promise;
return val;
});
Expand Down
4 changes: 2 additions & 2 deletions src/decorators/post-injection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export const PostInjection: () => MethodDecorator =
throw new Error("Expected function");
}

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

data.postInjection.push(func);
Expand Down
3 changes: 2 additions & 1 deletion src/injector.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ParameterProvider, Provider } from "./types";
import { Container } from "./container";

export interface InjectedData {
properties: Provider[];
parameters: ParameterProvider<unknown>[];
postInjection: (() => Promise<void> | unknown)[];
postInjection: ((container: Container) => Promise<void> | unknown)[];
}

const INJECT_KEY = "__inject__";
Expand Down

0 comments on commit cc5dece

Please sign in to comment.