UNPKG

1.62 kBJavaScriptView Raw
1var request = require('request');
2var norma = require('norma');
3var debug = require('debug', 'waif:waif');
4var assert = require('assert');
5var _ = require('lodash');
6var Service = require('./service');
7
8/**
9* Waif constructor.
10*
11* Instances will contain a map to all of the
12* services that have been mounted onto the
13* system.
14*/
15function Waif(opts) {
16 this._services = {};
17 return this;
18}
19
20// retrieve or set a service
21Waif.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
32Waif.prototype.start = function() {
33 debug('start');
34
35 _(this._services).invoke('start');
36 return this;
37};
38
39Waif.prototype.stop = function() {
40 debug('stop');
41 _(this._services).invoke('stop');
42 return this;
43};
44
45
46Waif.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
64Waif._instance = null;
65
66Waif.getInstance = function() {
67 if(this._instance === null){
68 this._instance = Waif.createInstance();
69 }
70 return this._instance;
71};
72
73
74module.exports = _makeExport();
75
76function _makeExport() {
77 var fn = Waif.getInstance.bind(Waif);
78 fn.createInstance = Waif.createInstance;
79 return fn;
80}