ts-ioc-container 55.0.0 → 55.2.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 +68 -1
- package/cjm/ExecutionContext.js +2 -0
- package/cjm/hooks/HooksRunner.js +4 -4
- package/cjm/hooks/onConstruct.js +13 -1
- package/esm/ExecutionContext.js +1 -0
- package/esm/hooks/HooksRunner.js +4 -4
- package/esm/hooks/onConstruct.js +13 -1
- package/esm/index.js +1 -1
- package/package.json +1 -1
- package/typings/ExecutionContext.d.ts +4 -0
- package/typings/hooks/HooksRunner.d.ts +5 -7
- package/typings/hooks/onConstruct.d.ts +4 -0
- package/typings/index.d.ts +3 -2
package/README.md
CHANGED
|
@@ -2462,7 +2462,16 @@ Sometimes you need to invoke methods after construct or dispose of class. This i
|
|
|
2462
2462
|
|
|
2463
2463
|
```typescript
|
|
2464
2464
|
import 'reflect-metadata';
|
|
2465
|
-
import {
|
|
2465
|
+
import {
|
|
2466
|
+
AddOnConstructHookModule,
|
|
2467
|
+
Container,
|
|
2468
|
+
type ExecutionContext,
|
|
2469
|
+
type HookFn,
|
|
2470
|
+
type IContainer,
|
|
2471
|
+
inject,
|
|
2472
|
+
onConstruct,
|
|
2473
|
+
Registration as R,
|
|
2474
|
+
} from 'ts-ioc-container';
|
|
2466
2475
|
|
|
2467
2476
|
const execute: HookFn = (ctx) => {
|
|
2468
2477
|
ctx.invokeMethod({ args: ctx.resolveArgs() });
|
|
@@ -2490,6 +2499,64 @@ describe('onConstruct', function () {
|
|
|
2490
2499
|
expect(db.isConnected).toBe(true);
|
|
2491
2500
|
expect(db.connectionString).toBe('postgres://localhost:5432');
|
|
2492
2501
|
});
|
|
2502
|
+
|
|
2503
|
+
it('should forward hook exceptions to the onException handler with the execution context', function () {
|
|
2504
|
+
const failure = new Error('boom');
|
|
2505
|
+
|
|
2506
|
+
class BrokenService {
|
|
2507
|
+
@onConstruct(() => {
|
|
2508
|
+
throw failure;
|
|
2509
|
+
})
|
|
2510
|
+
init() {}
|
|
2511
|
+
}
|
|
2512
|
+
|
|
2513
|
+
let captured: { ex: unknown; context: ExecutionContext } | undefined;
|
|
2514
|
+
const container = new Container().useModule(
|
|
2515
|
+
new AddOnConstructHookModule((ex, context) => {
|
|
2516
|
+
captured = { ex, context };
|
|
2517
|
+
}),
|
|
2518
|
+
);
|
|
2519
|
+
|
|
2520
|
+
expect(() => container.resolve(BrokenService)).not.toThrow();
|
|
2521
|
+
expect(captured?.ex).toBe(failure);
|
|
2522
|
+
expect(captured?.context.scope).toBe(container);
|
|
2523
|
+
});
|
|
2524
|
+
|
|
2525
|
+
it('should rethrow hook exceptions when no onException handler is provided', function () {
|
|
2526
|
+
const failure = new Error('boom');
|
|
2527
|
+
|
|
2528
|
+
class BrokenService {
|
|
2529
|
+
@onConstruct(() => {
|
|
2530
|
+
throw failure;
|
|
2531
|
+
})
|
|
2532
|
+
init() {}
|
|
2533
|
+
}
|
|
2534
|
+
|
|
2535
|
+
const container = new Container().useModule(new AddOnConstructHookModule());
|
|
2536
|
+
|
|
2537
|
+
expect(() => container.resolve(BrokenService)).toThrow(failure);
|
|
2538
|
+
});
|
|
2539
|
+
|
|
2540
|
+
it('should expose the resolving scope through the execution context', function () {
|
|
2541
|
+
class BrokenService {
|
|
2542
|
+
@onConstruct(() => {
|
|
2543
|
+
throw new Error('boom');
|
|
2544
|
+
})
|
|
2545
|
+
init() {}
|
|
2546
|
+
}
|
|
2547
|
+
|
|
2548
|
+
let scope: IContainer | undefined;
|
|
2549
|
+
const container = new Container().useModule(
|
|
2550
|
+
new AddOnConstructHookModule((_ex, context) => {
|
|
2551
|
+
scope = context.scope;
|
|
2552
|
+
}),
|
|
2553
|
+
);
|
|
2554
|
+
const child = container.createScope();
|
|
2555
|
+
|
|
2556
|
+
child.resolve(BrokenService);
|
|
2557
|
+
|
|
2558
|
+
expect(scope).toBe(child);
|
|
2559
|
+
});
|
|
2493
2560
|
});
|
|
2494
2561
|
|
|
2495
2562
|
```
|
package/cjm/hooks/HooksRunner.js
CHANGED
|
@@ -10,10 +10,10 @@ class HooksRunner {
|
|
|
10
10
|
constructor(key) {
|
|
11
11
|
this.key = key;
|
|
12
12
|
}
|
|
13
|
-
execute(target, { scope, createContext = HookContext_1.createHookContext, predicate = () => true }) {
|
|
13
|
+
execute(target, { scope, createContext = HookContext_1.createHookContext, mapContext = (context) => context, predicate = () => true, }) {
|
|
14
14
|
const hooks = Array.from((0, hook_1.getHooks)(target, this.key).entries()).filter(([methodName]) => predicate(methodName));
|
|
15
15
|
const runMethodHooks = (methodName, executions) => {
|
|
16
|
-
const context = createContext(target, scope, methodName);
|
|
16
|
+
const context = mapContext(createContext(target, scope, methodName));
|
|
17
17
|
for (const execute of executions) {
|
|
18
18
|
const result = execute(context);
|
|
19
19
|
if (result instanceof Promise) {
|
|
@@ -25,10 +25,10 @@ class HooksRunner {
|
|
|
25
25
|
runMethodHooks(methodName, executions.map(hook_1.toHookFn));
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
|
-
async executeAsync(target, { scope, createContext = HookContext_1.createHookContext, predicate = () => true, }) {
|
|
28
|
+
async executeAsync(target, { scope, createContext = HookContext_1.createHookContext, mapContext = (context) => context, predicate = () => true, }) {
|
|
29
29
|
const hooks = Array.from((0, hook_1.getHooks)(target, this.key).entries()).filter(([methodName]) => predicate(methodName));
|
|
30
30
|
const runMethodHooks = async (methodName, executions) => {
|
|
31
|
-
const context = createContext(target, scope, methodName);
|
|
31
|
+
const context = mapContext(createContext(target, scope, methodName));
|
|
32
32
|
for (const execute of executions) {
|
|
33
33
|
await (0, promise_1.promisify)(execute(context));
|
|
34
34
|
}
|
package/cjm/hooks/onConstruct.js
CHANGED
|
@@ -7,9 +7,21 @@ exports.onConstructHooksRunner = new HooksRunner_1.HooksRunner('onConstruct');
|
|
|
7
7
|
const onConstruct = (...fns) => (0, hook_1.hook)('onConstruct', ...fns);
|
|
8
8
|
exports.onConstruct = onConstruct;
|
|
9
9
|
class AddOnConstructHookModule {
|
|
10
|
+
onException;
|
|
11
|
+
constructor(onException) {
|
|
12
|
+
this.onException = onException;
|
|
13
|
+
}
|
|
10
14
|
applyTo(container) {
|
|
11
15
|
container.addOnConstructHook((instance, scope) => {
|
|
12
|
-
|
|
16
|
+
try {
|
|
17
|
+
exports.onConstructHooksRunner.execute(instance, { scope });
|
|
18
|
+
}
|
|
19
|
+
catch (ex) {
|
|
20
|
+
if (!this.onException) {
|
|
21
|
+
throw ex;
|
|
22
|
+
}
|
|
23
|
+
this.onException(ex, { scope });
|
|
24
|
+
}
|
|
13
25
|
});
|
|
14
26
|
}
|
|
15
27
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/esm/hooks/HooksRunner.js
CHANGED
|
@@ -7,10 +7,10 @@ export class HooksRunner {
|
|
|
7
7
|
constructor(key) {
|
|
8
8
|
this.key = key;
|
|
9
9
|
}
|
|
10
|
-
execute(target, { scope, createContext = createHookContext, predicate = () => true }) {
|
|
10
|
+
execute(target, { scope, createContext = createHookContext, mapContext = (context) => context, predicate = () => true, }) {
|
|
11
11
|
const hooks = Array.from(getHooks(target, this.key).entries()).filter(([methodName]) => predicate(methodName));
|
|
12
12
|
const runMethodHooks = (methodName, executions) => {
|
|
13
|
-
const context = createContext(target, scope, methodName);
|
|
13
|
+
const context = mapContext(createContext(target, scope, methodName));
|
|
14
14
|
for (const execute of executions) {
|
|
15
15
|
const result = execute(context);
|
|
16
16
|
if (result instanceof Promise) {
|
|
@@ -22,10 +22,10 @@ export class HooksRunner {
|
|
|
22
22
|
runMethodHooks(methodName, executions.map(toHookFn));
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
-
async executeAsync(target, { scope, createContext = createHookContext, predicate = () => true, }) {
|
|
25
|
+
async executeAsync(target, { scope, createContext = createHookContext, mapContext = (context) => context, predicate = () => true, }) {
|
|
26
26
|
const hooks = Array.from(getHooks(target, this.key).entries()).filter(([methodName]) => predicate(methodName));
|
|
27
27
|
const runMethodHooks = async (methodName, executions) => {
|
|
28
|
-
const context = createContext(target, scope, methodName);
|
|
28
|
+
const context = mapContext(createContext(target, scope, methodName));
|
|
29
29
|
for (const execute of executions) {
|
|
30
30
|
await promisify(execute(context));
|
|
31
31
|
}
|
package/esm/hooks/onConstruct.js
CHANGED
|
@@ -3,9 +3,21 @@ import { HooksRunner } from './HooksRunner';
|
|
|
3
3
|
export const onConstructHooksRunner = new HooksRunner('onConstruct');
|
|
4
4
|
export const onConstruct = (...fns) => hook('onConstruct', ...fns);
|
|
5
5
|
export class AddOnConstructHookModule {
|
|
6
|
+
onException;
|
|
7
|
+
constructor(onException) {
|
|
8
|
+
this.onException = onException;
|
|
9
|
+
}
|
|
6
10
|
applyTo(container) {
|
|
7
11
|
container.addOnConstructHook((instance, scope) => {
|
|
8
|
-
|
|
12
|
+
try {
|
|
13
|
+
onConstructHooksRunner.execute(instance, { scope });
|
|
14
|
+
}
|
|
15
|
+
catch (ex) {
|
|
16
|
+
if (!this.onException) {
|
|
17
|
+
throw ex;
|
|
18
|
+
}
|
|
19
|
+
this.onException(ex, { scope });
|
|
20
|
+
}
|
|
9
21
|
});
|
|
10
22
|
}
|
|
11
23
|
}
|
package/esm/index.js
CHANGED
|
@@ -24,7 +24,7 @@ export { CannonSingletonApplyTwiceError } from './errors/CannonSingletonApplyTwi
|
|
|
24
24
|
export { getHooks, hook, hasHooks } from './hooks/hook';
|
|
25
25
|
export { HookContext, createHookContextFactory, createHookContext } from './hooks/HookContext';
|
|
26
26
|
export { injectProp } from './hooks/injectProp';
|
|
27
|
-
export { onConstructHooksRunner, onConstruct, AddOnConstructHookModule } from './hooks/onConstruct';
|
|
27
|
+
export { onConstructHooksRunner, onConstruct, AddOnConstructHookModule, } from './hooks/onConstruct';
|
|
28
28
|
export { onDisposeHooksRunner, onDispose, AddOnDisposeHookModule } from './hooks/onDispose';
|
|
29
29
|
export { HooksRunner } from './hooks/HooksRunner';
|
|
30
30
|
// Tokens
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-ioc-container",
|
|
3
|
-
"version": "55.
|
|
3
|
+
"version": "55.2.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
|
"workspaces": [
|
|
6
6
|
"docs"
|
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type CreateHookContext, type IHookContext } from './HookContext';
|
|
2
2
|
import type { IContainer } from '../container/IContainer';
|
|
3
|
+
export type MapHookContext = (context: IHookContext) => IHookContext;
|
|
3
4
|
export type HooksRunnerContext = {
|
|
4
5
|
scope: IContainer;
|
|
5
6
|
createContext?: CreateHookContext;
|
|
7
|
+
mapContext?: MapHookContext;
|
|
6
8
|
predicate?: (methodName: string) => boolean;
|
|
7
9
|
};
|
|
8
10
|
export declare class HooksRunner {
|
|
9
11
|
private readonly key;
|
|
10
12
|
constructor(key: string | symbol);
|
|
11
|
-
execute(target: object, { scope, createContext, predicate }: HooksRunnerContext): void;
|
|
12
|
-
executeAsync(target: object, { scope, createContext, predicate, }:
|
|
13
|
-
scope: IContainer;
|
|
14
|
-
createContext?: typeof createHookContext;
|
|
15
|
-
predicate?: (methodName: string) => boolean;
|
|
16
|
-
}): Promise<void[]>;
|
|
13
|
+
execute(target: object, { scope, createContext, mapContext, predicate, }: HooksRunnerContext): void;
|
|
14
|
+
executeAsync(target: object, { scope, createContext, mapContext, predicate, }: HooksRunnerContext): Promise<void[]>;
|
|
17
15
|
}
|
|
@@ -2,9 +2,13 @@ import { HookClass, HookFn } from './hook';
|
|
|
2
2
|
import type { IContainer, IContainerModule } from '../container/IContainer';
|
|
3
3
|
import { constructor, Instance } from '../utils/basic';
|
|
4
4
|
import { HooksRunner } from './HooksRunner';
|
|
5
|
+
import type { ExecutionContext } from '../ExecutionContext';
|
|
5
6
|
export declare const onConstructHooksRunner: HooksRunner;
|
|
6
7
|
export declare const onConstruct: (...fns: (HookFn | constructor<HookClass>)[]) => (target: object, propertyKey: string | symbol) => void;
|
|
7
8
|
export type OnConstructHook = (instance: Instance, scope: IContainer) => void;
|
|
9
|
+
export type OnExceptionHandler = (ex: unknown, context: ExecutionContext) => void;
|
|
8
10
|
export declare class AddOnConstructHookModule implements IContainerModule {
|
|
11
|
+
private readonly onException?;
|
|
12
|
+
constructor(onException?: OnExceptionHandler | undefined);
|
|
9
13
|
applyTo(container: IContainer): void;
|
|
10
14
|
}
|
package/typings/index.d.ts
CHANGED
|
@@ -20,9 +20,9 @@ export { CannonSingletonApplyTwiceError } from './errors/CannonSingletonApplyTwi
|
|
|
20
20
|
export { getHooks, hook, hasHooks, type HookFn, type HookClass, type InjectFn, type HooksOfClass } from './hooks/hook';
|
|
21
21
|
export { HookContext, createHookContextFactory, createHookContext, type IHookContext } from './hooks/HookContext';
|
|
22
22
|
export { injectProp } from './hooks/injectProp';
|
|
23
|
-
export { onConstructHooksRunner, onConstruct, AddOnConstructHookModule } from './hooks/onConstruct';
|
|
23
|
+
export { onConstructHooksRunner, onConstruct, AddOnConstructHookModule, type OnExceptionHandler, } from './hooks/onConstruct';
|
|
24
24
|
export { onDisposeHooksRunner, onDispose, AddOnDisposeHookModule } from './hooks/onDispose';
|
|
25
|
-
export { HooksRunner, type HooksRunnerContext } from './hooks/HooksRunner';
|
|
25
|
+
export { HooksRunner, type HooksRunnerContext, type MapHookContext } from './hooks/HooksRunner';
|
|
26
26
|
export { InjectionToken } from './token/InjectionToken';
|
|
27
27
|
export { GroupAliasToken, toGroupAlias } from './token/GroupAliasToken';
|
|
28
28
|
export { SingleAliasToken, toSingleAlias } from './token/SingleAliasToken';
|
|
@@ -39,6 +39,7 @@ export { throttle } from './utils/throttle';
|
|
|
39
39
|
export { debounce } from './utils/debounce';
|
|
40
40
|
export { shallowCache } from './utils/shallowCache';
|
|
41
41
|
export { once } from './utils/once';
|
|
42
|
+
export { type ExecutionContext } from './ExecutionContext';
|
|
42
43
|
export { select } from './select';
|
|
43
44
|
export { pipe, type MapFn } from './utils/fp';
|
|
44
45
|
export { type constructor, type Instance, Is, resolveConstructor } from './utils/basic';
|