async-injection 1.6.0 → 2.0.0

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 (32) hide show
  1. package/Changelog.md +4 -0
  2. package/lib/cjs/index.js.map +1 -1
  3. package/lib/esm/index.js.map +1 -1
  4. package/lib/{cjs/index.d.ts → index.d.ts} +0 -1
  5. package/package.json +12 -9
  6. package/lib/esm/async-factory-provider.d.ts +0 -16
  7. package/lib/esm/bindable-provider.d.ts +0 -46
  8. package/lib/esm/binder.d.ts +0 -106
  9. package/lib/esm/class-provider.d.ts +0 -37
  10. package/lib/esm/constant-provider.d.ts +0 -10
  11. package/lib/esm/constants.d.ts +0 -8
  12. package/lib/esm/container.d.ts +0 -89
  13. package/lib/esm/decorators.d.ts +0 -56
  14. package/lib/esm/index.d.ts +0 -4
  15. package/lib/esm/injector.d.ts +0 -47
  16. package/lib/esm/provider.d.ts +0 -37
  17. package/lib/esm/state.d.ts +0 -15
  18. package/lib/esm/sync-factory-provider.d.ts +0 -21
  19. package/lib/esm/utils.d.ts +0 -14
  20. /package/lib/{cjs/async-factory-provider.d.ts → async-factory-provider.d.ts} +0 -0
  21. /package/lib/{cjs/bindable-provider.d.ts → bindable-provider.d.ts} +0 -0
  22. /package/lib/{cjs/binder.d.ts → binder.d.ts} +0 -0
  23. /package/lib/{cjs/class-provider.d.ts → class-provider.d.ts} +0 -0
  24. /package/lib/{cjs/constant-provider.d.ts → constant-provider.d.ts} +0 -0
  25. /package/lib/{cjs/constants.d.ts → constants.d.ts} +0 -0
  26. /package/lib/{cjs/container.d.ts → container.d.ts} +0 -0
  27. /package/lib/{cjs/decorators.d.ts → decorators.d.ts} +0 -0
  28. /package/lib/{cjs/injector.d.ts → injector.d.ts} +0 -0
  29. /package/lib/{cjs/provider.d.ts → provider.d.ts} +0 -0
  30. /package/lib/{cjs/state.d.ts → state.d.ts} +0 -0
  31. /package/lib/{cjs/sync-factory-provider.d.ts → sync-factory-provider.d.ts} +0 -0
  32. /package/lib/{cjs/utils.d.ts → utils.d.ts} +0 -0
package/Changelog.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 2.0.0 / 2024-08-06
2
+ * Move typedefs up from lib/{cjs|esm} to just lib.
3
+ * Remove binder.ts exports from index.ts
4
+
1
5
  ## 1.6.0 / 2024-05-23
2
6
  * Change internal meta-data constants from symbols to namespaced strings.
3
7
  This enables different compilation units to share a common Container.
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AACA,+CAAyC;AAAjC,yGAAA,SAAS,OAAA;AACjB,iDAAqF;AAA7E,uGAAA,MAAM,OAAA;AAAE,2GAAA,UAAU,OAAA;AAAE,yGAAA,QAAQ,OAAA;AAAE,8GAAA,aAAa,OAAA;AAAE,wGAAA,OAAO,OAAA;AAC5D,6CAAuF;AAA/C,6GAAA,cAAc,OAAA","sourcesContent":["export {Binder, SyncFactory, AsyncFactory, BindAs} from './binder.js';\nexport {Container} from './container.js';\nexport {Inject, Injectable, Optional, PostConstruct, Release} from './decorators.js';\nexport {ClassConstructor, InjectableId, InjectionToken, Injector} from './injector.js';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,+CAAyC;AAAjC,yGAAA,SAAS,OAAA;AACjB,iDAAqF;AAA7E,uGAAA,MAAM,OAAA;AAAE,2GAAA,UAAU,OAAA;AAAE,yGAAA,QAAQ,OAAA;AAAE,8GAAA,aAAa,OAAA;AAAE,wGAAA,OAAO,OAAA;AAC5D,6CAAuF;AAA/C,6GAAA,cAAc,OAAA","sourcesContent":["export {Container} from './container.js';\nexport {Inject, Injectable, Optional, PostConstruct, Release} from './decorators.js';\nexport {ClassConstructor, InjectableId, InjectionToken, Injector} from './injector.js';\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,SAAS,EAAC,MAAM,gBAAgB,CAAC;AACzC,OAAO,EAAC,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,aAAa,EAAE,OAAO,EAAC,MAAM,iBAAiB,CAAC;AACrF,OAAO,EAAiC,cAAc,EAAW,MAAM,eAAe,CAAC","sourcesContent":["export {Binder, SyncFactory, AsyncFactory, BindAs} from './binder.js';\nexport {Container} from './container.js';\nexport {Inject, Injectable, Optional, PostConstruct, Release} from './decorators.js';\nexport {ClassConstructor, InjectableId, InjectionToken, Injector} from './injector.js';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,gBAAgB,CAAC;AACzC,OAAO,EAAC,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,aAAa,EAAE,OAAO,EAAC,MAAM,iBAAiB,CAAC;AACrF,OAAO,EAAiC,cAAc,EAAW,MAAM,eAAe,CAAC","sourcesContent":["export {Container} from './container.js';\nexport {Inject, Injectable, Optional, PostConstruct, Release} from './decorators.js';\nexport {ClassConstructor, InjectableId, InjectionToken, Injector} from './injector.js';\n"]}
@@ -1,4 +1,3 @@
1
- export { Binder, SyncFactory, AsyncFactory, BindAs } from './binder.js';
2
1
  export { Container } from './container.js';
3
2
  export { Inject, Injectable, Optional, PostConstruct, Release } from './decorators.js';
4
3
  export { ClassConstructor, InjectableId, InjectionToken, Injector } from './injector.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "async-injection",
3
- "version": "1.6.0",
3
+ "version": "2.0.0",
4
4
  "description": "A robust lightweight dependency injection library for TypeScript.",
5
5
  "author": "Frank Stock",
6
6
  "license": "MIT",
@@ -22,10 +22,13 @@
22
22
  ".": {
23
23
  "import": "./lib/esm/index.js",
24
24
  "require": "./lib/cjs/index.js"
25
+ },
26
+ "./*": {
27
+ "types": "./lib/*.d.ts"
25
28
  }
26
29
  },
27
- "types": "lib/esm/index.d.ts",
28
- "typings": "lib/esm/index.d.ts",
30
+ "types": "lib/index.d.ts",
31
+ "typings": "lib/index.d.ts",
29
32
  "directories": {
30
33
  "lib": "lib"
31
34
  },
@@ -38,7 +41,7 @@
38
41
  "pretest": "npm run lint",
39
42
  "test": "TS_NODE_PROJECT=./tsconfig-test.json node --require ts-node/register --require tsconfig-paths/register node_modules/jasmine/bin/jasmine.js --config=jasmine.json",
40
43
  "coverage": "rimraf coverage && rimraf ./nyc_output && nyc -e .ts -x \"**/*.spec.ts\" -x \"tst/*\" --reporter=text-summary --reporter=lcov npm run test",
41
- "build": "npm run clean && tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json && npm run _postbuild",
44
+ "build": "npm run clean && tsc -p tsconfig.base.json && tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json && npm run _postbuild",
42
45
  "_postbuild": "echo '{\"type\": \"module\"}' >lib/esm/package.json && echo '{\"type\": \"commonjs\"}' >lib/cjs/package.json",
43
46
  "lint": "eslint 'src/**/*.ts'"
44
47
  },
@@ -54,19 +57,19 @@
54
57
  "@types/jasmine": "~5.1.4",
55
58
  "@typescript-eslint/eslint-plugin": "~5.62.0",
56
59
  "@typescript-eslint/parser": "~5.62.0",
57
- "eslint": "~8.53.0",
60
+ "eslint": "~8.57.0",
58
61
  "eslint-plugin-import": "~2.29.1",
59
62
  "eslint-plugin-jsdoc": "~39.9.1",
60
63
  "eslint-plugin-prefer-arrow": "~1.2.3",
61
- "jasmine": "~5.1.0",
64
+ "jasmine": "~5.2.0",
62
65
  "jasmine-console-reporter": "~3.1.0",
63
66
  "nyc": "~15.1.0",
64
- "reflect-metadata": "~0.1.14",
65
- "rimraf": "~5.0.7",
67
+ "reflect-metadata": "~0.2.2",
68
+ "rimraf": "~5.0.10",
66
69
  "source-map-support": "~0.5.21",
67
70
  "ts-node": "~10.9.2",
68
71
  "tsconfig-paths": "~4.2.0",
69
- "tslib": "~2.6.2",
72
+ "tslib": "~2.6.3",
70
73
  "typescript": "~4.9.5"
71
74
  },
72
75
  "nyc": {
@@ -1,16 +0,0 @@
1
- import { BindableProvider } from './bindable-provider.js';
2
- import { AsyncFactory } from './binder.js';
3
- import { InjectableId, Injector } from './injector.js';
4
- import { State } from './state.js';
5
- /**
6
- * @inheritDoc
7
- * This specialization invokes it's configured Factory asynchronously and waits until it can provide the result.
8
- */
9
- export declare class AsyncFactoryBasedProvider<T> extends BindableProvider<T, AsyncFactory<T>> {
10
- constructor(injector: Injector, id: InjectableId<T>, maker: AsyncFactory<T>);
11
- /**
12
- * @inheritDoc
13
- * This specialization invokes it's configured Factory and provides the result (or invokes the error handler if necessary).
14
- */
15
- provideAsState(): State<T>;
16
- }
@@ -1,46 +0,0 @@
1
- import { AsyncFactory, BindAs, OnErrorCallback, OnSuccessCallback, SyncFactory } from './binder.js';
2
- import { ClassConstructor, InjectableId, Injector } from './injector.js';
3
- import { Provider } from './provider.js';
4
- /**
5
- * @inheritDoc
6
- * This abstraction is for Providers that can be additionally configured as Singletons and/or configured with error and/or success handling callback(s).
7
- */
8
- export declare abstract class BindableProvider<T, M = ClassConstructor<T> | SyncFactory<T> | AsyncFactory<T>> extends Provider<T> {
9
- protected injector: Injector;
10
- protected id: InjectableId<T>;
11
- protected maker: M;
12
- protected constructor(injector: Injector, id: InjectableId<T>, maker: M);
13
- /**
14
- * A user supplied success handling function.
15
- * Default value is undefined.
16
- */
17
- protected successHandler?: OnSuccessCallback<T, any>;
18
- /**
19
- * A user supplied error handling function.
20
- * Default value is undefined.
21
- */
22
- protected errorHandler?: OnErrorCallback<T, any>;
23
- /**
24
- * Invoked by the Binder to create chain-able configuration
25
- *
26
- * @see BindAs
27
- */
28
- makeBindAs(): BindAs<T, M>;
29
- /**
30
- * Encapsulate the logic of invoking any configured error handler, and processing it's result.
31
- *
32
- * @see OnErrorCallback
33
- *
34
- * @returns The object substituted by the callback (otherwise this method throws the appropriate error).
35
- */
36
- protected queryErrorHandler(err: unknown, obj?: T): T;
37
- /**
38
- * This is like a retry mechanism that uses the Provider's errorHandler (if any) to attempt recovery whenever the supplied Promise rejects.
39
- * This method returns a Promise that rejects if recovery was not possible.
40
- * If the supplied Promise resolves, then this method passes the result to the callback, and then resolve as whatever that callback returns.
41
- *
42
- * @param waitFor The supplied Promise.
43
- * @param cb Callback to be invoked if the supplied Promise resolves.
44
- */
45
- protected makePromiseForObj<R>(waitFor: Promise<R>, cb: (result: R) => T): Promise<T>;
46
- }
@@ -1,106 +0,0 @@
1
- import { AbstractConstructor, ClassConstructor, InjectableId, Injector } from './injector.js';
2
- /**
3
- * Type definition for functions that return a value.
4
- * The function should return a valid value, but may throw an exception if it cannot.
5
- */
6
- export type SyncFactory<T> = (injector: Injector) => T;
7
- /**
8
- * Type definition for functions that return a Promise for a value.
9
- * The function *must* not throw and must return a valid Promise (e.g. pending, resolved, rejected).
10
- */
11
- export type AsyncFactory<T> = (injector: Injector) => Promise<T>;
12
- /**
13
- * You may bind an error handler which will be invoked, if the bound InjectableId could not be put into service.
14
- * An error handler *must* not throw, but may return an Error that will be propagated back up the call chain.
15
- *
16
- * @param binder The Binder that experienced the error.
17
- * @param id The identifier for what was trying to be made.
18
- * @param maker The thing that made (or tried to provideAsState). Will be one of type ClassConstructor, SyncFactory, or AsyncFactory, depending on how you registered the binding.
19
- * @param error Identifies the problem that occurred.
20
- * @param value If the 'maker' was able to create the thing, but it had an error during post construction, the made thing will be passed here.
21
- * @returns one of 3 results...
22
- * A substitute thing (kind of like a 'maker' do-over) which must be fully operational (e.g. any `@PostConstruct` will be ignored).
23
- * An alternate Error which will be propagated back up the call chain.
24
- * Undefined, which means the 'error' parameter will be propagated back up the call chain.
25
- */
26
- export type OnErrorCallback<T, M> = (injector: Injector, id: InjectableId<T>, maker: M, error: unknown, value?: T) => T | Error | void;
27
- /**
28
- * You may bind a success handler which will be invoked just before the bound InjectableId is put into service.
29
- * This is an alternative to the more preferred `@PostConstruct` decorator for scenarios when usage of that decorator is not feasible.
30
- * WARNING:
31
- * By registering a success handler, you override and nullify any `@PostConstruct` decorator on the class.
32
- * In such a scenario, the success handler should perform whatever care and feeding the class expected from the `@PostConstruct` decorator.
33
- * A success handler *must* not throw, but may return an Error that will be propagated back up the call chain.
34
- *
35
- * @param binder The Binder that performed the construction.
36
- * @param id The identifier for what was made.
37
- * @param maker The thing that made. Will be one of type ClassConstructor, SyncFactory, or AsyncFactory, depending on how you registered the binding.
38
- * @param value The thing that was made.
39
- * @returns one of 3 results...
40
- * An Error which will be propagated back up the call chain.
41
- * Undefined, which means the object is ready to be placed into service.
42
- * A Promise that resolves to one of the above two values (undefined or Error).
43
- */
44
- export type OnSuccessCallback<T, M> = (value: T, injector: Injector, id: InjectableId<T>, maker: M) => Promise<Error | void> | Error | void;
45
- /**
46
- * An interface allowing binding of an error handler.
47
- *
48
- * @see OnErrorCallback
49
- */
50
- export interface BindErrHandler<T, M> {
51
- onError(cb: OnErrorCallback<T, M>): void;
52
- }
53
- /**
54
- * An interface allowing binding of a post construction handler.
55
- *
56
- * @see OnSuccessCallback
57
- */
58
- export interface BindHandler<T, M> extends BindErrHandler<T, M> {
59
- onSuccess(cb: OnSuccessCallback<T, M>): BindErrHandler<T, M>;
60
- }
61
- /**
62
- * @inheritDoc
63
- * This specialization also allows you to specify that the binding is 'Singleton' (e.g. only one in the system).
64
- */
65
- export interface BindAs<T, M> extends BindHandler<T, M> {
66
- asSingleton(): BindHandler<T, M>;
67
- }
68
- /**
69
- * Bind Ids to producers.
70
- */
71
- export interface Binder extends Injector {
72
- /**
73
- * Bind an InjectableId to a constant value.
74
- * Constants are by their very nature singleton, and are assumed to be error proof.
75
- */
76
- bindConstant<T>(id: InjectableId<T>, value: T): T;
77
- /**
78
- * Bind an InjectableId to a class (actually it's constructor).
79
- * As a shortcut, you may use the class constructor as the 'id' (e.g. container.bindClass(A); ).
80
- * The container will also invoke any `@PostConstruct` present on the class.
81
- */
82
- bindClass<T>(id: ClassConstructor<T>, constructor?: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;
83
- bindClass<T>(id: string | symbol | AbstractConstructor<T> | InjectableId<T>, constructor: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;
84
- /**
85
- * Bind an InjectableId to a synchronous factory that will be invoked on demand when the object is needed.
86
- * The factory should produce the needed value
87
- * NOTE: The container will not invoke any `@PostConstruct` present on the class, this is the responsibility of the factory.
88
- */
89
- bindFactory<T>(id: InjectableId<T>, factory: SyncFactory<T>): BindAs<T, SyncFactory<T>>;
90
- /**
91
- * Bind an InjectableId to an asynchronous factory that will be invoked on demand when the object is needed.
92
- * The factory should produce the needed value (asynchronously of course).
93
- * NOTE: The container will not invoke any `@PostConstruct` present on the class, this is the responsibility of the factory.
94
- * WARNING!!! The factory may not throw and must return a valid Promise (which can be pending, resolved, rejected, etc.).
95
- */
96
- bindAsyncFactory<T>(id: InjectableId<T>, factory: AsyncFactory<T>): BindAs<T, AsyncFactory<T>>;
97
- /**
98
- * This essentially pre creates/loads all *singleton* InjectableIds currently known to the Binder.
99
- * This *may* be helpful if you wish to use Injector.get on a dependency tree that has asynchronous singletons within the tree.
100
- *
101
- * @param asyncOnly Only resolve AsyncFactorys as well as any bound classes that have an asynchronous `@PostConstruct` decorator. WARNING: If true, SyncFactorys will *not* be resolved even if they are Singletons.
102
- * @param parentRecursion If true and the the container has a parent, resolveIfSingleton will first be called for the parent
103
- * @returns A Promise that resolves when all Singleton's have been resolved, OR rejects if one or more of the Singleton's failed to resolve. NOTE: Rejection does not occur until all Singleton resolutions have settled, and the rejection reason/err will be a Map<InjectableId, Error>
104
- */
105
- resolveSingletons(asyncOnly?: boolean, parentRecursion?: boolean): Promise<this>;
106
- }
@@ -1,37 +0,0 @@
1
- import { BindableProvider } from './bindable-provider.js';
2
- import { ClassConstructor, InjectableId, Injector } from './injector.js';
3
- import { State } from './state.js';
4
- interface StateResolvingInjector extends Injector {
5
- resolveState<T>(id: InjectableId<T>): State<T>;
6
- }
7
- /**
8
- * @inheritDoc
9
- * This specialization invokes it's configured class constructor synchronously and then scans for (and invokes) any @PostConstruct (which may be synchronous or asynchronous).
10
- */
11
- export declare class ClassBasedProvider<T> extends BindableProvider<T, ClassConstructor<T>> {
12
- constructor(injector: StateResolvingInjector, id: InjectableId<T>, maker: ClassConstructor<T>);
13
- /**
14
- * @inheritDoc
15
- * @see the class description for this Provider.
16
- * This method is just a singleton guard, the real work is done by provideAsStateImpl.
17
- */
18
- provideAsState(): State<T>;
19
- /**
20
- * @inheritDoc
21
- * This specialization returns undefined if 'asyncOnly' is true and there is no asynchronous PostConstruct annotation (since class constructors can never by asynchronous).
22
- */
23
- resolveIfSingleton(asyncOnly: boolean): Promise<T>;
24
- /**
25
- * Make a resolved or pending State that reflects any @PostConstruct annotations.
26
- */
27
- protected makePostConstructState(obj: T): State<T>;
28
- /**
29
- * This method collects the States of all the constructor parameters for our target class.
30
- */
31
- protected getConstructorParameterStates(): State[];
32
- /**
33
- * Gather the needed constructor parameters, invoke the constructor, and figure out what post construction needs done.
34
- */
35
- private provideAsStateImpl;
36
- }
37
- export {};
@@ -1,10 +0,0 @@
1
- import { Provider } from './provider.js';
2
- import { State } from './state.js';
3
- /**
4
- * @inheritDoc
5
- * This specialization is always a Singleton.
6
- */
7
- export declare class ConstantProvider<T> extends Provider<T> {
8
- constructor(constant: T);
9
- provideAsState(): State<T>;
10
- }
@@ -1,8 +0,0 @@
1
- export declare const INJECTABLE_METADATA_KEY = "async-injection:INJECTABLE";
2
- export declare const INJECT_METADATA_KEY = "async-injection:INJECT";
3
- export declare const POSTCONSTRUCT_SYNC_METADATA_KEY = "async-injection:POSTCONSTRUCT_SYNC";
4
- export declare const POSTCONSTRUCT_ASYNC_METADATA_KEY = "async-injection:POSTCONSTRUCT_ASYNC";
5
- export declare const RELEASE_METADATA_KEY = "async-injection:RELEASE";
6
- export declare const OPTIONAL_METADATA_KEY = "async-injection:OPTIONAL";
7
- export declare const REFLECT_PARAMS = "design:paramtypes";
8
- export declare const REFLECT_RETURN = "design:returntype";
@@ -1,89 +0,0 @@
1
- import { AsyncFactory, BindAs, Binder, SyncFactory } from './binder.js';
2
- import { AbstractConstructor, ClassConstructor, InjectableId, Injector } from './injector.js';
3
- import { Provider } from './provider.js';
4
- import { State } from './state.js';
5
- /**
6
- * Binder and Injector (aka Container) to handle (a)synchronous dependency management.
7
- */
8
- export declare class Container implements Binder {
9
- protected parent?: Injector;
10
- /**
11
- * Create a new Container, with an optional parent Injector which will be searched if any given InjectableId is not bound within this Container.
12
- */
13
- constructor(parent?: Injector);
14
- protected providers: Map<InjectableId<any>, Provider<any>>;
15
- /**
16
- * @inheritDoc
17
- */
18
- isIdKnown<T>(id: InjectableId<T>, ascending?: boolean): boolean;
19
- /**
20
- * @inheritDoc
21
- */
22
- get<T>(id: InjectableId<T>): T;
23
- /**
24
- * @inheritDoc
25
- */
26
- resolve<T>(id: InjectableId<T>): Promise<T>;
27
- /**
28
- * This method is not part of the Binding interface, because it is highly unusual.
29
- * But that doesn't mean we can't imagine scenarios where you might require it.
30
- *
31
- * @param id The id to be removed.
32
- * @param ascending If true, this will remove all bindings of the specified id all the way up the parent container chain (if it exists).
33
- * @param releaseIfSingleton If true, @Provider.releaseIfSingleton will be invoked before the binding is removed.
34
- */
35
- removeBinding<T>(id: InjectableId<T>, ascending?: boolean, releaseIfSingleton?: boolean): void;
36
- /**
37
- * @inheritDoc
38
- */
39
- bindConstant<T>(id: InjectableId<T>, value: T): T;
40
- /**
41
- * @inheritDoc
42
- */
43
- bindClass<T>(id: ClassConstructor<T>, constructor?: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;
44
- bindClass<T>(id: string | symbol | AbstractConstructor<T> | InjectableId<T>, constructor: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;
45
- /**
46
- * @inheritDoc
47
- */
48
- bindFactory<T>(id: InjectableId<T>, factory: SyncFactory<T>): BindAs<T, SyncFactory<T>>;
49
- /**
50
- * @inheritDoc
51
- */
52
- bindAsyncFactory<T>(id: InjectableId<T>, factory: AsyncFactory<T>): BindAs<T, AsyncFactory<T>>;
53
- /**
54
- * @inheritDoc
55
- */
56
- resolveSingletons(asyncOnly?: boolean, parentRecursion?: boolean): Promise<this>;
57
- /**
58
- * As implied by the name prefix, this is a factored out method invoked only by the 'resolve' method.
59
- * It makes searching our parent (if it exists) easier (and quicker) IF our parent is a fellow instance of Container.
60
- */
61
- protected resolveState<T>(id: InjectableId<T>): State<T>;
62
- /**
63
- * Convenience method to assist in releasing non-garbage-collectable resources that Singletons in this Container may have allocated.
64
- * It will walk through all registered Providers (of this Container only), and invoke their @see Provider.releaseIfSingleton method.
65
- * This method is not part of the Binding interface, because you normally only create (and release) Containers.
66
- * NOTE:
67
- * This *only* releases active/pending Singleton's that have already been created by this Container.
68
- * The most likely use of this method would be when you have created a new child Container for a limited duration transaction, and you want to easily cleanup temporary resources.
69
- * For example, your service object may need to know when it should unsubscribe from an RxJs stream (failure to do so can result in your Singleton not being garbage collected at the end of a transaction).
70
- * In theory, you could handle all unsubscription and cleanup yourself, but the @Release decorator and this method are meant to simply make that easier.
71
- */
72
- releaseSingletons(): void;
73
- /**
74
- * Make a copy of this @see Container.
75
- * This is an experimental feature!
76
- * I have not thought through all the dark corners, so use at your own peril!
77
- * Here are some notes:
78
- * The injector parameter for SyncFactory and AsyncFactory callbacks will be the Container invoking the factory.
79
- * So a factory that uses a parent closure instead of the supplied injector may get unexpected results.
80
- * The injector parameter for OnSuccess and OnError callbacks will be the Container performing the resolution.
81
- * Singletons are cloned at their *existing* state..
82
- * If resolved in "this" container, they will not be re-resolved for the clone.
83
- * If released by the clone, they will be considered released by "this" container.
84
- * If a singleton is currently being asynchronously constructed any callbacks will reference "this" Container, however both Containers should have no problem awaiting resolution.
85
- * If a singleton is not resolved when the container is cloned, then if both containers resolve, you will create *two* "singletons".
86
- * The way to avoid this last effect is to @see resolveSingletons
87
- */
88
- clone(clazz?: ClassConstructor<Container>): Container;
89
- }
@@ -1,56 +0,0 @@
1
- import { InjectableId } from './injector.js';
2
- /**
3
- * Placed just before the class declaration, this class decorator applies metadata to the class constructor indicating that the user intends to bind the class into the container.
4
- * This decorator will throw if not placed on a class declaration, or if placed more than once on a class declaration.
5
- */
6
- export declare function Injectable(): ClassDecorator;
7
- /**
8
- * Placed just before a constructor parameter, this parameter decorator allows for specificity and control over the type of the type of Object that will be injected into the parameter.
9
- * In the absence of this decorator the container will use whatever is bound to a parameter's type (or throw an error if it is unable to recognize the type).
10
- *
11
- * @param id The identifier of the bound type that should be injected.
12
- */
13
- export declare function Inject(id: InjectableId<any>): ParameterDecorator;
14
- /**
15
- * This is a helper function used by the container to retrieve the @Inject metadata for a specifically indexed constructor parameter
16
- *
17
- * @param target The constructor function of the class (we don't allow @Inject on anything else).
18
- * @param parameterIndex The ordinal index of the parameter in the constructor’s parameter list
19
- * @see Inject
20
- */
21
- export declare function _getInjectedIdAt(target: Function, parameterIndex: number): InjectableId<any>;
22
- /**
23
- * Placed just before a constructor parameter, this parameter decorator signals the container that it should supply the 'alt' constant value (undefined by default) if for *any* reason it is unable to otherwise resolve the type of the parameter.
24
- * WARNING! It is your responsibility to ensure that alt is of the appropriate type/value.
25
- */
26
- export declare function Optional(alt?: any): ParameterDecorator;
27
- /**
28
- * This is a helper function used by the container to retrieve the @Optional metadata for a specifically indexed constructor parameter
29
- *
30
- * @param target The constructor function of the class (we don't allow @Optional on anything else).
31
- * @param parameterIndex The ordinal index of the parameter in the constructor’s parameter list
32
- * @see Optional
33
- * @returns an object containing the value provided in the decorator, or undefined if no annotation was present.
34
- */
35
- export declare function _getOptionalDefaultAt(target: Function, parameterIndex: number): {
36
- value: any;
37
- };
38
- /**
39
- * Placed just before a class method, this method decorator flags a method that should be called after an object has been instantiated by the container, but before it is put into service.
40
- * The method will be assumed to be synchronous unless the method signature explicitly declares it's return type to be ": Promise<something>"
41
- * This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.
42
- */
43
- export declare function PostConstruct(): MethodDecorator;
44
- /**
45
- * Placed just before a class method, this decorator identifies a method which should be called when an object is removed from service.
46
- * If invoked by the container, the container will drop any references it has to the object when the method returns.
47
- * Note that this decorator is *not* a guarantee (or even an implication) that the decorated method will be called (JavaScript has no mechanism to enforce such a contract).
48
- * This decorator simply serves as a flag to indicate a method which is intended to clean up resources allocated by the object *which would not otherwise be garbage collected*.
49
- * You should *not* use this decorator as a general "object finalization" method. It has very limited scope and purpose.
50
- * The decorated method must complete normally (no throwing), as "release" is not an abort-able process.
51
- * This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.
52
- * The @see InvokeReleaseMethod helper function can search for and invoke the @Release decorated method of an object.
53
- * Also @see Container.releaseSingletons for the intended usage of this decorator.
54
- * It is intended that after the @Release decorated method of an object is called, that object will not be used again, but this is of course not enforced).
55
- */
56
- export declare function Release(): MethodDecorator;
@@ -1,4 +0,0 @@
1
- export { Binder, SyncFactory, AsyncFactory, BindAs } from './binder.js';
2
- export { Container } from './container.js';
3
- export { Inject, Injectable, Optional, PostConstruct, Release } from './decorators.js';
4
- export { ClassConstructor, InjectableId, InjectionToken, Injector } from './injector.js';
@@ -1,47 +0,0 @@
1
- /**
2
- * This is about as close as we can get in Typescript
3
- */
4
- export type AbstractConstructor<T> = Function & {
5
- prototype: T;
6
- };
7
- /**
8
- * Standard definition of a constructor.
9
- */
10
- export type ClassConstructor<T> = new (...args: any[]) => T;
11
- /**
12
- * Allow for implicit typing of constants and interfaces.
13
- * Inspired by Angular and some colleges at work.
14
- */
15
- export declare class InjectionToken<T> {
16
- private id;
17
- constructor(id: string | symbol);
18
- toString(): string;
19
- }
20
- /**
21
- * Universal id that can be bound to a constant, class, or factories.
22
- */
23
- export type InjectableId<T> = string | symbol | AbstractConstructor<T> | ClassConstructor<T> | InjectionToken<T>;
24
- /**
25
- * Retrieve instances previously bound to the specified InjectableId.
26
- */
27
- export interface Injector {
28
- /**
29
- * Check to see if the existing InjectableId is known (aka has been bound).
30
- * Error callbacks may wish to know if a particular InjectableId is available.
31
- * Also the Binder's bindXXX calls always overwrite any previous bindings, so you may want to use this as a gate.
32
- *
33
- * @param id The id to check for.
34
- * @param ascending If true, this will search up the chain of parent containers (if they exist). Default is false (only checks if the id is bound within this container).
35
- */
36
- isIdKnown<T>(id: InjectableId<T> | AbstractConstructor<T>, ascending?: boolean): boolean;
37
- /**
38
- * Return an instance of <T> previously bound to 'id'.
39
- *
40
- * @throws Error if the InjectableId was never registered, OR if there are unresolved asynchronous dependencies in the dependency tree for 'id'.
41
- */
42
- get<T>(id: InjectableId<T> | AbstractConstructor<T>): T;
43
- /**
44
- * awaits the asynchronous resolution of all dependencies in the tree for 'id'.
45
- */
46
- resolve<T>(id?: InjectableId<T> | AbstractConstructor<T>): Promise<T>;
47
- }
@@ -1,37 +0,0 @@
1
- import { State } from './state.js';
2
- /**
3
- * Internally all InjectableIds are mapped to an abstract Provider<T>.
4
- * A Provider may choose to return a singleton or a new value each time it is queried.
5
- */
6
- export declare abstract class Provider<T = any> {
7
- protected constructor();
8
- /**
9
- * If the provider is configured as a singleton, this property will be the state of that singleton.
10
- * This value will be defined for resolved/resolving Singletons, null for Singletons that have not yet been queried, and will remain undefined for non-Singleton Providers.
11
- * Default value is undefined (e.g. not a Singleton).
12
- */
13
- protected singleton?: State<T>;
14
- /**
15
- * This is the workhorse method of the Provider, and is invoked directly or indirectly by both Injector.get and Injector.resolve.
16
- * This method returns the current State<T> if it is already known (which it might be for Singleton scenarios).
17
- * Otherwise it resolves the State<T>.
18
- * IF the Provider<T> is a Singleton, it's State<T> is updated before returning.
19
- */
20
- abstract provideAsState(): State<T>;
21
- /**
22
- * Base method to initialize the state of this Provider *if* (and only if) it has been configured as a Singleton.
23
- * If this Provider has not been configured as a singleton, this method is essentially a noop that returns undefined.
24
- *
25
- * @param asyncOnly This default implementation ignores this parameter.
26
- * @returns A completion Promise if initialization requires asynchronicity, otherwise the return value is undefined.
27
- */
28
- resolveIfSingleton(asyncOnly: boolean): Promise<T>;
29
- /**
30
- * If (and only if) this Provider has been configured as a Singleton, and if it has been (or is being resolved), find and invoke the @Release decorated method (if there is one).
31
- * NOTE that if the singleton is actively being resolved when this method is called, this method waits for the resolution to complete and then invokes the @Release decorated method; But in any case this is a synchronous method and returns immediately to it's caller.
32
- * Also note that invoking this method does not release or invalidate the Provider;
33
- * Rather, it resets a Singleton Provider to a fresh (unresolved/unqueried) state (aka sets this.singleton to null).
34
- * It is assumed that the Singleton itself will no longer be used after this method returns.
35
- */
36
- releaseIfSingleton(): void;
37
- }
@@ -1,15 +0,0 @@
1
- /**
2
- * Internal class that allows us to track the state of a promise (chain).
3
- */
4
- export declare class State<T = any> {
5
- static MakeState<TState = any>(promise: Promise<TState>, rejected?: unknown, fulfilled?: TState): State<TState>;
6
- protected constructor();
7
- protected _promise: Promise<T>;
8
- get promise(): Promise<T>;
9
- protected _pending: boolean;
10
- get pending(): boolean;
11
- protected _fulfilled: T;
12
- get fulfilled(): T;
13
- protected _rejected: unknown;
14
- get rejected(): unknown;
15
- }
@@ -1,21 +0,0 @@
1
- import { BindableProvider } from './bindable-provider.js';
2
- import { SyncFactory } from './binder.js';
3
- import { InjectableId, Injector } from './injector.js';
4
- import { State } from './state.js';
5
- /**
6
- * @inheritDoc
7
- * This specialization simply invokes it's configured Factory and provides the result.
8
- */
9
- export declare class FactoryBasedProvider<T> extends BindableProvider<T, SyncFactory<T>> {
10
- constructor(injector: Injector, id: InjectableId<T>, maker: SyncFactory<T>);
11
- /**
12
- * @inheritDoc
13
- * This specialization invokes it's configured Factory and provides the result (or invokes the error handler if necessary).
14
- */
15
- provideAsState(): State<T>;
16
- /**
17
- * @inheritDoc
18
- * This specialization returns undefined anytime 'asyncOnly' is true (since this Provider is by definition synchronous).
19
- */
20
- resolveIfSingleton(asyncOnly: boolean): Promise<T>;
21
- }
@@ -1,14 +0,0 @@
1
- /**
2
- * Returns true if the specified object looks like a JavaScript Error object.
3
- */
4
- export declare function isErrorObj(err: any): err is Error;
5
- /**
6
- * Returns true if the specified value is "thenable" (aka a Promise).
7
- */
8
- export declare function isPromise<T>(value: any): value is Promise<T>;
9
- /**
10
- * Simple helper function to find the @Release decorated method of an object (if any), and invoke it.
11
- * This is primarily an internal method as you probably know the exact method, and should invoke it yourself.
12
- * async-injection uses this helper to allow Singletons to clean up any non-garbage-collectable resources they may have allocated.
13
- */
14
- export declare function InvokeReleaseMethod<T = unknown>(obj: T): boolean;
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes