ts-ioc-container 67.0.0 → 68.1.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.
- package/README.md +240 -92
- package/cjm/errors/ArgumentNotFoundError.js +12 -0
- package/cjm/hooks/HookCollector.js +34 -0
- package/cjm/hooks/hook.js +3 -11
- package/cjm/index.js +14 -21
- package/cjm/utils/array.js +10 -1
- package/cjm/utils/getConstructorChain.js +13 -0
- package/cjm/utils/memoize.js +18 -0
- package/esm/errors/ArgumentNotFoundError.js +8 -0
- package/esm/hooks/HookCollector.js +29 -0
- package/esm/hooks/hook.js +1 -9
- package/esm/index.js +5 -7
- package/esm/utils/array.js +8 -0
- package/esm/utils/getConstructorChain.js +9 -0
- package/esm/utils/memoize.js +14 -0
- package/package.json +1 -1
- package/typings/errors/ArgumentNotFoundError.d.ts +5 -0
- package/typings/hooks/HookCollector.d.ts +28 -0
- package/typings/index.d.ts +5 -7
- package/typings/utils/array.d.ts +2 -0
- package/typings/utils/getConstructorChain.d.ts +1 -0
- package/typings/utils/memoize.d.ts +1 -0
- package/cjm/hooks/HookExecutionStrategy.js +0 -48
- package/cjm/hooks/ParallelAsync.js +0 -11
- package/cjm/hooks/SequentialAsync.js +0 -11
- package/cjm/hooks/SequentialSync.js +0 -12
- package/cjm/hooks/onConstruct.js +0 -18
- package/cjm/hooks/onResolved.js +0 -27
- package/cjm/hooks/onScopeDisposed.js +0 -20
- package/esm/hooks/HookExecutionStrategy.js +0 -44
- package/esm/hooks/ParallelAsync.js +0 -7
- package/esm/hooks/SequentialAsync.js +0 -7
- package/esm/hooks/SequentialSync.js +0 -8
- package/esm/hooks/onConstruct.js +0 -13
- package/esm/hooks/onResolved.js +0 -21
- package/esm/hooks/onScopeDisposed.js +0 -15
- package/typings/hooks/HookExecutionStrategy.d.ts +0 -33
- package/typings/hooks/ParallelAsync.d.ts +0 -4
- package/typings/hooks/SequentialAsync.d.ts +0 -4
- package/typings/hooks/SequentialSync.d.ts +0 -4
- package/typings/hooks/onConstruct.d.ts +0 -9
- package/typings/hooks/onResolved.d.ts +0 -10
- package/typings/hooks/onScopeDisposed.d.ts +0 -9
package/cjm/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.throttle = exports.runAtOnce = exports.runInOrder = exports.handleAsyncError = exports.handleError = exports.getMethodTags = exports.addMethodTag = exports.getMethodLabels = exports.addMethodLabel = exports.getMethodMeta = exports.addMethodMeta = exports.getParamTags = exports.addParamTag = exports.getParamLabels = exports.addParamLabel = exports.getParamMeta = exports.addParamMeta = exports.getClassTags = exports.addClassTag = exports.getClassLabels = exports.addClassLabel = exports.getClassMeta = exports.addClassMeta = exports.resolveConstructor = exports.GroupInstanceToken = exports.ConstantToken = exports.FunctionToken = exports.SingleToken = exports.ClassToken = exports.toSingleAlias = exports.SingleAliasToken = exports.toGroupAlias = exports.GroupAliasToken = exports.argToToken = exports.toMappedToken = exports.toToken = exports.InjectionToken = exports.
|
|
5
|
-
exports.Is = exports.unwrapProxy = exports.ProxyRegistry = exports.
|
|
3
|
+
exports.injectProp = exports.createHookExecutionContext = exports.createHookContextFactory = exports.HookContext = exports.toHookFn = exports.hasHooks = exports.hook = exports.getHooks = exports.ArgumentNotFoundError = exports.TypedEventDisposedError = exports.UnsupportedTokenTypeError = exports.CannonSingletonApplyTwiceError = exports.ProviderDisposedError = exports.ContainerDisposedError = exports.MethodNotImplementedError = exports.DependencyMissingKeyError = exports.ContainerNotFoundError = exports.DependencyNotFoundError = exports.ContainerError = exports.Registration = exports.onResolve = exports.appendArgsFn = exports.appendArgs = exports.decorate = exports.singleton = exports.autoResolve = exports.lazy = exports.scopeAccess = exports.scope = exports.bindTo = exports.register = exports.toRegistrationFn = exports.toProviderFn = exports.toBindToken = exports.registerPipe = exports.isProviderPipe = exports.Provider = exports.ProxyInjector = exports.SimpleInjector = exports.resolveArgs = exports.argsFn = exports.args = exports.arg = exports.inject = exports.MetadataInjector = exports.Injector = exports.EmptyContainer = exports.AutoResolveModule = exports.Container = exports.isDependencyKey = void 0;
|
|
4
|
+
exports.pipe = exports.select = exports.TypedEvent = exports.getConstructorChain = exports.memoize = exports.once = exports.shallowCache = exports.debounce = exports.throttle = exports.runAtOnce = exports.runInOrder = exports.handleAsyncError = exports.handleError = exports.getMethodTags = exports.addMethodTag = exports.getMethodLabels = exports.addMethodLabel = exports.getMethodMeta = exports.addMethodMeta = exports.getParamTags = exports.addParamTag = exports.getParamLabels = exports.addParamLabel = exports.getParamMeta = exports.addParamMeta = exports.getClassTags = exports.addClassTag = exports.getClassLabels = exports.addClassLabel = exports.getClassMeta = exports.addClassMeta = exports.resolveConstructor = exports.GroupInstanceToken = exports.ConstantToken = exports.FunctionToken = exports.SingleToken = exports.ClassToken = exports.toSingleAlias = exports.SingleAliasToken = exports.toGroupAlias = exports.GroupAliasToken = exports.argToToken = exports.toMappedToken = exports.toToken = exports.InjectionToken = exports.toTask = exports.HookCollector = exports.oncePerInstance = exports.parallel = exports.sequential = void 0;
|
|
5
|
+
exports.Is = exports.unwrapProxy = exports.ProxyRegistry = exports.findOrFail = void 0;
|
|
6
6
|
var IContainer_1 = require("./container/IContainer");
|
|
7
7
|
Object.defineProperty(exports, "isDependencyKey", { enumerable: true, get: function () { return IContainer_1.isDependencyKey; } });
|
|
8
8
|
var Container_1 = require("./container/Container");
|
|
@@ -65,6 +65,8 @@ var UnsupportedTokenTypeError_1 = require("./errors/UnsupportedTokenTypeError");
|
|
|
65
65
|
Object.defineProperty(exports, "UnsupportedTokenTypeError", { enumerable: true, get: function () { return UnsupportedTokenTypeError_1.UnsupportedTokenTypeError; } });
|
|
66
66
|
var TypedEventDisposedError_1 = require("./errors/TypedEventDisposedError");
|
|
67
67
|
Object.defineProperty(exports, "TypedEventDisposedError", { enumerable: true, get: function () { return TypedEventDisposedError_1.TypedEventDisposedError; } });
|
|
68
|
+
var ArgumentNotFoundError_1 = require("./errors/ArgumentNotFoundError");
|
|
69
|
+
Object.defineProperty(exports, "ArgumentNotFoundError", { enumerable: true, get: function () { return ArgumentNotFoundError_1.ArgumentNotFoundError; } });
|
|
68
70
|
var hook_1 = require("./hooks/hook");
|
|
69
71
|
Object.defineProperty(exports, "getHooks", { enumerable: true, get: function () { return hook_1.getHooks; } });
|
|
70
72
|
Object.defineProperty(exports, "hook", { enumerable: true, get: function () { return hook_1.hook; } });
|
|
@@ -76,28 +78,13 @@ Object.defineProperty(exports, "createHookContextFactory", { enumerable: true, g
|
|
|
76
78
|
Object.defineProperty(exports, "createHookExecutionContext", { enumerable: true, get: function () { return HookContext_1.createHookExecutionContext; } });
|
|
77
79
|
var injectProp_1 = require("./hooks/injectProp");
|
|
78
80
|
Object.defineProperty(exports, "injectProp", { enumerable: true, get: function () { return injectProp_1.injectProp; } });
|
|
79
|
-
var onConstruct_1 = require("./hooks/onConstruct");
|
|
80
|
-
Object.defineProperty(exports, "onConstruct", { enumerable: true, get: function () { return onConstruct_1.onConstruct; } });
|
|
81
|
-
Object.defineProperty(exports, "OnConstructModule", { enumerable: true, get: function () { return onConstruct_1.OnConstructModule; } });
|
|
82
|
-
var onScopeDisposed_1 = require("./hooks/onScopeDisposed");
|
|
83
|
-
Object.defineProperty(exports, "onScopeDisposed", { enumerable: true, get: function () { return onScopeDisposed_1.onScopeDisposed; } });
|
|
84
|
-
Object.defineProperty(exports, "OnDisposeModule", { enumerable: true, get: function () { return onScopeDisposed_1.OnDisposeModule; } });
|
|
85
|
-
var onResolved_1 = require("./hooks/onResolved");
|
|
86
|
-
Object.defineProperty(exports, "onResolved", { enumerable: true, get: function () { return onResolved_1.onResolved; } });
|
|
87
|
-
Object.defineProperty(exports, "OnResolvedModule", { enumerable: true, get: function () { return onResolved_1.OnResolvedModule; } });
|
|
88
|
-
Object.defineProperty(exports, "resolved", { enumerable: true, get: function () { return onResolved_1.resolved; } });
|
|
89
81
|
var combinators_1 = require("./hooks/combinators");
|
|
90
82
|
Object.defineProperty(exports, "sequential", { enumerable: true, get: function () { return combinators_1.sequential; } });
|
|
91
83
|
Object.defineProperty(exports, "parallel", { enumerable: true, get: function () { return combinators_1.parallel; } });
|
|
92
84
|
Object.defineProperty(exports, "oncePerInstance", { enumerable: true, get: function () { return combinators_1.oncePerInstance; } });
|
|
93
|
-
var
|
|
94
|
-
Object.defineProperty(exports, "
|
|
95
|
-
|
|
96
|
-
Object.defineProperty(exports, "SequentialSync", { enumerable: true, get: function () { return SequentialSync_1.SequentialSync; } });
|
|
97
|
-
var SequentialAsync_1 = require("./hooks/SequentialAsync");
|
|
98
|
-
Object.defineProperty(exports, "SequentialAsync", { enumerable: true, get: function () { return SequentialAsync_1.SequentialAsync; } });
|
|
99
|
-
var ParallelAsync_1 = require("./hooks/ParallelAsync");
|
|
100
|
-
Object.defineProperty(exports, "ParallelAsync", { enumerable: true, get: function () { return ParallelAsync_1.ParallelAsync; } });
|
|
85
|
+
var HookCollector_1 = require("./hooks/HookCollector");
|
|
86
|
+
Object.defineProperty(exports, "HookCollector", { enumerable: true, get: function () { return HookCollector_1.HookCollector; } });
|
|
87
|
+
Object.defineProperty(exports, "toTask", { enumerable: true, get: function () { return HookCollector_1.toTask; } });
|
|
101
88
|
var InjectionToken_1 = require("./token/InjectionToken");
|
|
102
89
|
Object.defineProperty(exports, "InjectionToken", { enumerable: true, get: function () { return InjectionToken_1.InjectionToken; } });
|
|
103
90
|
var toToken_1 = require("./token/toToken");
|
|
@@ -157,12 +144,18 @@ var shallowCache_1 = require("./utils/shallowCache");
|
|
|
157
144
|
Object.defineProperty(exports, "shallowCache", { enumerable: true, get: function () { return shallowCache_1.shallowCache; } });
|
|
158
145
|
var once_1 = require("./utils/once");
|
|
159
146
|
Object.defineProperty(exports, "once", { enumerable: true, get: function () { return once_1.once; } });
|
|
147
|
+
var memoize_1 = require("./utils/memoize");
|
|
148
|
+
Object.defineProperty(exports, "memoize", { enumerable: true, get: function () { return memoize_1.memoize; } });
|
|
149
|
+
var getConstructorChain_1 = require("./utils/getConstructorChain");
|
|
150
|
+
Object.defineProperty(exports, "getConstructorChain", { enumerable: true, get: function () { return getConstructorChain_1.getConstructorChain; } });
|
|
160
151
|
var TypedEvent_1 = require("./utils/TypedEvent");
|
|
161
152
|
Object.defineProperty(exports, "TypedEvent", { enumerable: true, get: function () { return TypedEvent_1.TypedEvent; } });
|
|
162
153
|
var select_1 = require("./select");
|
|
163
154
|
Object.defineProperty(exports, "select", { enumerable: true, get: function () { return select_1.select; } });
|
|
164
155
|
var fp_1 = require("./utils/fp");
|
|
165
156
|
Object.defineProperty(exports, "pipe", { enumerable: true, get: function () { return fp_1.pipe; } });
|
|
157
|
+
var array_1 = require("./utils/array");
|
|
158
|
+
Object.defineProperty(exports, "findOrFail", { enumerable: true, get: function () { return array_1.findOrFail; } });
|
|
166
159
|
var ProxyRegistry_1 = require("./utils/ProxyRegistry");
|
|
167
160
|
Object.defineProperty(exports, "ProxyRegistry", { enumerable: true, get: function () { return ProxyRegistry_1.ProxyRegistry; } });
|
|
168
161
|
Object.defineProperty(exports, "unwrapProxy", { enumerable: true, get: function () { return ProxyRegistry_1.unwrapProxy; } });
|
package/cjm/utils/array.js
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Filter = void 0;
|
|
3
|
+
exports.findOrFail = exports.Filter = void 0;
|
|
4
|
+
const ArgumentNotFoundError_1 = require("../errors/ArgumentNotFoundError");
|
|
4
5
|
exports.Filter = {
|
|
5
6
|
exclude: (arr) => {
|
|
6
7
|
const excludeSet = arr instanceof Array ? new Set(arr) : arr;
|
|
7
8
|
return (v) => !excludeSet.has(v);
|
|
8
9
|
},
|
|
9
10
|
};
|
|
11
|
+
const findOrFail = (predicate) => (...args) => {
|
|
12
|
+
const index = args.findIndex((arg) => predicate(arg));
|
|
13
|
+
if (index === -1) {
|
|
14
|
+
throw new ArgumentNotFoundError_1.ArgumentNotFoundError();
|
|
15
|
+
}
|
|
16
|
+
return args[index];
|
|
17
|
+
};
|
|
18
|
+
exports.findOrFail = findOrFail;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getConstructorChain = void 0;
|
|
4
|
+
const getConstructorChain = (ctor) => {
|
|
5
|
+
const chain = [];
|
|
6
|
+
let current = ctor;
|
|
7
|
+
while (typeof current === 'function' && current !== Function.prototype) {
|
|
8
|
+
chain.push(current);
|
|
9
|
+
current = Object.getPrototypeOf(current);
|
|
10
|
+
}
|
|
11
|
+
return chain;
|
|
12
|
+
};
|
|
13
|
+
exports.getConstructorChain = getConstructorChain;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.memoize = void 0;
|
|
4
|
+
const memoize = (fn) => {
|
|
5
|
+
const cache = new WeakMap();
|
|
6
|
+
return (target, key) => {
|
|
7
|
+
let byKey = cache.get(target);
|
|
8
|
+
if (!byKey) {
|
|
9
|
+
byKey = new Map();
|
|
10
|
+
cache.set(target, byKey);
|
|
11
|
+
}
|
|
12
|
+
if (!byKey.has(key)) {
|
|
13
|
+
byKey.set(key, fn(target, key));
|
|
14
|
+
}
|
|
15
|
+
return byKey.get(key);
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
exports.memoize = memoize;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ContainerError } from './ContainerError.js';
|
|
2
|
+
export class ArgumentNotFoundError extends ContainerError {
|
|
3
|
+
name = 'ArgumentNotFoundError';
|
|
4
|
+
constructor(message = 'Argument not found') {
|
|
5
|
+
super(message);
|
|
6
|
+
Object.setPrototypeOf(this, ArgumentNotFoundError.prototype);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { resolveConstructor } from '../metadata/target.js';
|
|
2
|
+
import { memoize } from '../utils/memoize.js';
|
|
3
|
+
import { getHooks, hasHooks, toHookFn } from './hook.js';
|
|
4
|
+
import { createHookExecutionContext } from './HookContext.js';
|
|
5
|
+
export class HookCollector {
|
|
6
|
+
key;
|
|
7
|
+
options;
|
|
8
|
+
hooksOf = memoize(getHooks);
|
|
9
|
+
constructor({ key, createExecutionContext = createHookExecutionContext, mapExecutionContext = (context) => context, predicate = () => true, }) {
|
|
10
|
+
this.key = key;
|
|
11
|
+
this.options = { createExecutionContext, mapExecutionContext, predicate };
|
|
12
|
+
}
|
|
13
|
+
hasHooks(target) {
|
|
14
|
+
return hasHooks(target, this.key);
|
|
15
|
+
}
|
|
16
|
+
getActions(target, { scope, createExecutionContext = this.options.createExecutionContext, mapExecutionContext = this.options.mapExecutionContext, predicate = this.options.predicate, }) {
|
|
17
|
+
const actions = [];
|
|
18
|
+
for (const [methodName, fn] of this.hooksOf(resolveConstructor(target), this.key)) {
|
|
19
|
+
if (predicate(methodName)) {
|
|
20
|
+
actions.push({
|
|
21
|
+
hook: toHookFn(fn),
|
|
22
|
+
context: mapExecutionContext(createExecutionContext(target, scope, methodName)),
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return actions;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export const toTask = ({ hook, context }) => () => hook(context);
|
package/esm/hooks/hook.js
CHANGED
|
@@ -1,18 +1,10 @@
|
|
|
1
1
|
import { Is } from '../utils/basic.js';
|
|
2
2
|
import { resolveConstructor } from '../metadata/target.js';
|
|
3
|
+
import { getConstructorChain } from '../utils/getConstructorChain.js';
|
|
3
4
|
const isHookClassConstructor = (execute) => {
|
|
4
5
|
return Is.constructor(execute) && execute.prototype.execute;
|
|
5
6
|
};
|
|
6
7
|
export const toHookFn = (execute) => isHookClassConstructor(execute) ? (context) => context.scope.resolve(execute).execute(context) : execute;
|
|
7
|
-
const getConstructorChain = (ctor) => {
|
|
8
|
-
const chain = [];
|
|
9
|
-
let current = ctor;
|
|
10
|
-
while (typeof current === 'function' && current !== Function.prototype) {
|
|
11
|
-
chain.push(current);
|
|
12
|
-
current = Object.getPrototypeOf(current);
|
|
13
|
-
}
|
|
14
|
-
return chain;
|
|
15
|
-
};
|
|
16
8
|
export function getHooks(target, key) {
|
|
17
9
|
const merged = new Map();
|
|
18
10
|
for (const ctor of getConstructorChain(resolveConstructor(target)).reverse()) {
|
package/esm/index.js
CHANGED
|
@@ -19,17 +19,12 @@ export { ProviderDisposedError } from './errors/ProviderDisposedError.js';
|
|
|
19
19
|
export { CannonSingletonApplyTwiceError } from './errors/CannonSingletonApplyTwiceError.js';
|
|
20
20
|
export { UnsupportedTokenTypeError } from './errors/UnsupportedTokenTypeError.js';
|
|
21
21
|
export { TypedEventDisposedError } from './errors/TypedEventDisposedError.js';
|
|
22
|
+
export { ArgumentNotFoundError } from './errors/ArgumentNotFoundError.js';
|
|
22
23
|
export { getHooks, hook, hasHooks, toHookFn, } from './hooks/hook.js';
|
|
23
24
|
export { HookContext, createHookContextFactory, createHookExecutionContext, } from './hooks/HookContext.js';
|
|
24
25
|
export { injectProp } from './hooks/injectProp.js';
|
|
25
|
-
export { onConstruct, OnConstructModule } from './hooks/onConstruct.js';
|
|
26
|
-
export { onScopeDisposed, OnDisposeModule } from './hooks/onScopeDisposed.js';
|
|
27
|
-
export { onResolved, OnResolvedModule, resolved } from './hooks/onResolved.js';
|
|
28
26
|
export { sequential, parallel, oncePerInstance } from './hooks/combinators.js';
|
|
29
|
-
export {
|
|
30
|
-
export { SequentialSync } from './hooks/SequentialSync.js';
|
|
31
|
-
export { SequentialAsync } from './hooks/SequentialAsync.js';
|
|
32
|
-
export { ParallelAsync } from './hooks/ParallelAsync.js';
|
|
27
|
+
export { HookCollector, toTask, } from './hooks/HookCollector.js';
|
|
33
28
|
export { InjectionToken } from './token/InjectionToken.js';
|
|
34
29
|
export { toToken, toMappedToken, argToToken } from './token/toToken.js';
|
|
35
30
|
export { GroupAliasToken, toGroupAlias } from './token/GroupAliasToken.js';
|
|
@@ -49,8 +44,11 @@ export { throttle } from './utils/throttle.js';
|
|
|
49
44
|
export { debounce } from './utils/debounce.js';
|
|
50
45
|
export { shallowCache } from './utils/shallowCache.js';
|
|
51
46
|
export { once } from './utils/once.js';
|
|
47
|
+
export { memoize } from './utils/memoize.js';
|
|
48
|
+
export { getConstructorChain } from './utils/getConstructorChain.js';
|
|
52
49
|
export { TypedEvent } from './utils/TypedEvent.js';
|
|
53
50
|
export { select } from './select.js';
|
|
54
51
|
export { pipe } from './utils/fp.js';
|
|
52
|
+
export { findOrFail } from './utils/array.js';
|
|
55
53
|
export { ProxyRegistry, unwrapProxy } from './utils/ProxyRegistry.js';
|
|
56
54
|
export { Is } from './utils/basic.js';
|
package/esm/utils/array.js
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
|
+
import { ArgumentNotFoundError } from '../errors/ArgumentNotFoundError.js';
|
|
1
2
|
export const Filter = {
|
|
2
3
|
exclude: (arr) => {
|
|
3
4
|
const excludeSet = arr instanceof Array ? new Set(arr) : arr;
|
|
4
5
|
return (v) => !excludeSet.has(v);
|
|
5
6
|
},
|
|
6
7
|
};
|
|
8
|
+
export const findOrFail = (predicate) => (...args) => {
|
|
9
|
+
const index = args.findIndex((arg) => predicate(arg));
|
|
10
|
+
if (index === -1) {
|
|
11
|
+
throw new ArgumentNotFoundError();
|
|
12
|
+
}
|
|
13
|
+
return args[index];
|
|
14
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export const memoize = (fn) => {
|
|
2
|
+
const cache = new WeakMap();
|
|
3
|
+
return (target, key) => {
|
|
4
|
+
let byKey = cache.get(target);
|
|
5
|
+
if (!byKey) {
|
|
6
|
+
byKey = new Map();
|
|
7
|
+
cache.set(target, byKey);
|
|
8
|
+
}
|
|
9
|
+
if (!byKey.has(key)) {
|
|
10
|
+
byKey.set(key, fn(target, key));
|
|
11
|
+
}
|
|
12
|
+
return byKey.get(key);
|
|
13
|
+
};
|
|
14
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-ioc-container",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "68.1.0",
|
|
4
4
|
"description": "Fast, lightweight TypeScript dependency injection container with a clean API, scoped lifecycles, decorators, tokens, hooks, lazy injection, customizable providers, and no global container objects.",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public",
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ExecutionContext } from '../ExecutionContext.js';
|
|
2
|
+
import { type Task } from '../utils/task.js';
|
|
3
|
+
import { type Instance } from '../utils/basic.js';
|
|
4
|
+
import { type HookFn } from './hook.js';
|
|
5
|
+
import { type CreateHookExecutionContext, type IHookContext } from './HookContext.js';
|
|
6
|
+
export type MapHookExecutionContext = (context: IHookContext) => IHookContext;
|
|
7
|
+
export type HookAction = {
|
|
8
|
+
hook: HookFn;
|
|
9
|
+
context: IHookContext;
|
|
10
|
+
};
|
|
11
|
+
export type HookCollectorOptions = {
|
|
12
|
+
createExecutionContext?: CreateHookExecutionContext;
|
|
13
|
+
mapExecutionContext?: MapHookExecutionContext;
|
|
14
|
+
predicate?: (methodName: string) => boolean;
|
|
15
|
+
};
|
|
16
|
+
export type HookCollectionContext = ExecutionContext & HookCollectorOptions;
|
|
17
|
+
export type HookCollectorProps = HookCollectorOptions & {
|
|
18
|
+
key: string | symbol;
|
|
19
|
+
};
|
|
20
|
+
export declare class HookCollector {
|
|
21
|
+
private readonly key;
|
|
22
|
+
private readonly options;
|
|
23
|
+
private readonly hooksOf;
|
|
24
|
+
constructor({ key, createExecutionContext, mapExecutionContext, predicate, }: HookCollectorProps);
|
|
25
|
+
hasHooks(target: Instance): boolean;
|
|
26
|
+
getActions(target: Instance, { scope, createExecutionContext, mapExecutionContext, predicate, }: HookCollectionContext): HookAction[];
|
|
27
|
+
}
|
|
28
|
+
export declare const toTask: ({ hook, context }: HookAction) => Task;
|
package/typings/index.d.ts
CHANGED
|
@@ -20,17 +20,12 @@ export { ProviderDisposedError } from './errors/ProviderDisposedError.js';
|
|
|
20
20
|
export { CannonSingletonApplyTwiceError } from './errors/CannonSingletonApplyTwiceError.js';
|
|
21
21
|
export { UnsupportedTokenTypeError } from './errors/UnsupportedTokenTypeError.js';
|
|
22
22
|
export { TypedEventDisposedError } from './errors/TypedEventDisposedError.js';
|
|
23
|
+
export { ArgumentNotFoundError } from './errors/ArgumentNotFoundError.js';
|
|
23
24
|
export { getHooks, hook, hasHooks, toHookFn, type HookFn, type HookClass, type HookType, type InjectFn, type HooksOfClass, } from './hooks/hook.js';
|
|
24
25
|
export { HookContext, createHookContextFactory, createHookExecutionContext, type CreateHookExecutionContext, type IHookContext, } from './hooks/HookContext.js';
|
|
25
26
|
export { injectProp } from './hooks/injectProp.js';
|
|
26
|
-
export { onConstruct, OnConstructModule } from './hooks/onConstruct.js';
|
|
27
|
-
export { onScopeDisposed, OnDisposeModule } from './hooks/onScopeDisposed.js';
|
|
28
|
-
export { onResolved, OnResolvedModule, resolved } from './hooks/onResolved.js';
|
|
29
27
|
export { sequential, parallel, oncePerInstance, type ResolvedObjectHook } from './hooks/combinators.js';
|
|
30
|
-
export {
|
|
31
|
-
export { SequentialSync } from './hooks/SequentialSync.js';
|
|
32
|
-
export { SequentialAsync } from './hooks/SequentialAsync.js';
|
|
33
|
-
export { ParallelAsync } from './hooks/ParallelAsync.js';
|
|
28
|
+
export { HookCollector, toTask, type HookAction, type HookCollectionContext, type HookCollectorOptions, type HookCollectorProps, type MapHookExecutionContext, } from './hooks/HookCollector.js';
|
|
34
29
|
export { InjectionToken } from './token/InjectionToken.js';
|
|
35
30
|
export { type Injectable, toToken, toMappedToken, argToToken } from './token/toToken.js';
|
|
36
31
|
export { GroupAliasToken, toGroupAlias } from './token/GroupAliasToken.js';
|
|
@@ -50,9 +45,12 @@ export { throttle } from './utils/throttle.js';
|
|
|
50
45
|
export { debounce } from './utils/debounce.js';
|
|
51
46
|
export { shallowCache } from './utils/shallowCache.js';
|
|
52
47
|
export { once } from './utils/once.js';
|
|
48
|
+
export { memoize } from './utils/memoize.js';
|
|
49
|
+
export { getConstructorChain } from './utils/getConstructorChain.js';
|
|
53
50
|
export { TypedEvent, type ITypedEvent, type TypedEventListener, type Unsubscribe } from './utils/TypedEvent.js';
|
|
54
51
|
export { type ExecutionContext } from './ExecutionContext.js';
|
|
55
52
|
export { select } from './select.js';
|
|
56
53
|
export { pipe, type MapFn } from './utils/fp.js';
|
|
54
|
+
export { findOrFail, type Predicate } from './utils/array.js';
|
|
57
55
|
export { ProxyRegistry, unwrapProxy, type IProxyRegistry } from './utils/ProxyRegistry.js';
|
|
58
56
|
export { type Branded, type constructor, type Instance, Is } from './utils/basic.js';
|
package/typings/utils/array.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getConstructorChain: (ctor: unknown) => object[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const memoize: <T extends object, K, R>(fn: (target: T, key: K) => R) => ((target: T, key: K) => R);
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HookExecutionStrategy = void 0;
|
|
4
|
-
const target_1 = require("../metadata/target");
|
|
5
|
-
const hook_1 = require("./hook");
|
|
6
|
-
const HookContext_1 = require("./HookContext");
|
|
7
|
-
class HookExecutionStrategy {
|
|
8
|
-
key;
|
|
9
|
-
onError;
|
|
10
|
-
options;
|
|
11
|
-
hooksByClass = new WeakMap();
|
|
12
|
-
constructor({ key, onError, createExecutionContext = HookContext_1.createHookExecutionContext, mapExecutionContext = (context) => context, predicate = () => true, }) {
|
|
13
|
-
this.key = key;
|
|
14
|
-
this.onError = onError;
|
|
15
|
-
this.options = { createExecutionContext, mapExecutionContext, predicate };
|
|
16
|
-
}
|
|
17
|
-
hasHooks(target) {
|
|
18
|
-
return (0, hook_1.hasHooks)(target, this.key);
|
|
19
|
-
}
|
|
20
|
-
execute(target, { scope, ...overrides }) {
|
|
21
|
-
const report = (ex) => this.onError?.(scope)(ex);
|
|
22
|
-
try {
|
|
23
|
-
this.processHooks(this.collect(target, scope, overrides))?.catch(report);
|
|
24
|
-
}
|
|
25
|
-
catch (ex) {
|
|
26
|
-
report(ex);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
collect(target, scope, { createExecutionContext = this.options.createExecutionContext, mapExecutionContext = this.options.mapExecutionContext, predicate = this.options.predicate, }) {
|
|
30
|
-
const members = [];
|
|
31
|
-
for (const { methodName, hook } of this.hooksOf(target)) {
|
|
32
|
-
if (predicate(methodName)) {
|
|
33
|
-
members.push({ hook, context: mapExecutionContext(createExecutionContext(target, scope, methodName)) });
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return members;
|
|
37
|
-
}
|
|
38
|
-
hooksOf(target) {
|
|
39
|
-
const Target = (0, target_1.resolveConstructor)(target);
|
|
40
|
-
let hooks = this.hooksByClass.get(Target);
|
|
41
|
-
if (!hooks) {
|
|
42
|
-
hooks = Array.from((0, hook_1.getHooks)(Target, this.key), ([methodName, fn]) => ({ methodName, hook: (0, hook_1.toHookFn)(fn) }));
|
|
43
|
-
this.hooksByClass.set(Target, hooks);
|
|
44
|
-
}
|
|
45
|
-
return hooks;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
exports.HookExecutionStrategy = HookExecutionStrategy;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ParallelAsync = void 0;
|
|
4
|
-
const HookExecutionStrategy_1 = require("./HookExecutionStrategy");
|
|
5
|
-
const task_1 = require("../utils/task");
|
|
6
|
-
class ParallelAsync extends HookExecutionStrategy_1.HookExecutionStrategy {
|
|
7
|
-
processHooks(members) {
|
|
8
|
-
return (0, task_1.runAtOnce)(members.map(({ hook, context }) => () => hook(context)));
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
exports.ParallelAsync = ParallelAsync;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SequentialAsync = void 0;
|
|
4
|
-
const HookExecutionStrategy_1 = require("./HookExecutionStrategy");
|
|
5
|
-
const task_1 = require("../utils/task");
|
|
6
|
-
class SequentialAsync extends HookExecutionStrategy_1.HookExecutionStrategy {
|
|
7
|
-
processHooks(members) {
|
|
8
|
-
return (0, task_1.runInOrder)(members.map(({ hook, context }) => () => hook(context)));
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
exports.SequentialAsync = SequentialAsync;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SequentialSync = void 0;
|
|
4
|
-
const HookExecutionStrategy_1 = require("./HookExecutionStrategy");
|
|
5
|
-
class SequentialSync extends HookExecutionStrategy_1.HookExecutionStrategy {
|
|
6
|
-
processHooks(members) {
|
|
7
|
-
for (const { hook, context } of members) {
|
|
8
|
-
hook(context);
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
exports.SequentialSync = SequentialSync;
|
package/cjm/hooks/onConstruct.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.OnConstructModule = exports.onConstruct = void 0;
|
|
4
|
-
const hook_1 = require("./hook");
|
|
5
|
-
const onConstruct = (fn) => (0, hook_1.hook)('onConstruct', fn);
|
|
6
|
-
exports.onConstruct = onConstruct;
|
|
7
|
-
class OnConstructModule {
|
|
8
|
-
strategy;
|
|
9
|
-
constructor(strategy) {
|
|
10
|
-
this.strategy = strategy;
|
|
11
|
-
}
|
|
12
|
-
applyTo(container) {
|
|
13
|
-
container.getInjector().onConstructed((instance, scope) => {
|
|
14
|
-
this.strategy.execute(instance, { scope });
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
exports.OnConstructModule = OnConstructModule;
|
package/cjm/hooks/onResolved.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.resolved = exports.OnResolvedModule = exports.onResolved = void 0;
|
|
4
|
-
const IRegistration_1 = require("../registration/IRegistration");
|
|
5
|
-
const basic_1 = require("../utils/basic");
|
|
6
|
-
const hook_1 = require("./hook");
|
|
7
|
-
const onResolved = (fn) => (0, hook_1.hook)('onResolved', fn);
|
|
8
|
-
exports.onResolved = onResolved;
|
|
9
|
-
const runHooks = (strategy) => (dependency, scope) => {
|
|
10
|
-
if (basic_1.Is.object(dependency)) {
|
|
11
|
-
strategy.execute(dependency, { scope });
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
class OnResolvedModule {
|
|
15
|
-
runHooks;
|
|
16
|
-
constructor(strategy) {
|
|
17
|
-
this.runHooks = runHooks(strategy);
|
|
18
|
-
}
|
|
19
|
-
applyTo(container) {
|
|
20
|
-
container.registered.subscribe((provider) => {
|
|
21
|
-
provider.onResolved(this.runHooks);
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
exports.OnResolvedModule = OnResolvedModule;
|
|
26
|
-
const resolved = (strategy) => (0, IRegistration_1.registerPipe)((p) => p.onResolved(runHooks(strategy)));
|
|
27
|
-
exports.resolved = resolved;
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.OnDisposeModule = exports.onScopeDisposed = void 0;
|
|
4
|
-
const hook_1 = require("./hook");
|
|
5
|
-
const onScopeDisposed = (fn) => (0, hook_1.hook)('onScopeDisposed', fn);
|
|
6
|
-
exports.onScopeDisposed = onScopeDisposed;
|
|
7
|
-
class OnDisposeModule {
|
|
8
|
-
strategy;
|
|
9
|
-
constructor(strategy) {
|
|
10
|
-
this.strategy = strategy;
|
|
11
|
-
}
|
|
12
|
-
applyTo(container) {
|
|
13
|
-
container.scopeDisposed.subscribe((scope) => {
|
|
14
|
-
for (const instance of scope.getInstances()) {
|
|
15
|
-
this.strategy.execute(instance, { scope });
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
exports.OnDisposeModule = OnDisposeModule;
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { resolveConstructor } from '../metadata/target.js';
|
|
2
|
-
import { getHooks, hasHooks, toHookFn } from './hook.js';
|
|
3
|
-
import { createHookExecutionContext } from './HookContext.js';
|
|
4
|
-
export class HookExecutionStrategy {
|
|
5
|
-
key;
|
|
6
|
-
onError;
|
|
7
|
-
options;
|
|
8
|
-
hooksByClass = new WeakMap();
|
|
9
|
-
constructor({ key, onError, createExecutionContext = createHookExecutionContext, mapExecutionContext = (context) => context, predicate = () => true, }) {
|
|
10
|
-
this.key = key;
|
|
11
|
-
this.onError = onError;
|
|
12
|
-
this.options = { createExecutionContext, mapExecutionContext, predicate };
|
|
13
|
-
}
|
|
14
|
-
hasHooks(target) {
|
|
15
|
-
return hasHooks(target, this.key);
|
|
16
|
-
}
|
|
17
|
-
execute(target, { scope, ...overrides }) {
|
|
18
|
-
const report = (ex) => this.onError?.(scope)(ex);
|
|
19
|
-
try {
|
|
20
|
-
this.processHooks(this.collect(target, scope, overrides))?.catch(report);
|
|
21
|
-
}
|
|
22
|
-
catch (ex) {
|
|
23
|
-
report(ex);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
collect(target, scope, { createExecutionContext = this.options.createExecutionContext, mapExecutionContext = this.options.mapExecutionContext, predicate = this.options.predicate, }) {
|
|
27
|
-
const members = [];
|
|
28
|
-
for (const { methodName, hook } of this.hooksOf(target)) {
|
|
29
|
-
if (predicate(methodName)) {
|
|
30
|
-
members.push({ hook, context: mapExecutionContext(createExecutionContext(target, scope, methodName)) });
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return members;
|
|
34
|
-
}
|
|
35
|
-
hooksOf(target) {
|
|
36
|
-
const Target = resolveConstructor(target);
|
|
37
|
-
let hooks = this.hooksByClass.get(Target);
|
|
38
|
-
if (!hooks) {
|
|
39
|
-
hooks = Array.from(getHooks(Target, this.key), ([methodName, fn]) => ({ methodName, hook: toHookFn(fn) }));
|
|
40
|
-
this.hooksByClass.set(Target, hooks);
|
|
41
|
-
}
|
|
42
|
-
return hooks;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { HookExecutionStrategy } from './HookExecutionStrategy.js';
|
|
2
|
-
import { runAtOnce } from '../utils/task.js';
|
|
3
|
-
export class ParallelAsync extends HookExecutionStrategy {
|
|
4
|
-
processHooks(members) {
|
|
5
|
-
return runAtOnce(members.map(({ hook, context }) => () => hook(context)));
|
|
6
|
-
}
|
|
7
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { HookExecutionStrategy } from './HookExecutionStrategy.js';
|
|
2
|
-
import { runInOrder } from '../utils/task.js';
|
|
3
|
-
export class SequentialAsync extends HookExecutionStrategy {
|
|
4
|
-
processHooks(members) {
|
|
5
|
-
return runInOrder(members.map(({ hook, context }) => () => hook(context)));
|
|
6
|
-
}
|
|
7
|
-
}
|
package/esm/hooks/onConstruct.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { hook } from './hook.js';
|
|
2
|
-
export const onConstruct = (fn) => hook('onConstruct', fn);
|
|
3
|
-
export class OnConstructModule {
|
|
4
|
-
strategy;
|
|
5
|
-
constructor(strategy) {
|
|
6
|
-
this.strategy = strategy;
|
|
7
|
-
}
|
|
8
|
-
applyTo(container) {
|
|
9
|
-
container.getInjector().onConstructed((instance, scope) => {
|
|
10
|
-
this.strategy.execute(instance, { scope });
|
|
11
|
-
});
|
|
12
|
-
}
|
|
13
|
-
}
|
package/esm/hooks/onResolved.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { registerPipe } from '../registration/IRegistration.js';
|
|
2
|
-
import { Is } from '../utils/basic.js';
|
|
3
|
-
import { hook } from './hook.js';
|
|
4
|
-
export const onResolved = (fn) => hook('onResolved', fn);
|
|
5
|
-
const runHooks = (strategy) => (dependency, scope) => {
|
|
6
|
-
if (Is.object(dependency)) {
|
|
7
|
-
strategy.execute(dependency, { scope });
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
export class OnResolvedModule {
|
|
11
|
-
runHooks;
|
|
12
|
-
constructor(strategy) {
|
|
13
|
-
this.runHooks = runHooks(strategy);
|
|
14
|
-
}
|
|
15
|
-
applyTo(container) {
|
|
16
|
-
container.registered.subscribe((provider) => {
|
|
17
|
-
provider.onResolved(this.runHooks);
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
export const resolved = (strategy) => registerPipe((p) => p.onResolved(runHooks(strategy)));
|