This repository was archived by the owner on Apr 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathContainer.js
More file actions
490 lines (412 loc) · 16.4 KB
/
Container.js
File metadata and controls
490 lines (412 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import $data from './initializeJayData.js';
import { Guard, Exception } from 'jaydata-error-handler';
import { StringFunctions } from './Extensions.js'
var Container = new ContainerCtor()
export var ContainerInstance = Container
export function ContainerCtor(parentContainer) {
var parent = parentContainer;
if (parent) {
parent.addChildContainer(this);
}
var classNames = {};
var consolidatedClassNames = [];
var classTypes = [];
this.classNames = classNames;
this.consolidatedClassNames = consolidatedClassNames;
this.classTypes = classTypes;
var mappedTo = [];
this.mappedTo = mappedTo;
var self = this;
this["holder"] = null;
var IoC = function(type, parameters) {
var t = self.resolveType(type);
var inst = Object.create(t.prototype);
t.apply(inst, parameters);
return inst;
};
var pendingResolutions = {};
this.pendingResolutions = pendingResolutions;
function addPendingResolution(name, onResolved) {
pendingResolutions[name] = pendingResolutions[name] || [];
pendingResolutions[name].push(onResolved);
}
this.addChildContainer = function(container) {
//children.push(container);
}
this.createInstance = function(type, parameters) {
return IoC(type, parameters);
};
this.mapType = function(aliasTypeOrName, realTypeOrName) {
Guard.requireValue("aliasType", aliasTypeOrName);
Guard.requireValue("realType", realTypeOrName);
var aliasT = this.getType(aliasTypeOrName);
var realT = this.getType(realTypeOrName);
var aliasPos = classTypes.indexOf(aliasT);
var realPos = classTypes.indexOf(realT);
mappedTo[aliasPos] = realPos;
},
//this.resolve = function (type, parameters) {
// var classFunction = this.resolveType(type, parameters);
// return new classFunction(parameters);
//};
this.isPrimitiveType = function(type) {
var t = this.resolveType(type);
switch (true) {
case t === Number:
case t === String:
case t === Date:
case t === Boolean:
case t === Array:
case t === Object:
case t === $data.Number:
case t === $data.Integer:
case t === $data.Date:
case t === $data.String:
case t === $data.Boolean:
case t === $data.Array:
case t === $data.Object:
case t === $data.Guid:
case t === $data.Byte:
case t === $data.SByte:
case t === $data.Decimal:
case t === $data.Float:
case t === $data.Int16:
case t === $data.Int32:
case t === $data.Int64:
case t === $data.DateTimeOffset:
case t === $data.Time:
case t === $data.Day:
case t === $data.Duration:
case t === $data.SimpleBase:
case t === $data.Geospatial:
case t === $data.GeographyBase:
case t === $data.GeographyPoint:
case t === $data.GeographyLineString:
case t === $data.GeographyPolygon:
case t === $data.GeographyMultiPoint:
case t === $data.GeographyMultiLineString:
case t === $data.GeographyMultiPolygon:
case t === $data.GeographyCollection:
case t === $data.GeometryBase:
case t === $data.GeometryPoint:
case t === $data.GeometryLineString:
case t === $data.GeometryPolygon:
case t === $data.GeometryMultiPoint:
case t === $data.GeometryMultiLineString:
case t === $data.GeometryMultiPolygon:
case t === $data.GeometryCollection:
return true;
default:
return false;
}
};
this.resolveName = function(type) {
var t = this.resolveType(type);
var tPos = classTypes.indexOf(t);
return consolidatedClassNames[tPos];
};
this.resolveType = function(typeOrName, onResolved) {
var t = typeOrName;
t = this.getType(t, onResolved ? true : false, onResolved);
var posT = classTypes.indexOf(t);
return typeof mappedTo[posT] === 'undefined' ? t : classTypes[mappedTo[posT]];
};
this.getType = function(typeOrName, doNotThrow, onResolved) {
Guard.requireValue("typeOrName", typeOrName);
if (typeof typeOrName === 'function') {
return typeOrName;
};
if (!(typeOrName in classNames)) {
if (parent) {
var tp = parent.getType(typeOrName, true);
if (tp) return tp;
}
if (onResolved) {
addPendingResolution(typeOrName, onResolved);
return;
} else if (doNotThrow) {
return undefined;
} else {
Guard.raise(new Exception("Unable to resolve type:" + typeOrName));
}
};
var result = classTypes[classNames[typeOrName]];
if (onResolved) {
onResolved(result);
}
return result;
};
this.getName = function(typeOrName) {
var t = this.getType(typeOrName);
var tPos = classTypes.indexOf(t);
if (tPos == -1)
Guard.raise("unknown type to request name for: " + typeOrName);
return consolidatedClassNames[tPos];
};
this.getTypes = function() {
var keys = Object.keys(classNames);
var ret = [];
for (var i = 0; i < keys.length; i++) {
var className = keys[i];
ret.push({
name: className,
type: classTypes[classNames[className]],
toString: function() {
return this.name;
}
});
}
return ret;
};
//this.getTypeName( in type);
//this.resolveType()
//this.inferTypeFromValue = function (value) {
this.getTypeName = function(value) {
//TODO refactor
switch (typeof value) {
case 'object':
if (value == null) return '$data.Object';
if (value instanceof Array) return '$data.Array';
if (value.getType) return value.getType().fullName;
if (value instanceof Date) return '$data.Date';
if (value instanceof $data.Guid) return '$data.Guid';
if (value instanceof $data.DateTimeOffset) return '$data.DateTimeOffset';
if (value instanceof $data.GeographyPoint) return '$data.GeographyPoint';
if (value instanceof $data.GeographyLineString) return '$data.GeographyLineString';
if (value instanceof $data.GeographyPolygon) return '$data.GeographyPolygon';
if (value instanceof $data.GeographyMultiPoint) return '$data.GeographyMultiPoint';
if (value instanceof $data.GeographyMultiLineString) return '$data.GeographyMultiLineString';
if (value instanceof $data.GeographyMultiPolygon) return '$data.GeographyMultiPolygon';
if (value instanceof $data.GeographyCollection) return '$data.GeographyCollection';
if (value instanceof $data.GeographyBase) return '$data.GeographyBase';
if (value instanceof $data.GeometryPoint) return '$data.GeometryPoint';
if (value instanceof $data.GeometryLineString) return '$data.GeometryLineString';
if (value instanceof $data.GeometryPolygon) return '$data.GeometryPolygon';
if (value instanceof $data.GeometryMultiPoint) return '$data.GeometryMultiPoint';
if (value instanceof $data.GeometryMultiLineString) return '$data.GeometryMultiLineString';
if (value instanceof $data.GeometryMultiPolygon) return '$data.GeometryMultiPolygon';
if (value instanceof $data.GeometryCollection) return '$data.GeometryCollection';
if (value instanceof $data.GeometryBase) return '$data.GeometryBase';
if (value instanceof $data.Geospatial) return '$data.Geospatial';
if (value instanceof $data.SimpleBase) return '$data.SimpleBase';
if (typeof value.toHexString === 'function') return '$data.ObjectID';
//if(value instanceof "number") return
default:
return typeof value;
}
};
this.isTypeRegistered = function(typeOrName) {
if (typeof typeOrName === 'function') {
return classTypes.indexOf(typeOrName) > -1;
} else {
return typeOrName in classNames;
}
};
this.unregisterType = function(type) {
Guard.raise("Unimplemented");
};
this.getDefault = function(typeOrName) {
var t = this.resolveType(typeOrName);
switch (t) {
case $data.Number:
return 0.0;
case $data.Float:
return 0.0;
case $data.Decimal:
return '0.0';
case $data.Integer:
return 0;
case $data.Int16:
return 0;
case $data.Int32:
return 0;
case $data.Int64:
return '0';
case $data.Byte:
return 0;
case $data.SByte:
return 0;
case $data.String:
return null;
case $data.Boolean:
return false;
default:
return null;
}
};
//name array ['', '', '']
this.getIndex = function(typeOrName) {
var t = this.resolveType(typeOrName);
return classTypes.indexOf(t);
}
this.resolveByIndex = function(index) {
return classTypes[index];
}
this.registerType = function(nameOrNamesArray, type, factoryFunc) {
///<signature>
///<summary>Registers a type and optionally a lifetimeManager with a name
///that can be used to later resolve the type or create new instances</summary>
///<param name="nameOrNamesArray" type="Array">The names of the type</param>
///<param name="type" type="Function">The type to register</param>
///<param name="instanceManager" type="Function"></param>
///</signature>
///<signature>
///<summary>Registers a new type that </summary>
///<param name="aliasType" type="Function">The name of the type</param>
///<param name="actualType" type="Function">The type to register</param>
///</signature>
///TODO remove
/*if (typeof typeNameOrAlias === 'string') {
if (classNames.indexOf(typeNameOrAlias) > -1) {
Guard.raise("Type already registered. Remove first");
}
}*/
if (!nameOrNamesArray) {
return;
}
//todo add ('number', 'number')
if (typeof type === "string") {
type = self.resolveType(type);
}
var namesArray = [];
if (typeof nameOrNamesArray === 'string') {
var tmp = [];
tmp.push(nameOrNamesArray);
namesArray = tmp;
} else {
namesArray = nameOrNamesArray;
}
for (var i = 0; i < namesArray.length; i++) {
var parts = namesArray[i].split('.');
var item = {};
item.shortName = parts[parts.length - 1];
item.fullName = namesArray[i];
namesArray[i] = item;
}
//if (type.
var creatorFnc = function() {
return IoC(type, arguments);
};
if (typeof intellisense !== 'undefined') {
intellisense.annotate(creatorFnc, type);
}
for (var i = 0, l = namesArray.length; i < l; i++) {
var item = namesArray[i];
if (!(("create" + item.shortName) in self)) {
if (typeof factoryFunc === 'function') {
self["create" + item.shortName] = factoryFunc;
} else {
self["create" + item.shortName] = creatorFnc;
}
}
var typePos = classTypes.indexOf(type);
if (typePos == -1) {
//new type
typePos = classTypes.push(type) - 1;
var fn = item.fullName;
consolidatedClassNames[typePos] = item.fullName;
};
classNames[item.fullName] = typePos;
var pending = pendingResolutions[item.fullName] || [];
if (pending.length > 0) {
pending.forEach(function(t) {
t(type);
});
pendingResolutions[item.fullName] = [];
}
}
if (parent) {
parent.registerType.apply(parent, arguments);
}
if (!type.name) {
try{ type.name = namesArray[0].shortName; }catch(err){ }
}
};
var _converters = {
from: {},
to: {}
};
this.converters = _converters;
this.convertTo = function(value, tType, eType /*if Array*/ , options) {
Guard.requireValue("typeOrName", tType);
if (Guard.isNullOrUndefined(value))
return value;
var sourceTypeName = Container.getTypeName(value);
var sourceType = Container.resolveType(sourceTypeName);
var sourceTypeName = Container.resolveName(sourceType);
var targetType = Container.resolveType(tType);
var targetTypeName = Container.resolveName(targetType);
var result;
try {
if (typeof targetType['from' + sourceTypeName] === 'function') {
// target from
result = targetType['from' + sourceTypeName].apply(targetType, arguments);
} else if (typeof sourceType['to' + targetTypeName] === 'function') {
// source to
result = sourceType['to' + targetTypeName].apply(sourceType, arguments);
} else if (_converters.to[targetTypeName] && _converters.to[targetTypeName][sourceTypeName]) {
// target from source
result = _converters.to[targetTypeName][sourceTypeName].apply(_converters, arguments);
} else if (_converters.from[sourceTypeName] && _converters.from[sourceTypeName][targetTypeName]) {
// source to target
result = _converters.from[sourceTypeName][targetTypeName].apply(_converters, arguments);
} else if (targetTypeName === sourceTypeName || value instanceof targetType) {
result = value;
} else if (_converters.to[targetTypeName] && _converters.to[targetTypeName]['default']) {
// target from anything
result = _converters.to[targetTypeName]['default'].apply(_converters, arguments);
} else {
throw "converter not found";
}
} catch (e) {
Guard.raise(new Exception("Value '" + sourceTypeName + "' not convertable to '" + targetTypeName + "'", 'TypeError', value));
}
if (targetType === $data.Array && eType && Array.isArray(result)) {
for (var i = 0; i < result.length; i++) {
result[i] = this.convertTo.call(this, result[i], eType, undefined, options);
}
}
return result;
};
this.registerConverter = function(target, sourceOrToConverters, toConverterOrFromConverters, fromConverter) {
//registerConverter($data.Guid, { $data.String: fn, int: fn }, { string: fn, int:fn })
//registerConverter($data.Guid, $data.String, fn, fn);
var targetName = Container.resolveName(target);
if (Container.isTypeRegistered(sourceOrToConverters)) {
//isSource
_converters.to[targetName] = _converters.to[targetName] || {};
_converters.from[targetName] = _converters.from[targetName] || {};
var sourceName = Container.resolveName(sourceOrToConverters);
if (toConverterOrFromConverters)
_converters.to[targetName][sourceName] = toConverterOrFromConverters;
if (fromConverter)
_converters.from[targetName][sourceName] = fromConverter;
} else {
// converterGroup
//fromConverters
if (_converters.to[targetName]) {
_converters.to[targetName] = $data.typeSystem.extend(_converters.to[targetName], sourceOrToConverters);
} else {
_converters.to[targetName] = sourceOrToConverters;
}
//toConverters
if (_converters.from[targetName]) {
_converters.from[targetName] = $data.typeSystem.extend(_converters.from[targetName], toConverterOrFromConverters);
} else {
_converters.from[targetName] = toConverterOrFromConverters;
}
}
};
this.createOrGetNamespace = function(parts, root) {
for (var i = 0; i < parts.length; i++) {
var part = parts[i];
if (!root[part]) {
var ns = {};
ns.__namespace = true;
root[part] = ns;
}
root = root[part];
}
return root;
};
}