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