ts-ioc-container 33.2.0 → 33.3.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/cjm/hooks/hook.js +13 -2
- package/esm/hooks/hook.js +14 -3
- package/package.json +2 -2
- package/typings/hooks/HookContext.d.ts +2 -1
- package/typings/hooks/hook.d.ts +11 -7
- package/typings/index.d.ts +1 -1
package/cjm/hooks/hook.js
CHANGED
|
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.injectProp = exports.runHooksAsync = exports.runHooks = exports.hasHooks = exports.getHooks = exports.hook = void 0;
|
|
4
4
|
const HookContext_1 = require("./HookContext");
|
|
5
5
|
const utils_1 = require("../utils");
|
|
6
|
+
const isHookClassConstructor = (execute) => {
|
|
7
|
+
return (0, utils_1.isConstructor)(execute) && execute.prototype.execute;
|
|
8
|
+
};
|
|
6
9
|
const hook = (key, ...fns) => (target, propertyKey) => {
|
|
7
10
|
const hooks = Reflect.hasMetadata(key, target.constructor)
|
|
8
11
|
? Reflect.getMetadata(key, target.constructor)
|
|
@@ -23,14 +26,22 @@ const runHooks = (target, key, { scope, createContext = HookContext_1.createHook
|
|
|
23
26
|
const hooks = Array.from(getHooks(target, key).entries()).filter(([methodName]) => predicate(methodName));
|
|
24
27
|
for (const [methodName, executions] of hooks) {
|
|
25
28
|
for (const execute of executions) {
|
|
26
|
-
|
|
29
|
+
const context = createContext(target, scope, methodName);
|
|
30
|
+
if (isHookClassConstructor(execute)) {
|
|
31
|
+
scope.resolve(execute).execute(context);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
execute(context);
|
|
35
|
+
}
|
|
27
36
|
}
|
|
28
37
|
}
|
|
29
38
|
};
|
|
30
39
|
exports.runHooks = runHooks;
|
|
31
40
|
const runHooksAsync = (target, key, { scope, createContext = HookContext_1.createHookContext, predicate = () => true, }) => {
|
|
32
41
|
const hooks = Array.from(getHooks(target, key).entries()).filter(([methodName]) => predicate(methodName));
|
|
33
|
-
const runExecution = (execute, context) => (
|
|
42
|
+
const runExecution = (execute, context) => isHookClassConstructor(execute)
|
|
43
|
+
? (0, utils_1.promisify)(context.scope.resolve(execute).execute(context))
|
|
44
|
+
: (0, utils_1.promisify)(execute(context));
|
|
34
45
|
return Promise.all(hooks.flatMap(([methodName, executions]) => {
|
|
35
46
|
const context = createContext(target, scope, methodName);
|
|
36
47
|
return executions.map((execute) => runExecution(execute, context));
|
package/esm/hooks/hook.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { createHookContext } from './HookContext';
|
|
2
|
-
import { promisify } from '../utils';
|
|
2
|
+
import { isConstructor, promisify } from '../utils';
|
|
3
|
+
const isHookClassConstructor = (execute) => {
|
|
4
|
+
return isConstructor(execute) && execute.prototype.execute;
|
|
5
|
+
};
|
|
3
6
|
export const hook = (key, ...fns) => (target, propertyKey) => {
|
|
4
7
|
const hooks = Reflect.hasMetadata(key, target.constructor)
|
|
5
8
|
? Reflect.getMetadata(key, target.constructor)
|
|
@@ -17,13 +20,21 @@ export const runHooks = (target, key, { scope, createContext = createHookContext
|
|
|
17
20
|
const hooks = Array.from(getHooks(target, key).entries()).filter(([methodName]) => predicate(methodName));
|
|
18
21
|
for (const [methodName, executions] of hooks) {
|
|
19
22
|
for (const execute of executions) {
|
|
20
|
-
|
|
23
|
+
const context = createContext(target, scope, methodName);
|
|
24
|
+
if (isHookClassConstructor(execute)) {
|
|
25
|
+
scope.resolve(execute).execute(context);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
execute(context);
|
|
29
|
+
}
|
|
21
30
|
}
|
|
22
31
|
}
|
|
23
32
|
};
|
|
24
33
|
export const runHooksAsync = (target, key, { scope, createContext = createHookContext, predicate = () => true, }) => {
|
|
25
34
|
const hooks = Array.from(getHooks(target, key).entries()).filter(([methodName]) => predicate(methodName));
|
|
26
|
-
const runExecution = (execute, context) =>
|
|
35
|
+
const runExecution = (execute, context) => isHookClassConstructor(execute)
|
|
36
|
+
? promisify(context.scope.resolve(execute).execute(context))
|
|
37
|
+
: promisify(execute(context));
|
|
27
38
|
return Promise.all(hooks.flatMap(([methodName, executions]) => {
|
|
28
39
|
const context = createContext(target, scope, methodName);
|
|
29
40
|
return executions.map((execute) => runExecution(execute, context));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-ioc-container",
|
|
3
|
-
"version": "33.
|
|
3
|
+
"version": "33.3.0",
|
|
4
4
|
"description": "Typescript IoC container",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public",
|
|
@@ -59,5 +59,5 @@
|
|
|
59
59
|
"ts-node": "^10.9.1",
|
|
60
60
|
"typescript": "5.4.3"
|
|
61
61
|
},
|
|
62
|
-
"gitHead": "
|
|
62
|
+
"gitHead": "5313997379db49d84f62a6acee5b7ae42c51b32f"
|
|
63
63
|
}
|
|
@@ -23,5 +23,6 @@ export declare class HookContext implements IHookContext {
|
|
|
23
23
|
setProperty(fn: InjectFn): void;
|
|
24
24
|
getProperty(): unknown;
|
|
25
25
|
}
|
|
26
|
-
export
|
|
26
|
+
export type CreateHookContext = (Target: object, scope: IContainer, methodName?: string) => IHookContext;
|
|
27
|
+
export declare const createHookContext: CreateHookContext;
|
|
27
28
|
export declare const hookMetaKey: (methodName?: string) => string;
|
package/typings/hooks/hook.d.ts
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
import { IContainer } from '../container/IContainer';
|
|
2
|
-
import { createHookContext, IHookContext, InjectFn } from './HookContext';
|
|
3
|
-
|
|
4
|
-
type
|
|
5
|
-
export
|
|
2
|
+
import { CreateHookContext, createHookContext, IHookContext, InjectFn } from './HookContext';
|
|
3
|
+
import { constructor } from '../utils';
|
|
4
|
+
export type HookFn<T extends IHookContext = IHookContext> = (context: T) => void | Promise<void>;
|
|
5
|
+
export interface HookClass<T extends IHookContext = IHookContext> {
|
|
6
|
+
execute(context: Omit<T, 'scope'>): void | Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
type HooksOfClass = Map<string, (HookFn | constructor<HookClass>)[]>;
|
|
9
|
+
export declare const hook: (key: string | symbol, ...fns: (HookFn | constructor<HookClass>)[]) => (target: object, propertyKey: string | symbol) => void;
|
|
6
10
|
export declare function getHooks(target: object, key: string | symbol): HooksOfClass;
|
|
7
11
|
export declare function hasHooks(target: object, key: string | symbol): boolean;
|
|
8
12
|
export declare const runHooks: (target: object, key: string | symbol, { scope, createContext, predicate, }: {
|
|
9
13
|
scope: IContainer;
|
|
10
|
-
createContext?:
|
|
14
|
+
createContext?: CreateHookContext | undefined;
|
|
11
15
|
predicate?: ((methodName: string) => boolean) | undefined;
|
|
12
16
|
}) => void;
|
|
13
17
|
export declare const runHooksAsync: (target: object, key: string | symbol, { scope, createContext, predicate, }: {
|
|
14
18
|
scope: IContainer;
|
|
15
|
-
createContext?:
|
|
19
|
+
createContext?: CreateHookContext | undefined;
|
|
16
20
|
predicate?: ((methodName: string) => boolean) | undefined;
|
|
17
21
|
}) => Promise<void[]>;
|
|
18
|
-
export declare const injectProp: (fn: InjectFn) =>
|
|
22
|
+
export declare const injectProp: (fn: InjectFn) => HookFn;
|
|
19
23
|
export {};
|
package/typings/index.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ export { Registration } from './registration/Registration';
|
|
|
18
18
|
export { DependencyNotFoundError } from './errors/DependencyNotFoundError';
|
|
19
19
|
export { MethodNotImplementedError } from './errors/MethodNotImplementedError';
|
|
20
20
|
export { ContainerDisposedError } from './errors/ContainerDisposedError';
|
|
21
|
-
export { getHooks, hook, hasHooks,
|
|
21
|
+
export { getHooks, hook, hasHooks, HookFn, HookClass, runHooks, runHooksAsync, injectProp } from './hooks/hook';
|
|
22
22
|
export { HookContext, InjectFn, IHookContext } from './hooks/HookContext';
|
|
23
23
|
export { by, InstancePredicate, IMemo, IMemoKey, byAlias, byAliases, depKey } from './by';
|
|
24
24
|
export { constructor } from './utils';
|