forked from smithamax/di-ioc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
42 lines (35 loc) · 1.05 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* eslint no-console:0 */
'use strict';
var ioc = require('./');
// Utility submodule
var util = ioc.create().define('random', function () {
var pseudoRandomBytes = require('crypto').pseudoRandomBytes;
return {
base64: function () {
return pseudoRandomBytes(20).toString('base64');
},
};
});
// Application submodule
var app = ioc.create().define('greet', function (random) {
return function (name) {
console.log(
'Hello ' + name + '! Here is a random string: ' + random.base64()
);
};
});
// Module export:
const cont = ioc
.create()
.define('util', util)
.define('app', app)
.on('factory-added', (...args) => console.log('factory-added', args))
.on('factory-called', (...args) => console.log('factory-called', args))
.on('service-used', (...args) => console.log('service-used', args))
.init();
// Using module:
var randomService = cont.util.random;
// eQ/NZnl7qusVN9hB/3nCn3wFKfY=
console.log(randomService.base64());
// Hello World! Here is a random string: dfLGC20CpCJxAZSu+uFp57dlJl0=
cont.app.greet('World');