1
- import { isMemoized , memoize } from "./memoize" ;
2
1
import type { Memoized } from "./memoize" ;
2
+ import { isMemoized , memoize } from "./memoize" ;
3
3
import { PartialContainer } from "./PartialContainer" ;
4
4
import type { AddService , AddServices , InjectableClass , InjectableFunction , TokenType , ValidTokens } from "./types" ;
5
- import { ConcatInjectable } from "./Injectable" ;
5
+ import { ClassInjectable , ConcatInjectable , Injectable } from "./Injectable" ;
6
6
import { entries } from "./entries" ;
7
7
8
8
type MaybeMemoizedFactories < Services > = {
@@ -407,13 +407,6 @@ export class Container<Services = {}> {
407
407
return this . providesService ( fnOrContainer ) ;
408
408
}
409
409
410
- /**
411
- * Create a new Container which provides a Service created by the given [InjectableClass].
412
- *
413
- * @param token - A unique Token which will correspond to the created Service.
414
- * @param cls - A class with a constructor that takes dependencies as arguments, which returns the Service.
415
- */
416
-
417
410
/**
418
411
* Registers a service in the container using a class constructor, simplifying the service creation process.
419
412
*
@@ -425,22 +418,10 @@ export class Container<Services = {}> {
425
418
* specifying these dependencies.
426
419
* @returns A new Container instance containing the newly created service, allowing for method chaining.
427
420
*/
428
- providesClass < Token extends TokenType , Service , Tokens extends readonly ValidTokens < Services > [ ] > (
421
+ providesClass = < Token extends TokenType , Service , Tokens extends readonly ValidTokens < Services > [ ] > (
429
422
token : Token ,
430
423
cls : InjectableClass < Services , Service , Tokens >
431
- ) : Container < AddService < Services , Token , Service > > {
432
- const dependencies : readonly any [ ] = cls . dependencies ;
433
- // If the service depends on itself, e.g. in the multi-binding case, where we call append multiple times with
434
- // the same token, we always must resolve the dependency using the parent container to avoid infinite loop.
435
- const getFromParent = dependencies . indexOf ( token ) !== - 1 ? ( ) => this . get ( token as any ) : undefined ;
436
- const factory = memoize ( this , function ( this : Container < Services > ) {
437
- // Safety: getFromParent is defined if the token is in the dependencies list, so it is safe to call it.
438
- return new cls ( ...( dependencies . map ( ( t ) => ( t === token ? getFromParent ! ( ) : this . get ( t ) ) ) as any ) ) ;
439
- } ) ;
440
-
441
- const factories = { ...this . factories , [ token ] : factory } ;
442
- return new Container ( factories as unknown as MaybeMemoizedFactories < AddService < Services , Token , Service > > ) ;
443
- }
424
+ ) => this . providesService ( ClassInjectable ( token , cls ) ) ;
444
425
445
426
/**
446
427
* Registers a static value as a service in the container. This method is ideal for services that do not
@@ -452,14 +433,8 @@ export class Container<Services = {}> {
452
433
* @returns A new Container instance that includes the provided service, allowing for chaining additional
453
434
* `provides` calls.
454
435
*/
455
- providesValue < Token extends TokenType , Service > (
456
- token : Token ,
457
- value : Service
458
- ) : Container < AddService < Services , Token , Service > > {
459
- const factory = memoize ( this , ( ) => value ) ;
460
- const factories = { ...this . factories , [ token ] : factory } ;
461
- return new Container ( factories as unknown as MaybeMemoizedFactories < AddService < Services , Token , Service > > ) ;
462
- }
436
+ providesValue = < Token extends TokenType , Service > ( token : Token , value : Service ) =>
437
+ this . providesService ( Injectable ( token , [ ] , ( ) => value ) ) ;
463
438
464
439
/**
465
440
* Appends a value to the array associated with a specified token in the current Container, then returns
@@ -477,14 +452,10 @@ export class Container<Services = {}> {
477
452
* @param value - A value to append to the array.
478
453
* @returns The updated Container with the appended value in the specified array.
479
454
*/
480
- appendValue < Token extends keyof Services , Service extends ArrayElement < Services [ Token ] > > (
455
+ appendValue = < Token extends keyof Services , Service extends ArrayElement < Services [ Token ] > > (
481
456
token : Token ,
482
457
value : Service
483
- ) : Service extends any ? Container < Services > : never ;
484
-
485
- appendValue < Token extends TokenType , Service > ( token : Token , value : Service ) : Container < any > {
486
- return this . providesService ( ConcatInjectable ( token , ( ) => value ) ) ;
487
- }
458
+ ) => this . providesService ( ConcatInjectable ( token , ( ) => value ) ) as Container < Services > ;
488
459
489
460
/**
490
461
* Appends an injectable class factory to the array associated with a specified token in the current Container,
@@ -501,18 +472,17 @@ export class Container<Services = {}> {
501
472
* @param cls - A class with a constructor that takes dependencies as arguments, which returns the Service.
502
473
* @returns The updated Container with the new service instance appended to the specified array.
503
474
*/
504
- appendClass <
475
+ appendClass = <
505
476
Token extends keyof Services ,
506
477
Tokens extends readonly ValidTokens < Services > [ ] ,
507
478
Service extends ArrayElement < Services [ Token ] > ,
508
- > ( token : Token , cls : InjectableClass < Services , Service , Tokens > ) : Service extends any ? Container < Services > : never ;
509
-
510
- appendClass < Token extends TokenType , Tokens extends readonly ValidTokens < Services > [ ] , Service > (
479
+ > (
511
480
token : Token ,
512
481
cls : InjectableClass < Services , Service , Tokens >
513
- ) : Container < any > {
514
- return this . providesService ( ConcatInjectable ( token , ( ) => this . providesClass ( token , cls ) . get ( token ) ) ) ;
515
- }
482
+ ) =>
483
+ this . providesService (
484
+ ConcatInjectable ( token , ( ) => this . providesClass ( token , cls ) . get ( token ) )
485
+ ) as Container < Services > ;
516
486
517
487
/**
518
488
* Appends a new service instance to an existing array within the container using an `InjectableFunction`.
@@ -531,17 +501,16 @@ export class Container<Services = {}> {
531
501
* @returns The updated Container, now including the new service instance appended to the array
532
502
* specified by the token.
533
503
*/
534
- append <
504
+ append = <
535
505
Token extends keyof Services ,
536
506
Tokens extends readonly ValidTokens < Services > [ ] ,
537
507
Service extends ArrayElement < Services [ Token ] > ,
538
- > ( fn : InjectableFunction < Services , Tokens , Token , Service > ) : Service extends any ? Container < Services > : never ;
539
-
540
- append < Token extends TokenType , Tokens extends readonly ValidTokens < Services > [ ] , Service > (
508
+ > (
541
509
fn : InjectableFunction < Services , Tokens , Token , Service >
542
- ) : Container < any > {
543
- return this . providesService ( ConcatInjectable ( fn . token , ( ) => this . providesService ( fn ) . get ( fn . token ) ) ) ;
544
- }
510
+ ) =>
511
+ this . providesService (
512
+ ConcatInjectable ( fn . token , ( ) => this . providesService ( fn ) . get ( fn . token ) )
513
+ ) as Container < Services > ;
545
514
546
515
private providesService < Token extends TokenType , Tokens extends readonly ValidTokens < Services > [ ] , Service > (
547
516
fn : InjectableFunction < Services , Tokens , Token , Service >
@@ -559,6 +528,6 @@ export class Container<Services = {}> {
559
528
// MaybeMemoizedFactories object with the expected set of services – but when using the spread operation to
560
529
// merge two objects, the compiler widens the Token type to string. So we must re-narrow via casting.
561
530
const factories = { ...this . factories , [ token ] : factory } ;
562
- return new Container ( factories as unknown as MaybeMemoizedFactories < AddService < Services , Token , Service > > ) ;
531
+ return new Container ( factories ) as Container < AddService < Services , Token , Service > > ;
563
532
}
564
533
}
0 commit comments