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