annotate src/implab/di/ActivationContext.js @ 0:fc2517695ee1

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