ts-ioc-container 32.3.0 → 32.5.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 +6 -13
- package/cjm/hooks/hook.js +6 -4
- package/esm/hooks/hook.js +6 -4
- package/package.json +2 -2
- package/typings/hooks/hook.d.ts +2 -2
package/README.md
CHANGED
|
@@ -1141,13 +1141,13 @@ import {
|
|
|
1141
1141
|
constructor,
|
|
1142
1142
|
Container,
|
|
1143
1143
|
key,
|
|
1144
|
-
getHooks,
|
|
1145
1144
|
hook,
|
|
1146
1145
|
IContainer,
|
|
1147
1146
|
IInjector,
|
|
1148
1147
|
MetadataInjector,
|
|
1149
1148
|
Registration as R,
|
|
1150
1149
|
register,
|
|
1150
|
+
executeHooks,
|
|
1151
1151
|
} from 'ts-ioc-container';
|
|
1152
1152
|
import { InjectOptions } from '../lib/injector/IInjector.ts';
|
|
1153
1153
|
|
|
@@ -1156,11 +1156,7 @@ class MyInjector implements IInjector {
|
|
|
1156
1156
|
|
|
1157
1157
|
resolve<T>(container: IContainer, value: constructor<T>, options: InjectOptions): T {
|
|
1158
1158
|
const instance = this.injector.resolve(container, value, options);
|
|
1159
|
-
|
|
1160
|
-
for (const [h] of getHooks(instance as object, 'onConstruct')) {
|
|
1161
|
-
// @ts-ignore
|
|
1162
|
-
instance[h]();
|
|
1163
|
-
}
|
|
1159
|
+
executeHooks(instance as object, 'onConstruct', { scope: container });
|
|
1164
1160
|
return instance;
|
|
1165
1161
|
}
|
|
1166
1162
|
}
|
|
@@ -1169,7 +1165,7 @@ class MyInjector implements IInjector {
|
|
|
1169
1165
|
class Logger {
|
|
1170
1166
|
isReady = false;
|
|
1171
1167
|
|
|
1172
|
-
@hook('onConstruct') // <--- or extract it to @onConstruct
|
|
1168
|
+
@hook('onConstruct', (context) => context.invokeMethod({ args: [] })) // <--- or extract it to @onConstruct
|
|
1173
1169
|
initialize() {
|
|
1174
1170
|
this.isReady = true;
|
|
1175
1171
|
}
|
|
@@ -1206,6 +1202,7 @@ import {
|
|
|
1206
1202
|
Registration as R,
|
|
1207
1203
|
MetadataInjector,
|
|
1208
1204
|
register,
|
|
1205
|
+
executeHooks,
|
|
1209
1206
|
} from 'ts-ioc-container';
|
|
1210
1207
|
|
|
1211
1208
|
@register(key('logsRepo'))
|
|
@@ -1228,7 +1225,7 @@ class Logger {
|
|
|
1228
1225
|
this.messages.push(message);
|
|
1229
1226
|
}
|
|
1230
1227
|
|
|
1231
|
-
@hook('onDispose') // <--- or extract it to @onDispose
|
|
1228
|
+
@hook('onDispose', (c) => c.invokeMethod({ args: [] })) // <--- or extract it to @onDispose
|
|
1232
1229
|
async save(): Promise<void> {
|
|
1233
1230
|
this.logsRepo.saveLogs(this.messages);
|
|
1234
1231
|
this.messages = [];
|
|
@@ -1243,11 +1240,7 @@ describe('onDispose', function () {
|
|
|
1243
1240
|
logger.log('Hello');
|
|
1244
1241
|
|
|
1245
1242
|
for (const instance of container.getInstances()) {
|
|
1246
|
-
|
|
1247
|
-
for (const [h] of getHooks(instance as object, 'onDispose')) {
|
|
1248
|
-
// @ts-ignore
|
|
1249
|
-
await instance[h]();
|
|
1250
|
-
}
|
|
1243
|
+
executeHooks(instance as object, 'onDispose', { scope: container });
|
|
1251
1244
|
}
|
|
1252
1245
|
|
|
1253
1246
|
expect(container.resolve<LogsRepo>('logsRepo').savedLogs).toContain('Hello');
|
package/cjm/hooks/hook.js
CHANGED
|
@@ -3,11 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.executeHooks = exports.hasHooks = exports.getHooks = exports.hook = void 0;
|
|
4
4
|
const ExecutionContext_1 = require("./ExecutionContext");
|
|
5
5
|
const createStore = () => new Map();
|
|
6
|
-
const hook = (key,
|
|
6
|
+
const hook = (key, ...fns) => (target, propertyKey) => {
|
|
7
7
|
const hooks = Reflect.hasMetadata(key, target.constructor)
|
|
8
8
|
? Reflect.getMetadata(key, target.constructor)
|
|
9
9
|
: createStore();
|
|
10
|
-
hooks.set(propertyKey,
|
|
10
|
+
hooks.set(propertyKey, (hooks.get(propertyKey) ?? []).concat(fns));
|
|
11
11
|
Reflect.defineMetadata(key, hooks, target.constructor); // eslint-disable-line @typescript-eslint/ban-types
|
|
12
12
|
};
|
|
13
13
|
exports.hook = hook;
|
|
@@ -20,8 +20,10 @@ function hasHooks(target, key) {
|
|
|
20
20
|
}
|
|
21
21
|
exports.hasHooks = hasHooks;
|
|
22
22
|
const executeHooks = (target, key, { scope, createContext = (c) => c, }) => {
|
|
23
|
-
for (const [methodName,
|
|
24
|
-
|
|
23
|
+
for (const [methodName, executions] of getHooks(target, key)) {
|
|
24
|
+
for (const execute of executions) {
|
|
25
|
+
execute(createContext(new ExecutionContext_1.ExecutionContext(target, methodName, scope)));
|
|
26
|
+
}
|
|
25
27
|
}
|
|
26
28
|
};
|
|
27
29
|
exports.executeHooks = executeHooks;
|
package/esm/hooks/hook.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { ExecutionContext } from './ExecutionContext';
|
|
2
2
|
const createStore = () => new Map();
|
|
3
|
-
export const hook = (key,
|
|
3
|
+
export const hook = (key, ...fns) => (target, propertyKey) => {
|
|
4
4
|
const hooks = Reflect.hasMetadata(key, target.constructor)
|
|
5
5
|
? Reflect.getMetadata(key, target.constructor)
|
|
6
6
|
: createStore();
|
|
7
|
-
hooks.set(propertyKey,
|
|
7
|
+
hooks.set(propertyKey, (hooks.get(propertyKey) ?? []).concat(fns));
|
|
8
8
|
Reflect.defineMetadata(key, hooks, target.constructor); // eslint-disable-line @typescript-eslint/ban-types
|
|
9
9
|
};
|
|
10
10
|
export function getHooks(target, key) {
|
|
@@ -14,7 +14,9 @@ export function hasHooks(target, key) {
|
|
|
14
14
|
return Reflect.hasMetadata(key, target.constructor);
|
|
15
15
|
}
|
|
16
16
|
export const executeHooks = (target, key, { scope, createContext = (c) => c, }) => {
|
|
17
|
-
for (const [methodName,
|
|
18
|
-
|
|
17
|
+
for (const [methodName, executions] of getHooks(target, key)) {
|
|
18
|
+
for (const execute of executions) {
|
|
19
|
+
execute(createContext(new ExecutionContext(target, methodName, scope)));
|
|
20
|
+
}
|
|
19
21
|
}
|
|
20
22
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-ioc-container",
|
|
3
|
-
"version": "32.
|
|
3
|
+
"version": "32.5.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": "464f1f083a81a9346e6a713373be32e204c03238"
|
|
63
63
|
}
|
package/typings/hooks/hook.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { IContainer } from '../container/IContainer';
|
|
2
2
|
import { ExecutionContext } from './ExecutionContext';
|
|
3
3
|
export type Execution = <T extends ExecutionContext>(context: T) => void;
|
|
4
|
-
export declare const hook: (key: string | symbol,
|
|
5
|
-
export declare function getHooks(target: object, key: string | symbol): Map<string, Execution>;
|
|
4
|
+
export declare const hook: (key: string | symbol, ...fns: Execution[]) => MethodDecorator;
|
|
5
|
+
export declare function getHooks(target: object, key: string | symbol): Map<string, Execution[]>;
|
|
6
6
|
export declare function hasHooks(target: object, key: string | symbol): boolean;
|
|
7
7
|
export declare const executeHooks: <Context extends ExecutionContext>(target: object, key: string | symbol, { scope, createContext, }: {
|
|
8
8
|
scope: IContainer;
|