ts-ioc-container 63.0.0 → 64.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.
- package/README.md +94 -90
- package/cjm/container/Container.js +3 -0
- package/cjm/container/EmptyContainer.js +3 -0
- package/cjm/hooks/AsyncHookExecutionStrategy.js +15 -0
- package/cjm/hooks/HookContext.js +4 -4
- package/cjm/hooks/HookExecutionStrategy.js +68 -0
- package/cjm/hooks/ParallelAsync.js +17 -0
- package/cjm/hooks/SequentialAsync.js +15 -0
- package/cjm/hooks/SequentialSync.js +14 -0
- package/cjm/hooks/onConstruct.js +7 -9
- package/cjm/hooks/onResolved.js +13 -9
- package/cjm/hooks/onScopeDisposed.js +5 -7
- package/cjm/hooks/resolveHooks.js +1 -18
- package/cjm/index.js +14 -11
- package/esm/container/Container.js +3 -0
- package/esm/container/EmptyContainer.js +3 -0
- package/esm/hooks/AsyncHookExecutionStrategy.js +11 -0
- package/esm/hooks/HookContext.js +2 -2
- package/esm/hooks/HookExecutionStrategy.js +62 -0
- package/esm/hooks/ParallelAsync.js +13 -0
- package/esm/hooks/SequentialAsync.js +11 -0
- package/esm/hooks/SequentialSync.js +10 -0
- package/esm/hooks/onConstruct.js +6 -8
- package/esm/hooks/onResolved.js +11 -8
- package/esm/hooks/onScopeDisposed.js +4 -6
- package/esm/hooks/resolveHooks.js +1 -14
- package/esm/index.js +10 -6
- package/package.json +1 -1
- package/typings/container/Container.d.ts +1 -0
- package/typings/container/EmptyContainer.d.ts +2 -0
- package/typings/container/IContainer.d.ts +2 -1
- package/typings/hooks/AsyncHookExecutionStrategy.d.ts +10 -0
- package/typings/hooks/HookContext.d.ts +3 -3
- package/typings/hooks/HookExecutionStrategy.d.ts +35 -0
- package/typings/hooks/ParallelAsync.d.ts +5 -0
- package/typings/hooks/SequentialAsync.d.ts +5 -0
- package/typings/hooks/SequentialSync.d.ts +4 -0
- package/typings/hooks/onConstruct.d.ts +7 -8
- package/typings/hooks/onResolved.d.ts +5 -5
- package/typings/hooks/onScopeDisposed.d.ts +4 -5
- package/typings/hooks/resolveHooks.d.ts +0 -6
- package/typings/index.d.ts +10 -6
- package/cjm/hooks/HooksRunner.js +0 -50
- package/esm/hooks/HooksRunner.js +0 -45
- package/typings/hooks/HooksRunner.d.ts +0 -19
package/esm/hooks/HooksRunner.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { createHookContext } from './HookContext.js';
|
|
2
|
-
import { getHooks, hasHooks, toHookFn } from './hook.js';
|
|
3
|
-
const runFrom = (fns, index, context) => {
|
|
4
|
-
for (let i = index; i < fns.length; i++) {
|
|
5
|
-
const result = fns[i](context);
|
|
6
|
-
if (result instanceof Promise) {
|
|
7
|
-
return result.then(() => runFrom(fns, i + 1, context));
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
export class HooksRunner {
|
|
12
|
-
key;
|
|
13
|
-
constructor(key) {
|
|
14
|
-
this.key = key;
|
|
15
|
-
}
|
|
16
|
-
hasHooks(target) {
|
|
17
|
-
return hasHooks(target, this.key);
|
|
18
|
-
}
|
|
19
|
-
execute(target, { scope, createContext = createHookContext, mapContext = (context) => context, predicate = () => true, }) {
|
|
20
|
-
const hooks = Array.from(getHooks(target, this.key).entries()).filter(([methodName]) => predicate(methodName));
|
|
21
|
-
const pending = [];
|
|
22
|
-
for (const [methodName, executions] of hooks) {
|
|
23
|
-
const context = mapContext(createContext(target, scope, methodName));
|
|
24
|
-
const result = runFrom(executions.map(toHookFn), 0, context);
|
|
25
|
-
if (result) {
|
|
26
|
-
pending.push(result);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return pending.length > 0 ? Promise.all(pending).then(() => undefined) : undefined;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
export const runHooks = (runner, target, scope, onException) => {
|
|
33
|
-
const report = (ex) => {
|
|
34
|
-
if (!onException) {
|
|
35
|
-
throw ex;
|
|
36
|
-
}
|
|
37
|
-
onException(ex, { scope });
|
|
38
|
-
};
|
|
39
|
-
try {
|
|
40
|
-
runner.execute(target, { scope })?.catch(report);
|
|
41
|
-
}
|
|
42
|
-
catch (ex) {
|
|
43
|
-
report(ex);
|
|
44
|
-
}
|
|
45
|
-
};
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { type CreateHookContext, type IHookContext } from './HookContext.js';
|
|
2
|
-
import type { IContainer } from '../container/IContainer.js';
|
|
3
|
-
import { type Instance } from '../utils/basic.js';
|
|
4
|
-
import type { ExecutionContext } from '../ExecutionContext.js';
|
|
5
|
-
export type MapHookContext = (context: IHookContext) => IHookContext;
|
|
6
|
-
export type OnExceptionHandler = (ex: unknown, context: ExecutionContext) => void;
|
|
7
|
-
export type HooksRunnerContext = {
|
|
8
|
-
scope: IContainer;
|
|
9
|
-
createContext?: CreateHookContext;
|
|
10
|
-
mapContext?: MapHookContext;
|
|
11
|
-
predicate?: (methodName: string) => boolean;
|
|
12
|
-
};
|
|
13
|
-
export declare class HooksRunner {
|
|
14
|
-
private readonly key;
|
|
15
|
-
constructor(key: string | symbol);
|
|
16
|
-
hasHooks(target: Instance): boolean;
|
|
17
|
-
execute(target: Instance, { scope, createContext, mapContext, predicate, }: HooksRunnerContext): void | Promise<void>;
|
|
18
|
-
}
|
|
19
|
-
export declare const runHooks: (runner: HooksRunner, target: Instance, scope: IContainer, onException?: OnExceptionHandler) => void;
|