annotate src/implab/di/Container.js @ 0:fc2517695ee1

Initial commit, draft import of existing work
author cin
date Thu, 01 Jun 2017 13:20:03 +0300
parents
children 93fb6c09f2e1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
1 define([
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
2 "../declare",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
3 "../safe",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
4 "../UUID",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
5 "dojo/Deferred",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
6 "./ActivationContext",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
7 "./Descriptor",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
8 "./ValueDescriptor",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
9 "./ReferenceDescriptor",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
10 "./ServiceDescriptor",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
11 "./ActivationError"
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
12 ], function (
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
13 declare,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
14 array,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
15 safe,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
16 UUID,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
17 Deferred,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
18 ActivationContext,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
19 Descriptor,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
20 Value,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
21 Reference,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
22 Service,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
23 ActivationError) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
24 var Container = declare(null, {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
25 _services: null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
26 _cache: null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
27 _cleanup: null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
28 _root: null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
29 _parent: null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
30
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
31 constructor: function (parent) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
32 this._parent = parent;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
33 this._services = parent ? safe.create(parent._services) : {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
34 this._cache = {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
35 this._cleanup = [];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
36 this._root = parent ? parent.getRootContainer() : this;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
37 this._services.container = new Value(this, true);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
38 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
39
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
40 getRootContainer: function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
41 return this._root;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
42 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
43
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
44 getParent: function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
45 return this._parent;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
46 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
47
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
48 getService: function (name, def) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
49 var d = this._services[name];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
50 if (!d)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
51 if (arguments.length > 1)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
52 return def;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
53 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
54 throw new Error("Service '" + name + "' isn't found");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
55 if (d.isInstanceCreated())
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
56 return d.getInstance();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
57
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
58 var context = new ActivationContext(this, this._services);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
59
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
60 try {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
61 return d.activate(context, name);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
62 } catch (error) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
63 throw new ActivationError(name, context.getStack(), error);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
64 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
65 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
66
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
67 register: function (name, service) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
68 if (arguments.length == 1) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
69 var data = name;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
70 for (name in data)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
71 this.register(name, data[name]);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
72 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
73 if (!(service instanceof Descriptor))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
74 service = new Value(service, true);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
75 this._services[name] = service;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
76 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
77 return this;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
78 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
79
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
80 onDispose: function (callback) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
81 if (!(callback instanceof Function))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
82 throw new Error("The callback must be a function");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
83 this._cleanup.push(callback);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
84 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
85
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
86 dispose: function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
87 if (this._cleanup) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
88 for (var i = 0; i < this._cleanup.length; i++)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
89 this._cleanup[i].call(null);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
90 this._cleanup = null;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
91 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
92 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
93
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
94 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
95 * @param{String|Object} config
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
96 * The configuration of the contaier. Can be either a string or an object,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
97 * if the configuration is an object it's treated as a collection of
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
98 * services which will be registed in the contaier.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
99 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
100 * @param{Function} opts.contextRequire
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
101 * The function which will be used to load a configuration or types for services.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
102 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
103 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
104 configure: function (config, opts) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
105 var p, me = this,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
106 contextRequire = (opts && opts.contextRequire);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
107
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
108 if (typeof (config) === "string") {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
109 p = new Deferred();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
110 if (!contextRequire) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
111 var shim = config + "-" + UUID();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
112 define(shim, ["require", config], function (ctx, data) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
113 p.resolve([data, {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
114 contextRequire: ctx
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
115 }]);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
116 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
117 require([shim]);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
118 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
119 // TODO how to get correct contextRequire for the relative config module?
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
120 contextRequire([config], function (data) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
121 p.resolve([data, {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
122 contextRequire: contextRequire
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
123 }]);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
124 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
125 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
126
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
127 return p.then(function (args) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
128 return me._configure.apply(me, args);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
129 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
130 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
131 return me._configure(config, opts);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
132 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
133 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
134
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
135 createChildContainer: function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
136 return new Container(this);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
137 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
138
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
139 has: function (id) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
140 return id in this._cache;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
141 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
142
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
143 get: function (id) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
144 return this._cache[id];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
145 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
146
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
147 store: function (id, value) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
148 return (this._cache[id] = value);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
149 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
150
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
151 _configure: function (data, opts) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
152 var typemap = {},
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
153 d = new Deferred(),
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
154 me = this,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
155 p,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
156 contextRequire = (opts && opts.contextRequire) || require;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
157
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
158 var services = {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
159
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
160 for (p in data) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
161 var service = me._parse(data[p], typemap);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
162 if (!(service instanceof Descriptor))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
163 service = new Value(service, false);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
164 services[p] = service;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
165 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
166
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
167 me.register(services);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
168
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
169 var names = [];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
170
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
171 for (p in typemap)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
172 names.push(p);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
173
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
174 if (names.length) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
175 contextRequire(names, function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
176 for (var i = 0; i < names.length; i++)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
177 typemap[names[i]] = arguments[i];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
178 d.resolve(me);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
179 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
180 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
181 d.resolve(me);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
182 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
183 return d.promise;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
184 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
185
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
186 _parse: function (data, typemap) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
187 if (safe.isPrimitive(data) || data instanceof Descriptor)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
188 return data;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
189 if (data.$dependency)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
190 return new Reference(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
191 data.$dependency,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
192 data.lazy,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
193 data.optional,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
194 data["default"],
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
195 data.services && this._parseObject(data.services, typemap));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
196 if (data.$value) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
197 var raw = !data.parse;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
198 return new Value(raw ? data.$value : this._parse(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
199 data.$value,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
200 typemap), raw);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
201 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
202 if (data.$type || data.$factory)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
203 return this._parseService(data, typemap);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
204 if (data instanceof Array)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
205 return this._parseArray(data, typemap);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
206
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
207 return this._parseObject(data, typemap);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
208 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
209
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
210 _parseService: function (data, typemap) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
211 var me = this,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
212 opts = {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
213 owner: this
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
214 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
215 if (data.$type) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
216
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
217 opts.type = data.$type;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
218
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
219 if (typeof (data.$type) === "string") {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
220 typemap[data.$type] = null;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
221 opts.typeMap = typemap;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
222 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
223 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
224
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
225 if (data.$factory)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
226 opts.factory = data.$factory;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
227
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
228 if (data.services)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
229 opts.services = me._parseObject(data.services, typemap);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
230 if (data.inject)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
231 opts.inject = data.inject instanceof Array ? array.map(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
232 data.inject,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
233 function (x) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
234 return me._parseObject(x, typemap);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
235 }) : me._parseObject(data.inject, typemap);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
236 if (data.params)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
237 opts.params = me._parse(data.params, typemap);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
238
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
239 if (data.activation) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
240 if (typeof (data.activation) === "string") {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
241 switch (data.activation.toLowerCase()) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
242 case "singleton":
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
243 opts.activation = Service.SINGLETON;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
244 break;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
245 case "container":
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
246 opts.activation = Service.CONTAINER;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
247 break;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
248 case "hierarchy":
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
249 opts.activation = Service.HIERARCHY;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
250 break;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
251 case "context":
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
252 opts.activation = Service.CONTEXT;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
253 break;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
254 case "call":
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
255 opts.activation = Service.CALL;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
256 break;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
257 default:
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
258 throw new Error("Unknown activation type: " +
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
259 data.activation);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
260 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
261 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
262 opts.activation = Number(data.activation);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
263 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
264 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
265
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
266 if (data.cleanup)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
267 opts.cleanup = data.cleanup;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
268
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
269 return new Service(opts);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
270 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
271
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
272 _parseObject: function (data, typemap) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
273 if (data.constructor &&
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
274 data.constructor.prototype !== Object.prototype)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
275 return new Value(data, true);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
276
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
277 var o = {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
278
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
279 for (var p in data)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
280 o[p] = this._parse(data[p], typemap);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
281
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
282 return o;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
283 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
284
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
285 _parseArray: function (data, typemap) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
286 if (data.constructor &&
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
287 data.constructor.prototype !== Array.prototype)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
288 return new Value(data, true);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
289
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
290 var me = this;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
291 return array.map(data, function (x) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
292 return me._parse(x, typemap);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
293 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
294 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
295
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
296 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
297
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
298 return Container;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
299 });