annotate core/src/js/di/Container.js @ 31:40d2e49a7219

working on dependencies
author cin
date Wed, 27 Jun 2018 04:31:39 +0300
parents acdcdf1a8d21
children
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",
1
93fb6c09f2e1 minor fixes
cin
parents: 0
diff changeset
4 "../Uuid",
93fb6c09f2e1 minor fixes
cin
parents: 0
diff changeset
5 "../Deferred",
0
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 safe,
1
93fb6c09f2e1 minor fixes
cin
parents: 0
diff changeset
15 Uuid,
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
16 Deferred,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
17 ActivationContext,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
18 Descriptor,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
19 Value,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
20 Reference,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
21 Service,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
22 ActivationError) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
23 var Container = declare(null, {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
24 _services: null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
25 _cache: null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
26 _cleanup: null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
27 _root: null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
28 _parent: null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
29
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
30 constructor: function (parent) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
31 this._parent = parent;
3
00779cb63b12 formatting
cin
parents: 1
diff changeset
32 this._services = parent ? Object.create(parent._services) : {};
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
33 this._cache = {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
34 this._cleanup = [];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
35 this._root = parent ? parent.getRootContainer() : this;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
36 this._services.container = new Value(this, true);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
37 },
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 getRootContainer: function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
40 return this._root;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
41 },
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 getParent: function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
44 return this._parent;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
45 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
46
1
93fb6c09f2e1 minor fixes
cin
parents: 0
diff changeset
47 /**
93fb6c09f2e1 minor fixes
cin
parents: 0
diff changeset
48 *
93fb6c09f2e1 minor fixes
cin
parents: 0
diff changeset
49 */
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
50 getService: function (name, def) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
51 var d = this._services[name];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
52 if (!d)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
53 if (arguments.length > 1)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
54 return def;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
55 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
56 throw new Error("Service '" + name + "' isn't found");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
57 if (d.isInstanceCreated())
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
58 return d.getInstance();
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 var context = new ActivationContext(this, this._services);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
61
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
62 try {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
63 return d.activate(context, name);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
64 } catch (error) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
65 throw new ActivationError(name, context.getStack(), error);
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 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
68
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
69 register: function (name, service) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
70 if (arguments.length == 1) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
71 var data = name;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
72 for (name in data)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
73 this.register(name, data[name]);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
74 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
75 if (!(service instanceof Descriptor))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
76 service = new Value(service, true);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
77 this._services[name] = service;
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 return this;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
80 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
81
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
82 onDispose: function (callback) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
83 if (!(callback instanceof Function))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
84 throw new Error("The callback must be a function");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
85 this._cleanup.push(callback);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
86 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
87
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
88 dispose: function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
89 if (this._cleanup) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
90 for (var i = 0; i < this._cleanup.length; i++)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
91 this._cleanup[i].call(null);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
92 this._cleanup = null;
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
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
96 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
97 * @param{String|Object} config
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
98 * 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
99 * 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
100 * services which will be registed in the contaier.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
101 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
102 * @param{Function} opts.contextRequire
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
103 * 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
104 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
105 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
106 configure: function (config, opts) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
107 var p, me = this,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
108 contextRequire = (opts && opts.contextRequire);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
109
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
110 if (typeof (config) === "string") {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
111 p = new Deferred();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
112 if (!contextRequire) {
4
fcc63f34d0a2 fixed container configuration when the config path contains only a package name
cin
parents: 3
diff changeset
113 var shim = [config, new Uuid()].join(config.indexOf("/") != -1 ? "-" : "/");
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
114 define(shim, ["require", config], function (ctx, data) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
115 p.resolve([data, {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
116 contextRequire: ctx
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
117 }]);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
118 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
119 require([shim]);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
120 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
121 // TODO how to get correct contextRequire for the relative config module?
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
122 contextRequire([config], function (data) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
123 p.resolve([data, {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
124 contextRequire: contextRequire
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 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
128
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
129 return p.then(function (args) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
130 return me._configure.apply(me, args);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
131 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
132 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
133 return me._configure(config, opts);
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 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
136
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
137 createChildContainer: function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
138 return new Container(this);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
139 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
140
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
141 has: function (id) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
142 return id in this._cache;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
143 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
144
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
145 get: function (id) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
146 return this._cache[id];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
147 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
148
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
149 store: function (id, value) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
150 return (this._cache[id] = value);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
151 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
152
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
153 _configure: function (data, opts) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
154 var typemap = {},
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
155 d = new Deferred(),
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
156 me = this,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
157 p,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
158 contextRequire = (opts && opts.contextRequire) || require;
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 var services = {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
161
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
162 for (p in data) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
163 var service = me._parse(data[p], typemap);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
164 if (!(service instanceof Descriptor))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
165 service = new Value(service, false);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
166 services[p] = service;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
167 }
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 me.register(services);
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 var names = [];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
172
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
173 for (p in typemap)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
174 names.push(p);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
175
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
176 if (names.length) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
177 contextRequire(names, function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
178 for (var i = 0; i < names.length; i++)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
179 typemap[names[i]] = arguments[i];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
180 d.resolve(me);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
181 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
182 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
183 d.resolve(me);
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 return d.promise;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
186 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
187
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
188 _parse: function (data, typemap) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
189 if (safe.isPrimitive(data) || data instanceof Descriptor)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
190 return data;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
191 if (data.$dependency)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
192 return new Reference(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
193 data.$dependency,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
194 data.lazy,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
195 data.optional,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
196 data["default"],
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
197 data.services && this._parseObject(data.services, typemap));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
198 if (data.$value) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
199 var raw = !data.parse;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
200 return new Value(raw ? data.$value : this._parse(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
201 data.$value,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
202 typemap), raw);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
203 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
204 if (data.$type || data.$factory)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
205 return this._parseService(data, typemap);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
206 if (data instanceof Array)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
207 return this._parseArray(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 return this._parseObject(data, typemap);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
210 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
211
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
212 _parseService: function (data, typemap) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
213 var me = this,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
214 opts = {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
215 owner: this
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 if (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 opts.type = data.$type;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
220
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
221 if (typeof (data.$type) === "string") {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
222 typemap[data.$type] = null;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
223 opts.typeMap = typemap;
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 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
226
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
227 if (data.$factory)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
228 opts.factory = data.$factory;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
229
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
230 if (data.services)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
231 opts.services = me._parseObject(data.services, typemap);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
232 if (data.inject)
1
93fb6c09f2e1 minor fixes
cin
parents: 0
diff changeset
233 opts.inject = data.inject instanceof Array ? data.inject.map(function (x) {
93fb6c09f2e1 minor fixes
cin
parents: 0
diff changeset
234 return me._parseObject(x, typemap);
93fb6c09f2e1 minor fixes
cin
parents: 0
diff changeset
235 }) : me._parseObject(data.inject, typemap);
0
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;
1
93fb6c09f2e1 minor fixes
cin
parents: 0
diff changeset
291 return data.map(function (x) {
0
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 });