ts-ioc-container 44.0.0 → 44.0.2
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 +31 -62
- 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 });
|
|
@@ -1470,9 +1430,24 @@ describe('onConstruct', function () {
|
|
|
1470
1430
|
|
|
1471
1431
|
### OnDispose
|
|
1472
1432
|
```typescript
|
|
1473
|
-
import {
|
|
1433
|
+
import {
|
|
1434
|
+
bindTo,
|
|
1435
|
+
Container,
|
|
1436
|
+
hook,
|
|
1437
|
+
type HookFn,
|
|
1438
|
+
HooksRunner,
|
|
1439
|
+
inject,
|
|
1440
|
+
register,
|
|
1441
|
+
Registration as R,
|
|
1442
|
+
select,
|
|
1443
|
+
singleton,
|
|
1444
|
+
} from 'ts-ioc-container';
|
|
1474
1445
|
|
|
1475
1446
|
const onDisposeHookRunner = new HooksRunner('onDispose');
|
|
1447
|
+
const execute: HookFn = (ctx) => {
|
|
1448
|
+
ctx.invokeMethod({ args: ctx.resolveArgs() });
|
|
1449
|
+
};
|
|
1450
|
+
|
|
1476
1451
|
@register(bindTo('logsRepo'), singleton())
|
|
1477
1452
|
class LogsRepo {
|
|
1478
1453
|
savedLogs: string[] = [];
|
|
@@ -1492,24 +1467,18 @@ class Logger {
|
|
|
1492
1467
|
|
|
1493
1468
|
constructor(@inject('logsRepo') private logsRepo: LogsRepo) {}
|
|
1494
1469
|
|
|
1495
|
-
log(
|
|
1470
|
+
log(message: string): void {
|
|
1496
1471
|
this.messages.push(message);
|
|
1497
1472
|
}
|
|
1498
1473
|
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
}
|
|
1502
|
-
|
|
1503
|
-
@hook('onDispose', (c) => {
|
|
1504
|
-
c.invokeMethod({ args: [] });
|
|
1505
|
-
}) // <--- or extract it to @onDispose
|
|
1506
|
-
async save(): Promise<void> {
|
|
1474
|
+
@hook('onDispose', execute) // <--- or extract it to @onDispose
|
|
1475
|
+
save() {
|
|
1507
1476
|
this.logsRepo.saveLogs(this.messages);
|
|
1508
1477
|
}
|
|
1509
1478
|
}
|
|
1510
1479
|
|
|
1511
1480
|
describe('onDispose', function () {
|
|
1512
|
-
it('should invoke hooks on all instances',
|
|
1481
|
+
it('should invoke hooks on all instances', function () {
|
|
1513
1482
|
const container = new Container().addRegistration(R.fromClass(Logger)).addRegistration(R.fromClass(LogsRepo));
|
|
1514
1483
|
|
|
1515
1484
|
const logger = container.resolve<Logger>('logger');
|