-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer.js
More file actions
154 lines (122 loc) · 3.54 KB
/
Copy pathcontainer.js
File metadata and controls
154 lines (122 loc) · 3.54 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
(function()
{
"use strict";
Array.prototype.indexOf = Array.prototype.indexOf || function(value)
{
return $.inArray(value, this);
};
function Container(parent)
{
this.initialize(parent);
}
window.Container = Container;
Container.prototype = {
constructor: Container,
initialize: function(parent)
{
this._aliases = {};
this._instances = {};
this._dataStore = {};
this._parent = parent || null;
},
alias: function(alias, key)
{
this._aliases[alias] = key;
return this;
},
_resolveAlias: function(key)
{
if(this._aliases.hasOwnProperty(key))
{
return this._aliases[key];
}
return key;
},
set: function(key, value, shared, protect)
{
var dataStore = this._dataStore,
callback;
if(dataStore.hasOwnProperty(key) && dataStore[key].protect)
{
throw new Error('Key ' + key + ' is protected and can\'t be overwritten.');
}
if(typeof value !== 'function')
{
callback = function()
{
return value;
};
}
else
{
callback = value;
}
dataStore[key] = {
callback: callback,
shared: Boolean(shared), // singleton
protect: Boolean(protect) // write protect
};
return this;
},
_getRaw: function(key)
{
key = this._resolveAlias(key);
if(this._dataStore.hasOwnProperty(key))
{
return this._dataStore[key];
}
else if(this._parent instanceof Container)
{
return this._parent._getRaw(key);
}
return null;
},
get: function(key, forceNew)
{
var raw = this._getRaw(key);
if(raw === null)
{
throw new Error('Key ' + key + ' has not been registered with the container.');
}
if(raw.shared)
{
if(!this._instances.hasOwnProperty(key) || forceNew)
{
this._instances[key] = raw.callback.call(this);
}
return this._instances[key];
}
return raw.callback.call(this);
},
protect: function(key, callback, shared)
{
return this.set(key, callback, shared, true);
},
share: function(key, callback, protect)
{
return this.set(key, callback, true, protect);
},
exists: function(key)
{
return Boolean(this._getRaw(key));
},
createChild: function()
{
return new Container(this);
},
register: function(className, callback)
{
this.set('Class:' + className, callback, true, true);
this.set(className, function()
{
var constructor = this.get('Class:' + className);
if(typeof constructor !== 'function')
{
throw new Error(className + ' is not a function!');
}
return new constructor;
}, true);
return this;
}
};
})();