ts-ioc-container 32.4.0 → 32.6.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 +15 -16
- package/package.json +2 -2
- package/typings/hooks/hook.d.ts +1 -1
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
|
}
|
|
@@ -1199,13 +1195,13 @@ import {
|
|
|
1199
1195
|
by,
|
|
1200
1196
|
Container,
|
|
1201
1197
|
key,
|
|
1202
|
-
getHooks,
|
|
1203
1198
|
hook,
|
|
1204
1199
|
inject,
|
|
1205
1200
|
provider,
|
|
1206
1201
|
Registration as R,
|
|
1207
1202
|
MetadataInjector,
|
|
1208
1203
|
register,
|
|
1204
|
+
executeHooks,
|
|
1209
1205
|
} from 'ts-ioc-container';
|
|
1210
1206
|
|
|
1211
1207
|
@register(key('logsRepo'))
|
|
@@ -1220,6 +1216,10 @@ class LogsRepo {
|
|
|
1220
1216
|
|
|
1221
1217
|
@register(key('logger'))
|
|
1222
1218
|
class Logger {
|
|
1219
|
+
@hook('onDispose', ({ instance, methodName }) => {
|
|
1220
|
+
// @ts-ignore
|
|
1221
|
+
instance[methodName].push('world');
|
|
1222
|
+
}) // <--- or extract it to @onDispose
|
|
1223
1223
|
private messages: string[] = [];
|
|
1224
1224
|
|
|
1225
1225
|
constructor(@inject(by.key('logsRepo')) private logsRepo: LogsRepo) {}
|
|
@@ -1228,10 +1228,13 @@ class Logger {
|
|
|
1228
1228
|
this.messages.push(message);
|
|
1229
1229
|
}
|
|
1230
1230
|
|
|
1231
|
-
|
|
1231
|
+
size(): number {
|
|
1232
|
+
return this.messages.length;
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
@hook('onDispose', (c) => c.invokeMethod({ args: [] })) // <--- or extract it to @onDispose
|
|
1232
1236
|
async save(): Promise<void> {
|
|
1233
1237
|
this.logsRepo.saveLogs(this.messages);
|
|
1234
|
-
this.messages = [];
|
|
1235
1238
|
}
|
|
1236
1239
|
}
|
|
1237
1240
|
|
|
@@ -1243,14 +1246,10 @@ describe('onDispose', function () {
|
|
|
1243
1246
|
logger.log('Hello');
|
|
1244
1247
|
|
|
1245
1248
|
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
|
-
}
|
|
1249
|
+
executeHooks(instance as object, 'onDispose', { scope: container });
|
|
1251
1250
|
}
|
|
1252
1251
|
|
|
1253
|
-
expect(container.resolve<LogsRepo>('logsRepo').savedLogs).
|
|
1252
|
+
expect(container.resolve<LogsRepo>('logsRepo').savedLogs.join(',')).toBe('Hello,world');
|
|
1254
1253
|
});
|
|
1255
1254
|
});
|
|
1256
1255
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-ioc-container",
|
|
3
|
-
"version": "32.
|
|
3
|
+
"version": "32.6.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": "991c10cb1fc85ecb8ac3b918ffcdd6bae3f100d4"
|
|
63
63
|
}
|
package/typings/hooks/hook.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
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, ...fns: Execution[]) =>
|
|
4
|
+
export declare const hook: (key: string | symbol, ...fns: Execution[]) => (target: object, propertyKey: string | symbol) => void;
|
|
5
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, }: {
|