ts-ioc-container 33.1.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 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)
@@ -19,19 +22,30 @@ function hasHooks(target, key) {
19
22
  return Reflect.hasMetadata(key, target.constructor);
20
23
  }
21
24
  exports.hasHooks = hasHooks;
22
- const runHooks = (target, key, { scope, createContext = HookContext_1.createHookContext, }) => {
23
- const hooks = Array.from(getHooks(target, key).entries());
25
+ const runHooks = (target, key, { scope, createContext = HookContext_1.createHookContext, predicate = () => true, }) => {
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
- execute(createContext(target, scope, methodName));
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
- const runHooksAsync = (target, key, { scope, createContext = HookContext_1.createHookContext, }) => {
32
- const hooks = Array.from(getHooks(target, key).entries());
33
- const runExecution = (execute, context) => (0, utils_1.promisify)(execute(context));
34
- return Promise.all(hooks.flatMap(([methodName, executions]) => executions.map((execute) => runExecution(execute, createContext(target, scope, methodName)))));
40
+ const runHooksAsync = (target, key, { scope, createContext = HookContext_1.createHookContext, predicate = () => true, }) => {
41
+ const hooks = Array.from(getHooks(target, key).entries()).filter(([methodName]) => predicate(methodName));
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));
45
+ return Promise.all(hooks.flatMap(([methodName, executions]) => {
46
+ const context = createContext(target, scope, methodName);
47
+ return executions.map((execute) => runExecution(execute, context));
48
+ }));
35
49
  };
36
50
  exports.runHooksAsync = runHooksAsync;
37
51
  const injectProp = (fn) => (context) => context.setProperty(fn);
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)
@@ -13,17 +16,28 @@ export function getHooks(target, key) {
13
16
  export function hasHooks(target, key) {
14
17
  return Reflect.hasMetadata(key, target.constructor);
15
18
  }
16
- export const runHooks = (target, key, { scope, createContext = createHookContext, }) => {
17
- const hooks = Array.from(getHooks(target, key).entries());
19
+ export const runHooks = (target, key, { scope, createContext = createHookContext, predicate = () => true, }) => {
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
- execute(createContext(target, scope, methodName));
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
- export const runHooksAsync = (target, key, { scope, createContext = createHookContext, }) => {
25
- const hooks = Array.from(getHooks(target, key).entries());
26
- const runExecution = (execute, context) => promisify(execute(context));
27
- return Promise.all(hooks.flatMap(([methodName, executions]) => executions.map((execute) => runExecution(execute, createContext(target, scope, methodName)))));
33
+ export const runHooksAsync = (target, key, { scope, createContext = createHookContext, predicate = () => true, }) => {
34
+ const hooks = Array.from(getHooks(target, key).entries()).filter(([methodName]) => predicate(methodName));
35
+ const runExecution = (execute, context) => isHookClassConstructor(execute)
36
+ ? promisify(context.scope.resolve(execute).execute(context))
37
+ : promisify(execute(context));
38
+ return Promise.all(hooks.flatMap(([methodName, executions]) => {
39
+ const context = createContext(target, scope, methodName);
40
+ return executions.map((execute) => runExecution(execute, context));
41
+ }));
28
42
  };
29
43
  export const injectProp = (fn) => (context) => context.setProperty(fn);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-ioc-container",
3
- "version": "33.1.0",
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": "284fe762ac07beb7f3747dfb5faf3409055d75df"
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 declare const createHookContext: (Target: object, scope: IContainer, methodName?: string) => IHookContext;
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;
@@ -1,17 +1,23 @@
1
1
  import { IContainer } from '../container/IContainer';
2
- import { createHookContext, IHookContext, InjectFn } from './HookContext';
3
- export type Hook<T extends IHookContext = IHookContext> = (context: T) => void | Promise<void>;
4
- type HooksOfClass = Map<string, Hook[]>;
5
- export declare const hook: (key: string | symbol, ...fns: Hook[]) => (target: object, propertyKey: string | symbol) => void;
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
- export declare const runHooks: (target: object, key: string | symbol, { scope, createContext, }: {
12
+ export declare const runHooks: (target: object, key: string | symbol, { scope, createContext, predicate, }: {
9
13
  scope: IContainer;
10
- createContext?: ((Target: object, scope: IContainer, methodName?: string) => IHookContext) | undefined;
14
+ createContext?: CreateHookContext | undefined;
15
+ predicate?: ((methodName: string) => boolean) | undefined;
11
16
  }) => void;
12
- export declare const runHooksAsync: (target: object, key: string | symbol, { scope, createContext, }: {
17
+ export declare const runHooksAsync: (target: object, key: string | symbol, { scope, createContext, predicate, }: {
13
18
  scope: IContainer;
14
- createContext?: ((Target: object, scope: IContainer, methodName?: string) => IHookContext) | undefined;
19
+ createContext?: CreateHookContext | undefined;
20
+ predicate?: ((methodName: string) => boolean) | undefined;
15
21
  }) => Promise<void[]>;
16
- export declare const injectProp: (fn: InjectFn) => Hook;
22
+ export declare const injectProp: (fn: InjectFn) => HookFn;
17
23
  export {};
@@ -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, Hook, runHooks, runHooksAsync, injectProp } from './hooks/hook';
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';