Skip to content

Commit

Permalink
Fix defaultValue
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulBGD committed Nov 16, 2021
1 parent 2625b2a commit 0d2e0ee
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 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.6",
"version": "0.1.7",
"author": "PaulBGD",
"license": "MIT",
"scripts": {
Expand Down
15 changes: 15 additions & 0 deletions src/decorators/inject.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Inject } from "./inject";
import { getInjectedData } from "../injector";
import { expect } from "chai";
import { it } from "mocha";
import { Container } from "../container";

describe("@Inject", () => {
it("adds injected data to the class", () => {
Expand Down Expand Up @@ -31,4 +32,18 @@ describe("@Inject", () => {
}
}).to.throw("Cannot inject self in constructor");
});

it("supports reading the default value", () => {
class A {
@Inject(
(c, field) =>
field.fieldType === "property" && field.defaultValue
)
val = 5;
}

const container = new Container();
const a = container.get(A);
expect(a.val).to.be.equal(5);
});
});
38 changes: 19 additions & 19 deletions src/decorators/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export interface InjectProps {
provider?: FieldProvider<any>;
}

const NO_VALUE = Symbol("no-value");

export const Inject: (
props?: InjectProps | FieldProvider<any>
) => PropertyDecorator & ParameterDecorator =
Expand Down Expand Up @@ -58,22 +60,35 @@ export const Inject: (
);

if (parameterIndex === undefined) {
const defaultValue = (target as any)[propertyName];

let val: any = undefined;
let val: any = NO_VALUE;
const provider: Provider = (
container: Container,
target: Object
) => {
const field: FieldProperty<unknown> = {
fieldType: "property",
name: propertyName as string,
defaultValue,
defaultValue: (target as any)[propertyName],
target,
type: propertyType,
getValue: () => val,
};

Object.defineProperty(target, propertyName, {
get() {
if (val === NO_VALUE) {
throw new Error(
`Property ${propertyName} has not been injected`
);
}
return val;
},

set(newVal: never) {
val = newVal;
},
});

const retrieved = retrievalProvider(container, field);
if (retrieved instanceof Promise) {
return retrieved.then((newVal) => (val = newVal));
Expand All @@ -82,21 +97,6 @@ export const Inject: (
}
};
data.properties.push(provider);

Object.defineProperty(target, propertyName, {
get() {
if (val === undefined) {
throw new Error(
`Property ${propertyName} has not been injected`
);
}
return val;
},

set(newVal: never) {
val = newVal;
},
});
} else {
if (propertyType === target) {
throw new Error("Cannot inject self in constructor");
Expand Down

0 comments on commit 0d2e0ee

Please sign in to comment.