ts-ioc-container 25.0.4 → 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.
Files changed (2) hide show
  1. package/README.md +51 -43
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -6,8 +6,6 @@
6
6
  [![Coverage Status](https://coveralls.io/repos/github/IgorBabkin/ts-ioc-container/badge.svg?branch=master)](https://coveralls.io/github/IgorBabkin/ts-ioc-container?branch=master)
7
7
  ![License](https://img.shields.io/npm/l/ts-ioc-container)
8
8
 
9
- * * *
10
-
11
9
  ## Advantages
12
10
  - battle tested :boom:
13
11
  - written on `typescript`
@@ -19,7 +17,7 @@
19
17
  - composable and open to extend
20
18
  - awesome for testing (auto mocking)
21
19
 
22
- * * *
20
+ ## Content
23
21
 
24
22
  - [Setup](#setup)
25
23
  - [Container](#container)
@@ -40,11 +38,11 @@
40
38
  - [Basic usage](#basic-usage-1)
41
39
  - [Registration module (Provider + DependencyKey)](#registration-module-provider--dependencykey)
42
40
  - [Hooks](#hooks)
41
+ - [OnConstruct](#onconstruct)
42
+ - [OnDispose](#ondispose)
43
43
  - [Tests and Mocks](#tests-and-mocks)
44
44
  - [Errors](#errors)
45
45
 
46
- * * *
47
-
48
46
  ## Setup
49
47
 
50
48
  ```shell script
@@ -69,8 +67,6 @@ And `tsconfig.json` should have next options:
69
67
  }
70
68
  ```
71
69
 
72
- * * *
73
-
74
70
  ## Container
75
71
  `IContainer` consists of 2 main parts:
76
72
 
@@ -194,8 +190,6 @@ describe('Disposing', function () {
194
190
 
195
191
  ```
196
192
 
197
- * * *
198
-
199
193
  ## Injectors
200
194
  `IInjector` is used to describe how dependencies should be injected to constructor.
201
195
 
@@ -330,8 +324,6 @@ describe('ProxyInjector', function () {
330
324
 
331
325
  ```
332
326
 
333
- * * *
334
-
335
327
  ## Providers
336
328
  `IProvider<T>` is used to describe how instances should be created. It has next basic methods:
337
329
  - `resolve` - creates instance with passed arguments
@@ -490,8 +482,6 @@ describe('ArgsProvider', function () {
490
482
 
491
483
  ```
492
484
 
493
- * * *
494
-
495
485
  ## Container modules
496
486
  Sometimes you want to encapsulate registration logic in separate module. This is what `IContainerModule` is for.
497
487
 
@@ -560,16 +550,13 @@ describe('Registration module', function () {
560
550
 
561
551
  ```
562
552
 
563
- * * *
564
-
565
553
  ## Hooks
566
554
  Sometimes you need to invoke methods after construct or dispose of class. This is what hooks are for.
567
555
 
556
+ ### OnConstruct
568
557
  ```typescript
569
558
  import 'reflect-metadata';
570
559
  import {
571
- singleton,
572
- by,
573
560
  constructor,
574
561
  Container,
575
562
  key,
@@ -577,11 +564,10 @@ import {
577
564
  hook,
578
565
  IContainer,
579
566
  IInjector,
580
- inject,
581
- provider,
582
567
  ReflectionInjector,
583
568
  Registration,
584
569
  } from 'ts-ioc-container';
570
+ import * as console from 'console';
585
571
 
586
572
  class MyInjector implements IInjector {
587
573
  private injector = new ReflectionInjector();
@@ -597,6 +583,48 @@ class MyInjector implements IInjector {
597
583
  }
598
584
  }
599
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
+
600
628
  @key('logsRepo')
601
629
  @provider(singleton())
602
630
  class LogsRepo {
@@ -609,16 +637,10 @@ class LogsRepo {
609
637
 
610
638
  @key('logger')
611
639
  class Logger {
612
- isReady = false;
613
640
  private messages: string[] = [];
614
641
 
615
642
  constructor(@inject(by('logsRepo')) private logsRepo: LogsRepo) {}
616
643
 
617
- @hook('onConstruct')
618
- initialize() {
619
- this.isReady = true;
620
- }
621
-
622
644
  log(message: string): void {
623
645
  this.messages.push(message);
624
646
  }
@@ -630,13 +652,11 @@ class Logger {
630
652
  }
631
653
  }
632
654
 
633
- describe('Hooks', function () {
634
- function createContainer() {
635
- return new Container(new MyInjector());
636
- }
637
-
655
+ describe('onDispose', function () {
638
656
  it('should invoke hooks on all instances', async function () {
639
- const container = createContainer().use(Registration.fromClass(Logger)).use(Registration.fromClass(LogsRepo));
657
+ const container = new Container(new ReflectionInjector())
658
+ .use(Registration.fromClass(Logger))
659
+ .use(Registration.fromClass(LogsRepo));
640
660
 
641
661
  const logger = container.resolve<Logger>('logger');
642
662
  logger.log('Hello');
@@ -651,20 +671,10 @@ describe('Hooks', function () {
651
671
 
652
672
  expect(container.resolve<LogsRepo>('logsRepo').savedLogs).toContain('Hello');
653
673
  });
654
-
655
- it('should make logger be ready on resolve', function () {
656
- const container = createContainer().use(Registration.fromClass(Logger)).use(Registration.fromClass(LogsRepo));
657
-
658
- const logger = container.resolve<Logger>('logger');
659
-
660
- expect(logger.isReady).toBe(true);
661
- });
662
674
  });
663
675
 
664
676
  ```
665
677
 
666
- * * *
667
-
668
678
  ## Tests and Mocks
669
679
  Sometimes you need to automatically mock all dependencies in container. This is what `AutoMockedContainer` is for.
670
680
 
@@ -707,8 +717,6 @@ describe('Mocking', () => {
707
717
 
708
718
  ```
709
719
 
710
- * * *
711
-
712
720
  ## Errors
713
721
 
714
722
  - [DependencyNotFoundError.ts](lib%2Fcontainer%2FDependencyNotFoundError.ts)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-ioc-container",
3
- "version": "25.0.4",
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": "02c953490e29c0a46b6609ae22ca6b1a688406e6"
59
+ "gitHead": "ecf176122b8a6a635b0c47e90306bb3c4e2f8015"
60
60
  }