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

Слияние
author nickolay
date Wed, 05 Jun 2019 20:44:15 +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([
1
93fb6c09f2e1 minor fixes
cin
parents: 0
diff changeset
2 "../declare", "../safe", "./Descriptor", "./ActivationError", "./ValueDescriptor"
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
3 ],
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
4
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
5 function(declare, safe, Descriptor, ActivationError, Value) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
6 return declare(Descriptor, {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
7 _name : null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
8 _lazy : false,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
9 _optional : false,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
10 _default : undefined,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
11
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
12 constructor : function(name, lazy, optional, def, services) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
13 safe.argumentNotEmptyString(name, "name");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
14 this._name = name;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
15 this._lazy = Boolean(lazy);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
16 this._optional = Boolean(optional);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
17 this._default = def;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
18 this._services = services;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
19 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
20
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
21 activate : function(context, name) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
22 var me = this;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
23
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
24 context.enter(name, this, true);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
25
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
26 // добавляем сервисы
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
27 if (me._services) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
28 for ( var p in me._services) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
29 var sv = me._services[p];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
30 context.register(p, sv instanceof Descriptor ? sv : new Value(sv, false));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
31 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
32 }
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 if (me._lazy) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
35 // сохраняем контекст активации
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
36 context = context.clone();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
37 return function(cfg) {
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 var ct = context.clone();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
41 try {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
42 if (cfg)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
43 safe.each(cfg, function(v, k) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
44 ct.register(k, v instanceof Descriptor ? v : new Value(v, false));
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 return me._optional ? ct.getService(me._name, me._default) : ct
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
47 .getService(me._name);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
48 } catch (error) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
49 throw new ActivationError(me._name, ct.getStack(), error);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
50 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
51 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
52 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
53
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
54 var v = me._optional ? context.getService(me._name, me._default) : context
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
55 .getService(me._name);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
56 context.leave(me);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
57 return v;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
58 },
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 isInstanceCreated : function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
61 return false;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
62 },
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 toString : function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
65 var opts = [];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
66 if (this._optional)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
67 opts.push("optional");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
68 if (this._lazy)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
69 opts.push("lazy");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
70
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
71 var parts = [
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
72 "@ref "
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
73 ];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
74 if (opts.length) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
75 parts.push("{");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
76 parts.push(opts.join());
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
77 parts.push("} ");
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 parts.push(this._name);
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 if (!safe.isNull(this._default)) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
83 parts.push(" = ");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
84 parts.push(this._default);
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
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
87 return parts.join("");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
88 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
89 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
90 });