ts-ioc-container 25.0.5 → 25.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -24
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -38,6 +38,8 @@
|
|
|
38
38
|
- [Basic usage](#basic-usage-1)
|
|
39
39
|
- [Registration module (Provider + DependencyKey)](#registration-module-provider--dependencykey)
|
|
40
40
|
- [Hooks](#hooks)
|
|
41
|
+
- [OnConstruct](#onconstruct)
|
|
42
|
+
- [OnDispose](#ondispose)
|
|
41
43
|
- [Tests and Mocks](#tests-and-mocks)
|
|
42
44
|
- [Errors](#errors)
|
|
43
45
|
|
|
@@ -551,11 +553,10 @@ describe('Registration module', function () {
|
|
|
551
553
|
## Hooks
|
|
552
554
|
Sometimes you need to invoke methods after construct or dispose of class. This is what hooks are for.
|
|
553
555
|
|
|
556
|
+
### OnConstruct
|
|
554
557
|
```typescript
|
|
555
558
|
import 'reflect-metadata';
|
|
556
559
|
import {
|
|
557
|
-
singleton,
|
|
558
|
-
by,
|
|
559
560
|
constructor,
|
|
560
561
|
Container,
|
|
561
562
|
key,
|
|
@@ -563,11 +564,10 @@ import {
|
|
|
563
564
|
hook,
|
|
564
565
|
IContainer,
|
|
565
566
|
IInjector,
|
|
566
|
-
inject,
|
|
567
|
-
provider,
|
|
568
567
|
ReflectionInjector,
|
|
569
568
|
Registration,
|
|
570
569
|
} from 'ts-ioc-container';
|
|
570
|
+
import * as console from 'console';
|
|
571
571
|
|
|
572
572
|
class MyInjector implements IInjector {
|
|
573
573
|
private injector = new ReflectionInjector();
|
|
@@ -583,6 +583,48 @@ class MyInjector implements IInjector {
|
|
|
583
583
|
}
|
|
584
584
|
}
|
|
585
585
|
|
|
586
|
+
@key('logger')
|
|
587
|
+
class Logger {
|
|
588
|
+
isReady = false;
|
|
589
|
+
|
|
590
|
+
@hook('onConstruct')
|
|
591
|
+
initialize() {
|
|
592
|
+
this.isReady = true;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
log(message: string): void {
|
|
596
|
+
console.log(message);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
describe('onConstruct', function () {
|
|
601
|
+
it('should make logger be ready on resolve', function () {
|
|
602
|
+
const container = new Container(new MyInjector()).use(Registration.fromClass(Logger));
|
|
603
|
+
|
|
604
|
+
const logger = container.resolve<Logger>('logger');
|
|
605
|
+
|
|
606
|
+
expect(logger.isReady).toBe(true);
|
|
607
|
+
});
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
```
|
|
611
|
+
|
|
612
|
+
### OnDispose
|
|
613
|
+
```typescript
|
|
614
|
+
import 'reflect-metadata';
|
|
615
|
+
import {
|
|
616
|
+
singleton,
|
|
617
|
+
by,
|
|
618
|
+
Container,
|
|
619
|
+
key,
|
|
620
|
+
getHooks,
|
|
621
|
+
hook,
|
|
622
|
+
inject,
|
|
623
|
+
provider,
|
|
624
|
+
Registration,
|
|
625
|
+
ReflectionInjector,
|
|
626
|
+
} from 'ts-ioc-container';
|
|
627
|
+
|
|
586
628
|
@key('logsRepo')
|
|
587
629
|
@provider(singleton())
|
|
588
630
|
class LogsRepo {
|
|
@@ -595,16 +637,10 @@ class LogsRepo {
|
|
|
595
637
|
|
|
596
638
|
@key('logger')
|
|
597
639
|
class Logger {
|
|
598
|
-
isReady = false;
|
|
599
640
|
private messages: string[] = [];
|
|
600
641
|
|
|
601
642
|
constructor(@inject(by('logsRepo')) private logsRepo: LogsRepo) {}
|
|
602
643
|
|
|
603
|
-
@hook('onConstruct')
|
|
604
|
-
initialize() {
|
|
605
|
-
this.isReady = true;
|
|
606
|
-
}
|
|
607
|
-
|
|
608
644
|
log(message: string): void {
|
|
609
645
|
this.messages.push(message);
|
|
610
646
|
}
|
|
@@ -616,13 +652,11 @@ class Logger {
|
|
|
616
652
|
}
|
|
617
653
|
}
|
|
618
654
|
|
|
619
|
-
describe('
|
|
620
|
-
function createContainer() {
|
|
621
|
-
return new Container(new MyInjector());
|
|
622
|
-
}
|
|
623
|
-
|
|
655
|
+
describe('onDispose', function () {
|
|
624
656
|
it('should invoke hooks on all instances', async function () {
|
|
625
|
-
const container =
|
|
657
|
+
const container = new Container(new ReflectionInjector())
|
|
658
|
+
.use(Registration.fromClass(Logger))
|
|
659
|
+
.use(Registration.fromClass(LogsRepo));
|
|
626
660
|
|
|
627
661
|
const logger = container.resolve<Logger>('logger');
|
|
628
662
|
logger.log('Hello');
|
|
@@ -637,14 +671,6 @@ describe('Hooks', function () {
|
|
|
637
671
|
|
|
638
672
|
expect(container.resolve<LogsRepo>('logsRepo').savedLogs).toContain('Hello');
|
|
639
673
|
});
|
|
640
|
-
|
|
641
|
-
it('should make logger be ready on resolve', function () {
|
|
642
|
-
const container = createContainer().use(Registration.fromClass(Logger)).use(Registration.fromClass(LogsRepo));
|
|
643
|
-
|
|
644
|
-
const logger = container.resolve<Logger>('logger');
|
|
645
|
-
|
|
646
|
-
expect(logger.isReady).toBe(true);
|
|
647
|
-
});
|
|
648
674
|
});
|
|
649
675
|
|
|
650
676
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-ioc-container",
|
|
3
|
-
"version": "25.0.
|
|
3
|
+
"version": "25.0.6",
|
|
4
4
|
"description": "Typescript IoC container",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public",
|
|
@@ -56,5 +56,5 @@
|
|
|
56
56
|
"ts-node": "^10.9.1",
|
|
57
57
|
"typescript": "4.4.3"
|
|
58
58
|
},
|
|
59
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "ecf176122b8a6a635b0c47e90306bb3c4e2f8015"
|
|
60
60
|
}
|