comparison core/src/js/di/ReferenceDescriptor.js @ 34:27e8e9e38e07 default tip

Слияние
author nickolay
date Wed, 05 Jun 2019 20:44:15 +0300
parents acdcdf1a8d21
children
comparison
equal deleted inserted replaced
33:8af8e840dd49 34:27e8e9e38e07
1 define([
2 "../declare", "../safe", "./Descriptor", "./ActivationError", "./ValueDescriptor"
3 ],
4
5 function(declare, safe, Descriptor, ActivationError, Value) {
6 return declare(Descriptor, {
7 _name : null,
8 _lazy : false,
9 _optional : false,
10 _default : undefined,
11
12 constructor : function(name, lazy, optional, def, services) {
13 safe.argumentNotEmptyString(name, "name");
14 this._name = name;
15 this._lazy = Boolean(lazy);
16 this._optional = Boolean(optional);
17 this._default = def;
18 this._services = services;
19 },
20
21 activate : function(context, name) {
22 var me = this;
23
24 context.enter(name, this, true);
25
26 // добавляем сервисы
27 if (me._services) {
28 for ( var p in me._services) {
29 var sv = me._services[p];
30 context.register(p, sv instanceof Descriptor ? sv : new Value(sv, false));
31 }
32 }
33
34 if (me._lazy) {
35 // сохраняем контекст активации
36 context = context.clone();
37 return function(cfg) {
38 // защищаем контекст на случай исключения в процессе
39 // активации
40 var ct = context.clone();
41 try {
42 if (cfg)
43 safe.each(cfg, function(v, k) {
44 ct.register(k, v instanceof Descriptor ? v : new Value(v, false));
45 });
46 return me._optional ? ct.getService(me._name, me._default) : ct
47 .getService(me._name);
48 } catch (error) {
49 throw new ActivationError(me._name, ct.getStack(), error);
50 }
51 };
52 }
53
54 var v = me._optional ? context.getService(me._name, me._default) : context
55 .getService(me._name);
56 context.leave(me);
57 return v;
58 },
59
60 isInstanceCreated : function() {
61 return false;
62 },
63
64 toString : function() {
65 var opts = [];
66 if (this._optional)
67 opts.push("optional");
68 if (this._lazy)
69 opts.push("lazy");
70
71 var parts = [
72 "@ref "
73 ];
74 if (opts.length) {
75 parts.push("{");
76 parts.push(opts.join());
77 parts.push("} ");
78 }
79
80 parts.push(this._name);
81
82 if (!safe.isNull(this._default)) {
83 parts.push(" = ");
84 parts.push(this._default);
85 }
86
87 return parts.join("");
88 }
89 });
90 });