annotate src/implab/di/ServiceDescriptor.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 [
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
3 "dojo/_base/declare",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
4 "../safe",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
5 "dojo/_base/lang",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
6 "dojo/_base/array",
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 ],
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
10
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
11 function (declare, safe, lang, array, Descriptor, Value) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
12 var SINGLETON_ACTIVATION = 1,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
13 CONTAINER_ACTIVATION = 2,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
14 CONTEXT_ACTIVATION = 3,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
15 CALL_ACTIVATION = 4,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
16 HIERARCHY_ACTIVATION = 5;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
17
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
18 var injectMethod = function (target, method, context, args) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
19 var m = target[method];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
20 if (!m)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
21 throw new Error("Method '" + method + "' not found");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
22
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
23 if (args instanceof Array)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
24 m.apply(target, context.parse(args, "." + method));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
25 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
26 m.call(target, context.parse(args, "." + method));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
27 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
28
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
29 var makeClenupCallback = function (target, method) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
30 if (typeof (method) === "string") {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
31 return function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
32 target[method]();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
33 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
34 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
35 return function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
36 method(target);
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 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
40
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
41 var cacheId = 0;
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 var cls = declare(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
44 Descriptor, {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
45 _instance: null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
46 _hasInstance: false,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
47 _activationType: CALL_ACTIVATION,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
48 _services: null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
49 _type: null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
50 _typeMap: null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
51 _factory: null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
52 _params: undefined,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
53 _inject: null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
54 _cleanup: null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
55 _cacheId: null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
56 _owner: null,
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 constructor: function (opts) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
59 safe.argumentNotNull(opts, "opts");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
60 safe.argumentNotNull(opts.owner, "opts.owner");
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 this._owner = opts.owner;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
63
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
64 if (!(opts.type || opts.factory))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
65 throw new Error(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
66 "Either a type or a factory must be specified");
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 if (typeof (opts.type) === "string" && !opts.typeMap)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
69 throw new Error(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
70 "The typeMap is required when the type is specified by its name");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
71
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
72 if (opts.activation)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
73 this._activationType = opts.activation;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
74 if (opts.type)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
75 this._type = opts.type;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
76 if (opts.params)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
77 this._params = opts.params;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
78 if (opts.inject)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
79 this._inject = opts.inject instanceof Array ? opts.inject : [opts.inject];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
80 if (opts.services)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
81 this._services = opts.services;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
82 if (opts.factory)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
83 this._factory = opts.factory;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
84 if (opts.typeMap)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
85 this._typeMap = opts.typeMap;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
86 if (opts.cleanup) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
87 if (!(typeof (opts.cleanup) === "string" || opts.cleanup instanceof Function))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
88 throw new Error(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
89 "The cleanup parameter must be either a function or a function name");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
90
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
91 this._cleanup = opts.cleanup;
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 this._cacheId = ++cacheId;
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 activate: function (context, name) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
98
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
99 // if we have a local service records, register them first
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
100
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
101 var instance;
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 switch (this._activationType) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
104 case 1: // SINGLETON
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
105 // if the value is cached return it
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
106 if (this._hasInstance)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
107 return this._instance;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
108
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
109 var tof = this._type || this._factory;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
110
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
111 // create the persistent cache identifier for the type
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
112 if (safe.isPrimitive(tof))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
113 this._cacheId = this._type;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
114 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
115 this._cacheId = safe.oid(tof);
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 // singletons are bound to the root container
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
118 var container = context.container.getRootContainer();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
119
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
120 if (container.has(this._cacheId)) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
121 instance = container.get(this._cacheId);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
122 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
123 instance = this._create(context, name);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
124 container.store(this._cacheId, instance);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
125 if (this._cleanup)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
126 container.onDispose(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
127 makeClenupCallback(instance, this._cleanup));
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
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
130 this._hasInstance = true;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
131 return (this._instance = instance);
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 case 2: // CONTAINER
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
134 //return a cached value
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
135 if (this._hasInstance)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
136 return this._instance;
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 // create an instance
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
139 instance = this._create(context, name);
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 // the instance is bound to the container
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
142 if (this._cleanup)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
143 this._owner.onDispose(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
144 makeClenupCallback(instance, this._cleanup));
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 // cache and return the instance
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
147 this._hasInstance = true;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
148 return (this._instance = instance);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
149 case 3: // CONTEXT
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
150 //return a cached value if one exists
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
151 if (context.has(this._cacheId))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
152 return context.get(this._cacheId);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
153 // context context activated instances are controlled by callers
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
154 return context.store(this._cacheId, this._create(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
155 context,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
156 name));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
157 case 4: // CALL
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
158 // per-call created instances are controlled by callers
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
159 return this._create(context, name);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
160 case 5: // HIERARCHY
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
161 // hierarchy activated instances are behave much like container activated
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
162 // except they are created and bound to the child container
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
163
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
164 // return a cached value
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
165 if (context.container.has(this._cacheId))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
166 return context.container.get(this._cacheId);
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 instance = this._create(context, name);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
169
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
170 if (this._cleanup)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
171 context.container.onDispose(makeClenupCallback(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
172 instance,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
173 this._cleanup));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
174
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
175 return context.container.store(this._cacheId, instance);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
176 default:
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
177 throw "Invalid activation type: " + this._activationType;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
178 }
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
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
181 isInstanceCreated: function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
182 return this._hasInstance;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
183 },
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 getInstance: function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
186 return this._instance;
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
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
189 _create: function (context, name) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
190 context.enter(name, this, Boolean(this._services));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
191
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
192 if (this._activationType != CALL_ACTIVATION &&
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
193 context.visit(this._cacheId) > 0)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
194 throw new Error("Recursion detected");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
195
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
196 if (this._services) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
197 for (var p in this._services) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
198 var sv = this._services[p];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
199 context.register(p, sv instanceof Descriptor ? sv : new Value(sv, false));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
200 }
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
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
203 var instance;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
204
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
205 if (!this._factory) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
206 var ctor, type = this._type;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
207
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
208 if (typeof (type) === "string") {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
209 ctor = this._typeMap[type];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
210 if (!ctor)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
211 throw new Error("Failed to resolve the type '" +
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
212 type + "'");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
213 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
214 ctor = type;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
215 }
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 (this._params === undefined) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
218 this._factory = function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
219 return new ctor();
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 } else if (this._params instanceof Array) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
222 this._factory = function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
223 var inst = lang.delegate(ctor.prototype);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
224 var ret = ctor.apply(inst, arguments);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
225 return typeof (ret) === "object" ? ret : inst;
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 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
228 this._factory = function (param) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
229 return new ctor(param);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
230 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
231 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
232 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
233
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
234 if (this._params === undefined) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
235 instance = this._factory();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
236 } else if (this._params instanceof Array) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
237 instance = this._factory.apply(this, context.parse(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
238 this._params,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
239 ".params"));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
240 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
241 instance = this._factory(context.parse(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
242 this._params,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
243 ".params"));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
244 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
245
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
246 if (this._inject) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
247 array.forEach(this._inject, function (spec) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
248 for (var m in spec)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
249 injectMethod(instance, m, context, spec[m]);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
250 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
251 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
252
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
253 context.leave();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
254
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
255 return instance;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
256 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
257
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
258 // @constructor {singleton} foo/bar/Baz
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
259 // @factory {singleton}
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
260 toString: function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
261 var parts = [];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
262
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
263 parts.push(this._type ? "@constructor" : "@factory");
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 parts.push(activationNames[this._activationType]);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
266
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
267 if (typeof (this._type) === "string")
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
268 parts.push(this._type);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
269
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
270 return parts.join(" ");
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
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
273 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
274
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
275 cls.SINGLETON = SINGLETON_ACTIVATION;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
276 cls.CONTAINER = CONTAINER_ACTIVATION;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
277 cls.CONTEXT = CONTEXT_ACTIVATION;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
278 cls.CALL = CALL_ACTIVATION;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
279 cls.HIERARCHY = HIERARCHY_ACTIVATION;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
280
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
281 var activationNames = [
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
282 "",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
283 "{singleton}",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
284 "{container}",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
285 "{context}",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
286 "{call}",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
287 "{hierarchy}"
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
288 ];
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 return cls;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
291 });