ts-ioc-container 55.1.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/onConstruct.js +13 -1
- package/esm/ExecutionContext.js +1 -0
- 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/onConstruct.d.ts +4 -0
- package/typings/index.d.ts +2 -1
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/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/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"
|
|
@@ -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,7 +20,7 @@ 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
25
|
export { HooksRunner, type HooksRunnerContext, type MapHookContext } from './hooks/HooksRunner';
|
|
26
26
|
export { InjectionToken } from './token/InjectionToken';
|
|
@@ -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';
|