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