Skip to content

Commit c4b739b

Browse files
authored
feat(di): provided in root injectables (serhiisol#186)
1 parent f5706ff commit c4b739b

File tree

6 files changed

+37
-12
lines changed

6 files changed

+37
-12
lines changed

di/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

di/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@
4242
"test": "jest"
4343
},
4444
"types": "lib/index.d.ts",
45-
"version": "3.0.1"
45+
"version": "3.1.0"
4646
}

di/src/container.spec.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Container } from './container';
33
import { InjectionToken } from './injection-token';
44
import { Injectable, Inject } from './decorators';
55
import { InvalidDependencyError, MissingDependencyError, RecursiveDependencyError } from './errors';
6+
import { RootContainer } from './root-container';
67

78
describe('Container', () => {
89
let extraContainer: Container;
@@ -111,7 +112,7 @@ describe('Container', () => {
111112
describe('ClassProvider', () => {
112113
it('registers a provider', async () => {
113114
@Injectable()
114-
class TestInjectable {}
115+
class TestInjectable { }
115116

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

123124
it('replaces a provider', async () => {
124125
@Injectable()
125-
class TestInjectable {}
126+
class TestInjectable { }
126127

127128
@Injectable()
128-
class AnotherInjectable {}
129+
class AnotherInjectable { }
129130

130131
container.provide([
131132
{ provide: TestInjectable, useClass: TestInjectable },
@@ -251,7 +252,7 @@ describe('Container', () => {
251252

252253
it('registers a provider for existing class', async () => {
253254
@Injectable()
254-
class TestInjectable {}
255+
class TestInjectable { }
255256

256257
const token = new InjectionToken('token');
257258

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

295296
@Injectable()
296-
class TestInjectable {}
297+
class TestInjectable { }
297298

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

314315
@Injectable()
315-
class TestInjectable {}
316+
class TestInjectable { }
316317

317318
container.provide([
318319
{ provide: token, useValue: 1, multi: true },
@@ -345,3 +346,12 @@ describe('Container', () => {
345346
});
346347
});
347348
});
349+
350+
describe('RootContainer', () => {
351+
it('registers a provider in RootContainer', async () => {
352+
@Injectable({ providedIn: 'root' })
353+
class TestInjectable { }
354+
355+
expect(await RootContainer.has(TestInjectable)).toBeTruthy();
356+
});
357+
});

di/src/decorators/injectable.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { ClassConstructor } from '../types';
22
import { DEP_IDS_METADATA } from '../constants';
33

4-
export function Injectable() {
4+
import { RootContainer } from '../root-container';
5+
6+
interface InjectableOptions {
7+
providedIn?: 'root';
8+
}
9+
10+
export function Injectable(options?: InjectableOptions) {
511
return (target: ClassConstructor) => {
612
const params = Reflect.getMetadata('design:paramtypes', target) ?? [];
713
const ids = Reflect.getMetadata(DEP_IDS_METADATA, target) ?? [];
@@ -13,5 +19,12 @@ export function Injectable() {
1319
});
1420

1521
Reflect.defineMetadata(DEP_IDS_METADATA, verifiedIds, target);
22+
23+
if (options?.providedIn === 'root') {
24+
RootContainer.provide([{
25+
provide: target,
26+
useClass: target,
27+
}]);
28+
}
1629
};
1730
}

di/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'reflect-metadata';
2-
import { Container } from './container';
32

4-
export const RootContainer = new Container();
3+
export { RootContainer } from './root-container';
54
export { Container } from './container';
65
export { Injectable, Inject, Optional } from './decorators';
76
export { InjectionToken } from './injection-token';

di/src/root-container.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Container } from './container';
2+
3+
export const RootContainer = new Container();

0 commit comments

Comments
 (0)