Skip to content

Commit

Permalink
Merge pull request #44 from hyj1991/master
Browse files Browse the repository at this point in the history
feat: support lazy injectable
  • Loading branch information
hyj1991 authored Aug 1, 2022
2 parents ca711b4 + 93f04a7 commit 0497742
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
20 changes: 16 additions & 4 deletions src/decorator/injectable.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { InjectableOption, ScopeEnum } from '../types';
import { setMetadata } from '../util';
import { CLASS_CONSTRUCTOR } from '../constant';
import { InjectableOption, ReflectMetadataType, ScopeEnum } from '../types';
import { recursiveGetMetadata, setMetadata } from '../util';
import { CLASS_CONSTRUCTOR, CLASS_PROPERTY, INJECT_HANDLER_PROPS, LAZY_HANDLER } from '../constant';

export function Injectable(options?: InjectableOption): ClassDecorator {
return (target: any) => {
setMetadata(CLASS_CONSTRUCTOR, { id: target, scope: ScopeEnum.SINGLETON, ...options }, target);
const md = { id: target, scope: ScopeEnum.SINGLETON, lazy: false, ...options };
setMetadata(CLASS_CONSTRUCTOR, md, target);

// make all properties lazy
if (md.lazy) {
const props = recursiveGetMetadata(CLASS_PROPERTY, target) as ReflectMetadataType[];
const handlerProps = recursiveGetMetadata(
INJECT_HANDLER_PROPS,
target,
) as ReflectMetadataType[];
const properties = (props ?? []).concat(handlerProps ?? []);
properties.forEach(property => property.handler = LAZY_HANDLER);
}
};
}
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface InjectOptions {
export interface InjectableOption {
id?: Identifier;
scope?: ScopeEnum;
lazy?: boolean;
}

export interface InjectableDefinition<T = unknown> {
Expand Down
7 changes: 7 additions & 0 deletions test/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ExecutionClazzA from './fixtures/execution/a';
import { HandlerDemo, CONFIG_ALL } from './fixtures/handler_resolve/handler';
import ClassA from './fixtures/value/a';
import ClassB from './fixtures/value/b';
import LazyCClass from './fixtures/lazy/lazy_c';
import LazyBClass from './fixtures/lazy/lazy_b';
import LazyAClass from './fixtures/lazy/lazy_a';

Expand Down Expand Up @@ -285,5 +286,11 @@ describe('container#lazy', () => {
expect(instance.lazyB).toBeInstanceOf(LazyBClass);
expect(instance.lazyB === instance.lazyB).toBeTruthy();
expect(instance.lazyB.name).toBe('lazyBClass');
container.set({ type: LazyCClass });
const instanceb = container.get(LazyBClass);
expect(instanceb.lazyC).toBeDefined();
expect(instanceb.lazyC).toBeInstanceOf(LazyCClass);
expect(instanceb.lazyC === instanceb.lazyC).toBeTruthy();
expect(instanceb.lazyC.name).toBe('lazyCClass');
});
});
4 changes: 2 additions & 2 deletions test/fixtures/lazy/lazy_a.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import LazyB from './lazy_b';
import { Inject, Injectable } from '../../../src';

@Injectable()
@Injectable({ lazy: true })
export default class LazyAClass {
@Inject({ lazy: true })
@Inject()
lazyB!: LazyB;
}
6 changes: 6 additions & 0 deletions test/fixtures/lazy/lazy_b.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import LazyC from './lazy_c';
import { Inject, Injectable } from '../../../src';

@Injectable()
export default class LazyBClass {
@Inject({ lazy: true })
lazyC!: LazyC;
public name = 'lazyBClass';
}
3 changes: 3 additions & 0 deletions test/fixtures/lazy/lazy_c.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default class LazyCClass {
public name = 'lazyCClass';
}

0 comments on commit 0497742

Please sign in to comment.