Mercurial > pub > ImplabJs
annotate src/implab/di/ActivationContext.js @ 24:f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
author | cin |
---|---|
date | Thu, 07 Dec 2017 19:19:29 +0300 |
parents | 00779cb63b12 |
children |
rev | line source |
---|---|
0 | 1 define([ |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
2 "../declare", |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
3 "../safe", |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
4 "./Descriptor", |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
5 "./ValueDescriptor", |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
6 "../log/trace!" |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
7 ], function (declare, safe, Descriptor, Value, trace) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
8 var Context = declare(null, { |
0 | 9 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
10 _cache: null, |
0 | 11 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
12 _services: null, |
0 | 13 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
14 _stack: null, |
0 | 15 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
16 _visited: null, |
0 | 17 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
18 container: null, |
0 | 19 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
20 _trace: true, |
0 | 21 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
22 constructor: function (container, services, cache, visited) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
23 safe.argumentNotNull(container, "container"); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
24 safe.argumentNotNull(services, "services"); |
0 | 25 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
26 this._visited = visited || {}; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
27 this._stack = []; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
28 this._cache = cache || {}; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
29 this._services = services; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
30 this.container = container; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
31 }, |
0 | 32 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
33 getService: function (name, def) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
34 var d = this._services[name]; |
0 | 35 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
36 if (!d) |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
37 if (arguments.length > 1) |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
38 return def; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
39 else |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
40 throw new Error("Service '" + name + "' not found"); |
0 | 41 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
42 return d.activate(this, name); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
43 }, |
0 | 44 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
45 /** |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
46 * registers services local to the the activation context |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
47 * |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
48 * @name{string} the name of the service |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
49 * @service{string} the service descriptor to register |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
50 */ |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
51 register: function (name, service) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
52 safe.argumentNotEmptyString(name, "name"); |
0 | 53 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
54 if (!(service instanceof Descriptor)) |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
55 service = new Value(service, true); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
56 this._services[name] = service; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
57 }, |
0 | 58 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
59 clone: function () { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
60 return new Context( |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
61 this.container, |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
62 Object.create(this._services), |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
63 this._cache, |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
64 this._visited |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
65 ); |
0 | 66 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
67 }, |
0 | 68 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
69 has: function (id) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
70 return id in this._cache; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
71 }, |
0 | 72 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
73 get: function (id) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
74 return this._cache[id]; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
75 }, |
0 | 76 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
77 store: function (id, value) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
78 return (this._cache[id] = value); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
79 }, |
0 | 80 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
81 parse: function (data, name) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
82 var me = this; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
83 if (safe.isPrimitive(data)) |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
84 return data; |
0 | 85 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
86 if (data instanceof Descriptor) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
87 return data.activate(this, name); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
88 } else if (data instanceof Array) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
89 me.enter(name); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
90 var v = data.map(function (x, i) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
91 return me.parse(x, "." + i); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
92 }); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
93 me.leave(); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
94 return v; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
95 } else { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
96 me.enter(name); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
97 var result = {}; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
98 for (var p in data) |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
99 result[p] = me.parse(data[p], "." + p); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
100 me.leave(); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
101 return result; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
102 } |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
103 }, |
0 | 104 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
105 visit: function (id) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
106 var count = this._visited[id] || 0; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
107 this._visited[id] = count + 1; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
108 return count; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
109 }, |
0 | 110 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
111 getStack: function () { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
112 return this._stack.slice().reverse(); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
113 }, |
0 | 114 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
115 enter: function (name, d, localize) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
116 if (this._trace) |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
117 trace.log("enter " + name + " " + (d || "") + |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
118 (localize ? " localize" : "")); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
119 this._stack.push({ |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
120 name: name, |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
121 service: d, |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
122 scope: this._services |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
123 }); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
124 if (localize) |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
125 this._services = Object.create(this._services); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
126 }, |
0 | 127 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
128 leave: function () { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
129 var ctx = this._stack.pop(); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
130 this._services = ctx.scope; |
0 | 131 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
132 if (this._trace) |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
133 trace.log("leave " + ctx.name + " " + (ctx.service || "")); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
134 } |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
135 }); |
0 | 136 |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
137 return Context; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
3
diff
changeset
|
138 }); |