Skip to content

Commit

Permalink
fix: extending context
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed May 7, 2020
1 parent 8357173 commit 99b1d6b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/test/src/unit/Context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test("Context", async () => {
expect(await context.getMemoryDb()).toBeInstanceOf(MemoryDb);
expect(await context.getMemoryDb() === context.memoryDb).toBe(true);

expect(context.parentDb).toBeInstanceOf(MemoryDb);
expect(context.randomBytes).not.toStrictEqual(context.randomBytes);
expect(context.randomBytes).toBeInstanceOf(Buffer);

Expand Down
8 changes: 7 additions & 1 deletion packages/test/src/unit/src/context/BaseContext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import {Context} from "@typesafeunit/unit";
import {Context, resolve} from "@typesafeunit/unit";
import {MemoryDbServiceResolver} from "./services/MemoryDbServiceResolver";

export class BaseContext extends Context {
public version = "1.0.0";
public startAt = new Date();

@resolve
public get parentDb() {
return new MemoryDbServiceResolver("parent");
}
}
2 changes: 1 addition & 1 deletion packages/unit/src/Context/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Context {
const finish = this.logger.perf("Resolve context", {context: name});
try {
const descriptionMap: PropertyDescriptorMap = Object.getOwnPropertyDescriptors(context);
for (const key of Service.getRef(context)) {
for (const key of Service.getReferences(context, Context)) {
if (Reflect.has(context, key)) {
const service: Service<any> = Reflect.get(context, key);
const finishResolve = this.logger.perf(
Expand Down
18 changes: 16 additions & 2 deletions packages/unit/src/Service/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import {IServiceResolver} from "../interfaces";
import {ServiceRef} from "./ServiceRef";

export abstract class Service<T> implements IServiceResolver<T> {
public static getRef(target: object) {
return ServiceRef.get(target);
public static getReferences(target: object, base: Function | null = null) {
const references = [];
for (const proto of this.getPrototypes(target, base)) {
references.push(...ServiceRef.get(proto));
}

return references;
}

public static resolve(): PropertyDecorator {
Expand All @@ -12,5 +17,14 @@ export abstract class Service<T> implements IServiceResolver<T> {
};
}

protected static* getPrototypes(target: object, base: Function | null = null) {
let proto = Object.getPrototypeOf(target);
const baseProto = base === null ? null : base.prototype;
while (baseProto !== proto) {
yield proto;
proto = Object.getPrototypeOf(proto);
}
}

public abstract resolve(): Promise<T>;
}

0 comments on commit 99b1d6b

Please sign in to comment.