-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathModule.js
More file actions
76 lines (61 loc) · 1.99 KB
/
Module.js
File metadata and controls
76 lines (61 loc) · 1.99 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* vim: set et sw=2 ts=2: */
'use strict';
var lib = require('./lib');
var $ = require('nodash');
module.exports = function Module(factory, name) {
var _name = name;
var _isInitialized = false;
var _isSingleton = true;
var _instance = null;
var _error = null;
var _factory = factory;
var _dependencies = [];
if (!$.isFunction(factory) || factory.$factory === false) {
_factory = $.idf(factory);
_instance = factory;
_isInitialized = true;
} else {
if ($.isArray(_factory.$dependencies) && $.all($.isString, _factory.$dependencies)) {
_dependencies = $.map($.id, _factory.$dependencies);
} else {
_dependencies = lib.getFunctionParameterNames(_factory);
}
_isSingleton = factory.$singleton !== false;
}
if ($.any($.eq('$module'), _dependencies)) {
_isSingleton = false;
}
var _isAsync = $.any($.eq('$done'), _dependencies);
this.getInstance = function getInstance(container, callback) {
var __forModule = arguments[2];
if (_isInitialized) {
setImmediate(function () {
container.emit('retrieved', name, _error, _instance);
callback(_error, _instance);
});
} else {
setImmediate(function () {
container.emit('initializing', name, _isInitialized);
container.injectWith(_factory, _dependencies, function (err, instance) {
container.emit('initialized', name, err, instance, _isInitialized);
_isInitialized = _isSingleton;
if (err) {
_error = err;
callback(err);
} else {
if (_isSingleton) {
_instance = instance;
}
callback(null, instance);
}
}, _name, __forModule);
});
}
};
this.dependencies = $.idf(_dependencies);
this.hasError = function () { return !!_error; };
this.isAsync = $.idf(_isAsync);
this.isInitialized = function () { return _isInitialized; };
this.isSingleton = $.idf(_isSingleton);
this.name = $.idf(_name);
};