Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
feat(di): provided in root injectables (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiisol authored Aug 9, 2023
1 parent f5706ff commit c4b739b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 12 deletions.
4 changes: 2 additions & 2 deletions di/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion di/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@
"test": "jest"
},
"types": "lib/index.d.ts",
"version": "3.0.1"
"version": "3.1.0"
}
22 changes: 16 additions & 6 deletions di/src/container.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Container } from './container';
import { InjectionToken } from './injection-token';
import { Injectable, Inject } from './decorators';
import { InvalidDependencyError, MissingDependencyError, RecursiveDependencyError } from './errors';
import { RootContainer } from './root-container';

describe('Container', () => {
let extraContainer: Container;
Expand Down Expand Up @@ -111,7 +112,7 @@ describe('Container', () => {
describe('ClassProvider', () => {
it('registers a provider', async () => {
@Injectable()
class TestInjectable {}
class TestInjectable { }

container.provide([
{ provide: TestInjectable, useClass: TestInjectable },
Expand All @@ -122,10 +123,10 @@ describe('Container', () => {

it('replaces a provider', async () => {
@Injectable()
class TestInjectable {}
class TestInjectable { }

@Injectable()
class AnotherInjectable {}
class AnotherInjectable { }

container.provide([
{ provide: TestInjectable, useClass: TestInjectable },
Expand Down Expand Up @@ -251,7 +252,7 @@ describe('Container', () => {

it('registers a provider for existing class', async () => {
@Injectable()
class TestInjectable {}
class TestInjectable { }

const token = new InjectionToken('token');

Expand Down Expand Up @@ -293,7 +294,7 @@ describe('Container', () => {
const token = new InjectionToken('token');

@Injectable()
class TestInjectable {}
class TestInjectable { }

container.provide([
{ provide: token, useClass: TestInjectable, multi: true },
Expand All @@ -312,7 +313,7 @@ describe('Container', () => {
const token = new InjectionToken('token');

@Injectable()
class TestInjectable {}
class TestInjectable { }

container.provide([
{ provide: token, useValue: 1, multi: true },
Expand Down Expand Up @@ -345,3 +346,12 @@ describe('Container', () => {
});
});
});

describe('RootContainer', () => {
it('registers a provider in RootContainer', async () => {
@Injectable({ providedIn: 'root' })
class TestInjectable { }

expect(await RootContainer.has(TestInjectable)).toBeTruthy();
});
});
15 changes: 14 additions & 1 deletion di/src/decorators/injectable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { ClassConstructor } from '../types';
import { DEP_IDS_METADATA } from '../constants';

export function Injectable() {
import { RootContainer } from '../root-container';

interface InjectableOptions {
providedIn?: 'root';
}

export function Injectable(options?: InjectableOptions) {
return (target: ClassConstructor) => {
const params = Reflect.getMetadata('design:paramtypes', target) ?? [];
const ids = Reflect.getMetadata(DEP_IDS_METADATA, target) ?? [];
Expand All @@ -13,5 +19,12 @@ export function Injectable() {
});

Reflect.defineMetadata(DEP_IDS_METADATA, verifiedIds, target);

if (options?.providedIn === 'root') {
RootContainer.provide([{
provide: target,
useClass: target,
}]);
}
};
}
3 changes: 1 addition & 2 deletions di/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'reflect-metadata';
import { Container } from './container';

export const RootContainer = new Container();
export { RootContainer } from './root-container';
export { Container } from './container';
export { Injectable, Inject, Optional } from './decorators';
export { InjectionToken } from './injection-token';
Expand Down
3 changes: 3 additions & 0 deletions di/src/root-container.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Container } from './container';

export const RootContainer = new Container();

0 comments on commit c4b739b

Please sign in to comment.