ts-ioc-container 44.0.0 → 44.0.1
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 +12 -56
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1392,68 +1392,28 @@ Sometimes you need to invoke methods after construct or dispose of class. This i
|
|
|
1392
1392
|
|
|
1393
1393
|
### OnConstruct
|
|
1394
1394
|
```typescript
|
|
1395
|
-
import {
|
|
1396
|
-
bindTo,
|
|
1397
|
-
Container,
|
|
1398
|
-
hook,
|
|
1399
|
-
HookContext,
|
|
1400
|
-
HookFn,
|
|
1401
|
-
HooksRunner,
|
|
1402
|
-
inject,
|
|
1403
|
-
onConstruct,
|
|
1404
|
-
register,
|
|
1405
|
-
Registration as R,
|
|
1406
|
-
} from 'ts-ioc-container';
|
|
1395
|
+
import { Container, HookContext, HookFn, HooksRunner, inject, onConstruct, Registration as R } from 'ts-ioc-container';
|
|
1407
1396
|
|
|
1408
1397
|
const onConstructHooksRunner = new HooksRunner('onConstruct');
|
|
1398
|
+
const execute: HookFn = (ctx: HookContext) => {
|
|
1399
|
+
ctx.invokeMethod({ args: ctx.resolveArgs() });
|
|
1400
|
+
};
|
|
1409
1401
|
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
isReady = false;
|
|
1402
|
+
class Car {
|
|
1403
|
+
private engine!: string;
|
|
1413
1404
|
|
|
1414
|
-
@
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
initialize() {
|
|
1418
|
-
this.isReady = true;
|
|
1405
|
+
@onConstruct(execute)
|
|
1406
|
+
setEngine(@inject('engine') engine: string) {
|
|
1407
|
+
this.engine = engine;
|
|
1419
1408
|
}
|
|
1420
1409
|
|
|
1421
|
-
|
|
1422
|
-
|
|
1410
|
+
getEngine() {
|
|
1411
|
+
return this.engine;
|
|
1423
1412
|
}
|
|
1424
1413
|
}
|
|
1425
1414
|
|
|
1426
1415
|
describe('onConstruct', function () {
|
|
1427
|
-
it('should make logger be ready on resolve', function () {
|
|
1428
|
-
const container = new Container()
|
|
1429
|
-
.addOnConstructHook((instance, scope) => {
|
|
1430
|
-
onConstructHooksRunner.execute(instance as object, { scope });
|
|
1431
|
-
})
|
|
1432
|
-
.addRegistration(R.fromClass(Logger));
|
|
1433
|
-
|
|
1434
|
-
const logger = container.resolve<Logger>('logger');
|
|
1435
|
-
|
|
1436
|
-
expect(logger.isReady).toBe(true);
|
|
1437
|
-
});
|
|
1438
|
-
|
|
1439
1416
|
it('should run methods and resolve arguments from container', function () {
|
|
1440
|
-
const execute: HookFn = (ctx: HookContext) => {
|
|
1441
|
-
ctx.invokeMethod({ args: ctx.resolveArgs() });
|
|
1442
|
-
};
|
|
1443
|
-
|
|
1444
|
-
class Car {
|
|
1445
|
-
private engine!: string;
|
|
1446
|
-
|
|
1447
|
-
@onConstruct(execute)
|
|
1448
|
-
setEngine(@inject('engine') engine: string) {
|
|
1449
|
-
this.engine = engine;
|
|
1450
|
-
}
|
|
1451
|
-
|
|
1452
|
-
getEngine() {
|
|
1453
|
-
return this.engine;
|
|
1454
|
-
}
|
|
1455
|
-
}
|
|
1456
|
-
|
|
1457
1417
|
const root = new Container()
|
|
1458
1418
|
.addOnConstructHook((instance, scope) => {
|
|
1459
1419
|
onConstructHooksRunner.execute(instance as object, { scope });
|
|
@@ -1492,14 +1452,10 @@ class Logger {
|
|
|
1492
1452
|
|
|
1493
1453
|
constructor(@inject('logsRepo') private logsRepo: LogsRepo) {}
|
|
1494
1454
|
|
|
1495
|
-
log(
|
|
1455
|
+
log(message: string): void {
|
|
1496
1456
|
this.messages.push(message);
|
|
1497
1457
|
}
|
|
1498
1458
|
|
|
1499
|
-
size(): number {
|
|
1500
|
-
return this.messages.length;
|
|
1501
|
-
}
|
|
1502
|
-
|
|
1503
1459
|
@hook('onDispose', (c) => {
|
|
1504
1460
|
c.invokeMethod({ args: [] });
|
|
1505
1461
|
}) // <--- or extract it to @onDispose
|