ts-ioc-container 32.11.0 → 32.12.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 +7 -7
- package/cjm/hooks/hook.js +13 -9
- package/cjm/index.js +2 -2
- package/esm/hooks/hook.js +10 -7
- package/esm/index.js +1 -1
- package/package.json +2 -2
- package/typings/hooks/hook.d.ts +5 -11
- package/typings/index.d.ts +2 -2
package/README.md
CHANGED
|
@@ -1161,7 +1161,7 @@ class MyInjector implements IInjector {
|
|
|
1161
1161
|
|
|
1162
1162
|
resolve<T>(container: IContainer, value: constructor<T>, options: InjectOptions): T {
|
|
1163
1163
|
const instance = this.injector.resolve(container, value, options);
|
|
1164
|
-
|
|
1164
|
+
runHooks(instance as object, 'onConstruct', { scope: container });
|
|
1165
1165
|
return instance;
|
|
1166
1166
|
}
|
|
1167
1167
|
}
|
|
@@ -1255,7 +1255,7 @@ describe('onDispose', function () {
|
|
|
1255
1255
|
logger.log('Hello');
|
|
1256
1256
|
|
|
1257
1257
|
for (const instance of container.getInstances()) {
|
|
1258
|
-
|
|
1258
|
+
runHooks(instance as object, 'onDispose', { scope: container });
|
|
1259
1259
|
}
|
|
1260
1260
|
|
|
1261
1261
|
expect(container.resolve<LogsRepo>('logsRepo').savedLogs.join(',')).toBe('Hello,world');
|
|
@@ -1267,7 +1267,7 @@ describe('onDispose', function () {
|
|
|
1267
1267
|
### Inject property
|
|
1268
1268
|
|
|
1269
1269
|
```typescript
|
|
1270
|
-
import { by, Container, hook, injectProp, MetadataInjector, Registration,
|
|
1270
|
+
import { by, Container, hook, injectProp, MetadataInjector, Registration, runHooksAsync } from 'ts-ioc-container';
|
|
1271
1271
|
|
|
1272
1272
|
describe('inject property', () => {
|
|
1273
1273
|
it('should inject property', () => {
|
|
@@ -1279,7 +1279,7 @@ describe('inject property', () => {
|
|
|
1279
1279
|
|
|
1280
1280
|
const container = new Container(new MetadataInjector()).add(Registration.fromValue(expected).to('greeting'));
|
|
1281
1281
|
const app = container.resolve(App);
|
|
1282
|
-
|
|
1282
|
+
runHooksAsync(app as object, 'onInit', { scope: container });
|
|
1283
1283
|
|
|
1284
1284
|
expect(app.greeting).toBe(expected);
|
|
1285
1285
|
});
|
|
@@ -1290,7 +1290,7 @@ describe('inject property', () => {
|
|
|
1290
1290
|
### Inject method
|
|
1291
1291
|
|
|
1292
1292
|
```typescript
|
|
1293
|
-
import { by, Container,
|
|
1293
|
+
import { by, Container, hook, inject, invokeExecution, MetadataInjector, Registration, runHooksAsync } from 'ts-ioc-container';
|
|
1294
1294
|
|
|
1295
1295
|
describe('inject method', () => {
|
|
1296
1296
|
const sleep = (number: number) => new Promise((resolve) => setTimeout(resolve, number));
|
|
@@ -1308,7 +1308,7 @@ describe('inject method', () => {
|
|
|
1308
1308
|
|
|
1309
1309
|
const container = new Container(new MetadataInjector()).add(Registration.fromValue(expected).to('greeting'));
|
|
1310
1310
|
const app = container.resolve(App);
|
|
1311
|
-
await
|
|
1311
|
+
await runHooksAsync(app, 'onInit', { scope: container });
|
|
1312
1312
|
|
|
1313
1313
|
expect(app.greeting).toBe(expected);
|
|
1314
1314
|
});
|
|
@@ -1328,7 +1328,7 @@ describe('inject method', () => {
|
|
|
1328
1328
|
.add(Registration.fromFn(() => sleep(25).then(() => 'world')).to('person'));
|
|
1329
1329
|
|
|
1330
1330
|
const app = container.resolve(App);
|
|
1331
|
-
await
|
|
1331
|
+
await runHooksAsync(app, 'onInit', { scope: container });
|
|
1332
1332
|
|
|
1333
1333
|
expect(app.greeting).toBe('Hello,world');
|
|
1334
1334
|
});
|
package/cjm/hooks/hook.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.invokeExecution = 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
6
|
const hook = (key, ...fns) => (target, propertyKey) => {
|
|
@@ -19,12 +19,21 @@ function hasHooks(target, key) {
|
|
|
19
19
|
return Reflect.hasMetadata(key, target.constructor);
|
|
20
20
|
}
|
|
21
21
|
exports.hasHooks = hasHooks;
|
|
22
|
-
const runHooks = (target, key, { scope, createContext = HookContext_1.createHookContext,
|
|
22
|
+
const runHooks = (target, key, { scope, createContext = HookContext_1.createHookContext, }) => {
|
|
23
23
|
const hooks = Array.from(getHooks(target, key).entries());
|
|
24
|
-
const
|
|
25
|
-
|
|
24
|
+
for (const [methodName, executions] of hooks) {
|
|
25
|
+
for (const execute of executions) {
|
|
26
|
+
execute(createContext(target, scope, methodName));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
26
29
|
};
|
|
27
30
|
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)))));
|
|
35
|
+
};
|
|
36
|
+
exports.runHooksAsync = runHooksAsync;
|
|
28
37
|
const injectProp = (fn) => (context) => context.injectProperty(fn);
|
|
29
38
|
exports.injectProp = injectProp;
|
|
30
39
|
const invokeExecution = ({ handleResult }) => async (context) => {
|
|
@@ -32,8 +41,3 @@ const invokeExecution = ({ handleResult }) => async (context) => {
|
|
|
32
41
|
return handleResult(context.invokeMethod({ args }), context);
|
|
33
42
|
};
|
|
34
43
|
exports.invokeExecution = invokeExecution;
|
|
35
|
-
/**
|
|
36
|
-
* @deprecated Use `runHooks` instead
|
|
37
|
-
* @TODO: Remove in v33
|
|
38
|
-
*/
|
|
39
|
-
exports.executeHooks = exports.runHooks;
|
package/cjm/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getParameterMetadata = exports.getMethodMetadata = exports.setMethodMetadata = exports.setParameterMetadata = exports.getMetadata = exports.setMetadata = exports.byAliases = exports.byAlias = exports.IMemoKey = exports.by = exports.HookContext = exports.invokeExecution = exports.injectProp = exports.
|
|
3
|
+
exports.getParameterMetadata = exports.getMethodMetadata = exports.setMethodMetadata = exports.setParameterMetadata = exports.getMetadata = exports.setMetadata = exports.byAliases = exports.byAlias = exports.IMemoKey = exports.by = exports.HookContext = exports.invokeExecution = exports.injectProp = exports.runHooksAsync = exports.runHooks = exports.hasHooks = exports.hook = exports.getHooks = exports.ContainerDisposedError = exports.MethodNotImplementedError = exports.DependencyNotFoundError = exports.Registration = exports.register = exports.scope = exports.key = exports.decorate = exports.multiCache = exports.MultiCache = exports.SingletonProvider = exports.singleton = exports.Provider = exports.ProviderDecorator = exports.args = exports.argsFn = exports.alias = exports.visible = exports.provider = exports.ProxyInjector = exports.SimpleInjector = exports.MetadataInjector = exports.resolveArgs = exports.inject = exports.AutoMockedContainer = exports.EmptyContainer = exports.Container = exports.isDependencyKey = void 0;
|
|
4
4
|
// Containers
|
|
5
5
|
var IContainer_1 = require("./container/IContainer");
|
|
6
6
|
Object.defineProperty(exports, "isDependencyKey", { enumerable: true, get: function () { return IContainer_1.isDependencyKey; } });
|
|
@@ -57,8 +57,8 @@ var hook_1 = require("./hooks/hook");
|
|
|
57
57
|
Object.defineProperty(exports, "getHooks", { enumerable: true, get: function () { return hook_1.getHooks; } });
|
|
58
58
|
Object.defineProperty(exports, "hook", { enumerable: true, get: function () { return hook_1.hook; } });
|
|
59
59
|
Object.defineProperty(exports, "hasHooks", { enumerable: true, get: function () { return hook_1.hasHooks; } });
|
|
60
|
-
Object.defineProperty(exports, "executeHooks", { enumerable: true, get: function () { return hook_1.executeHooks; } });
|
|
61
60
|
Object.defineProperty(exports, "runHooks", { enumerable: true, get: function () { return hook_1.runHooks; } });
|
|
61
|
+
Object.defineProperty(exports, "runHooksAsync", { enumerable: true, get: function () { return hook_1.runHooksAsync; } });
|
|
62
62
|
Object.defineProperty(exports, "injectProp", { enumerable: true, get: function () { return hook_1.injectProp; } });
|
|
63
63
|
Object.defineProperty(exports, "invokeExecution", { enumerable: true, get: function () { return hook_1.invokeExecution; } });
|
|
64
64
|
var HookContext_1 = require("./hooks/HookContext");
|
package/esm/hooks/hook.js
CHANGED
|
@@ -13,9 +13,17 @@ export function getHooks(target, key) {
|
|
|
13
13
|
export function hasHooks(target, key) {
|
|
14
14
|
return Reflect.hasMetadata(key, target.constructor);
|
|
15
15
|
}
|
|
16
|
-
export const runHooks = (target, key, { scope, createContext = createHookContext,
|
|
16
|
+
export const runHooks = (target, key, { scope, createContext = createHookContext, }) => {
|
|
17
17
|
const hooks = Array.from(getHooks(target, key).entries());
|
|
18
|
-
const
|
|
18
|
+
for (const [methodName, executions] of hooks) {
|
|
19
|
+
for (const execute of executions) {
|
|
20
|
+
execute(createContext(target, scope, methodName));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
};
|
|
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));
|
|
19
27
|
return Promise.all(hooks.flatMap(([methodName, executions]) => executions.map((execute) => runExecution(execute, createContext(target, scope, methodName)))));
|
|
20
28
|
};
|
|
21
29
|
export const injectProp = (fn) => (context) => context.injectProperty(fn);
|
|
@@ -23,8 +31,3 @@ export const invokeExecution = ({ handleResult }) => async (context) => {
|
|
|
23
31
|
const args = await Promise.all(context.resolveArgs().map(promisify));
|
|
24
32
|
return handleResult(context.invokeMethod({ args }), context);
|
|
25
33
|
};
|
|
26
|
-
/**
|
|
27
|
-
* @deprecated Use `runHooks` instead
|
|
28
|
-
* @TODO: Remove in v33
|
|
29
|
-
*/
|
|
30
|
-
export const executeHooks = runHooks;
|
package/esm/index.js
CHANGED
|
@@ -22,7 +22,7 @@ export { DependencyNotFoundError } from './errors/DependencyNotFoundError';
|
|
|
22
22
|
export { MethodNotImplementedError } from './errors/MethodNotImplementedError';
|
|
23
23
|
export { ContainerDisposedError } from './errors/ContainerDisposedError';
|
|
24
24
|
// Hooks
|
|
25
|
-
export { getHooks, hook, hasHooks,
|
|
25
|
+
export { getHooks, hook, hasHooks, runHooks, runHooksAsync, injectProp, invokeExecution } from './hooks/hook';
|
|
26
26
|
export { HookContext } from './hooks/HookContext';
|
|
27
27
|
// Others
|
|
28
28
|
export { by, IMemoKey, byAlias, byAliases } from './by';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-ioc-container",
|
|
3
|
-
"version": "32.
|
|
3
|
+
"version": "32.12.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": "0220f5ff8371041dc9c7a4642d3a0b41a1f9a5e6"
|
|
63
63
|
}
|
package/typings/hooks/hook.d.ts
CHANGED
|
@@ -5,23 +5,17 @@ type HooksOfClass = Map<string, Hook[]>;
|
|
|
5
5
|
export declare const hook: (key: string | symbol, ...fns: Hook[]) => (target: object, propertyKey: string | symbol) => void;
|
|
6
6
|
export declare function getHooks(target: object, key: string | symbol): HooksOfClass;
|
|
7
7
|
export declare function hasHooks(target: object, key: string | symbol): boolean;
|
|
8
|
-
export declare const runHooks: (target: object, key: string | symbol, { scope, createContext,
|
|
8
|
+
export declare const runHooks: (target: object, key: string | symbol, { scope, createContext, }: {
|
|
9
|
+
scope: IContainer;
|
|
10
|
+
createContext?: ((Target: object, scope: IContainer, methodName?: string) => IHookContext) | undefined;
|
|
11
|
+
}) => void;
|
|
12
|
+
export declare const runHooksAsync: (target: object, key: string | symbol, { scope, createContext, }: {
|
|
9
13
|
scope: IContainer;
|
|
10
14
|
createContext?: ((Target: object, scope: IContainer, methodName?: string) => IHookContext) | undefined;
|
|
11
|
-
handleError: (e: Error, s: IContainer) => void;
|
|
12
15
|
}) => Promise<void[]>;
|
|
13
16
|
export declare const injectProp: (fn: InjectFn) => Hook;
|
|
14
17
|
type HandleResult = (result: unknown, context: IHookContext) => void | Promise<void>;
|
|
15
18
|
export declare const invokeExecution: ({ handleResult }: {
|
|
16
19
|
handleResult: HandleResult;
|
|
17
20
|
}) => Hook;
|
|
18
|
-
/**
|
|
19
|
-
* @deprecated Use `runHooks` instead
|
|
20
|
-
* @TODO: Remove in v33
|
|
21
|
-
*/
|
|
22
|
-
export declare const executeHooks: (target: object, key: string | symbol, { scope, createContext, handleError, }: {
|
|
23
|
-
scope: IContainer;
|
|
24
|
-
createContext?: ((Target: object, scope: IContainer, methodName?: string) => IHookContext) | undefined;
|
|
25
|
-
handleError: (e: Error, s: IContainer) => void;
|
|
26
|
-
}) => Promise<void[]>;
|
|
27
21
|
export {};
|
package/typings/index.d.ts
CHANGED
|
@@ -18,8 +18,8 @@ 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,
|
|
22
|
-
export { HookContext, InjectFn } from './hooks/HookContext';
|
|
21
|
+
export { getHooks, hook, hasHooks, Hook, runHooks, runHooksAsync, injectProp, invokeExecution } from './hooks/hook';
|
|
22
|
+
export { HookContext, InjectFn, IHookContext } from './hooks/HookContext';
|
|
23
23
|
export { by, InstancePredicate, IMemo, IMemoKey, byAlias, byAliases } from './by';
|
|
24
24
|
export { constructor } from './utils';
|
|
25
25
|
export { setMetadata, getMetadata, setParameterMetadata, setMethodMetadata, getMethodMetadata, getParameterMetadata, } from './metadata';
|