1 | var request = require('request');
|
2 | var norma = require('norma');
|
3 | var debug = require('debug', 'waif:waif');
|
4 | var assert = require('assert');
|
5 | var _ = require('lodash');
|
6 | var Service = require('./service');
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function Waif(opts) {
|
16 | this._services = {};
|
17 | return this;
|
18 | }
|
19 |
|
20 |
|
21 | Waif.prototype.service = function() {
|
22 | var args = norma('s', arguments);
|
23 | var name = args[0];
|
24 |
|
25 | debug('service', args);
|
26 | if (!this._services[name]) {
|
27 | this._services[name] = Service.createInstance(name, this.accessor);
|
28 | }
|
29 | return this._services[name];
|
30 | };
|
31 |
|
32 | Waif.prototype.start = function() {
|
33 | debug('start');
|
34 |
|
35 | _(this._services).invoke('start');
|
36 | return this;
|
37 | };
|
38 |
|
39 | Waif.prototype.stop = function() {
|
40 | debug('stop');
|
41 | _(this._services).invoke('stop');
|
42 | return this;
|
43 | };
|
44 |
|
45 |
|
46 | Waif.createInstance = function() {
|
47 | var _waif = new Waif();
|
48 | var fn = function() {
|
49 | var args = norma('s', arguments);
|
50 | var name = args[0];
|
51 |
|
52 | return _waif.service(name);
|
53 | };
|
54 | fn._id = _.uniqueId();
|
55 |
|
56 | fn.instance = _waif;
|
57 | fn.createInstance = Waif.createInstance;
|
58 | fn.start = _waif.start.bind(_waif);
|
59 | fn.stop = _waif.stop.bind(_waif);
|
60 | _waif.accessor = fn;
|
61 | return fn;
|
62 | };
|
63 |
|
64 | Waif._instance = null;
|
65 |
|
66 | Waif.getInstance = function() {
|
67 | if(this._instance === null){
|
68 | this._instance = Waif.createInstance();
|
69 | }
|
70 | return this._instance;
|
71 | };
|
72 |
|
73 |
|
74 | module.exports = _makeExport();
|
75 |
|
76 | function _makeExport() {
|
77 | var fn = Waif.getInstance.bind(Waif);
|
78 | fn.createInstance = Waif.createInstance;
|
79 | return fn;
|
80 | }
|