@@ -27,10 +27,9 @@ class StratusApp {
27
27
this.token = token;
28
28
this.classes = {};
29
29
this.components = {};
30
- this.frontCallsResultsBuffer = {};
31
30
this.debug = false;
32
31
this.rootElement = document;
33
- this.httpRequests = [] ;
32
+ this.bus = null ;
34
33
}
35
34
36
35
getClass(id) {
@@ -49,61 +48,12 @@ class StratusApp {
49
48
this.components[component.id] = component;
50
49
}
51
50
52
- getNewXMLHttpRequest() {
53
- const xhr = new XMLHttpRequest();
54
- xhr.lastResponseLen = 0;
55
-
56
- xhr.onprogress = this._onprogress;
57
- xhr.onreadystatechange = this._onreadystatechange;
58
-
59
- xhr.open('POST', this.controller, true);
60
- xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
61
-
62
- return xhr;
63
- }
64
-
65
- _onprogress(event) {
66
- if (! event.currentTarget) return;
67
-
68
- let currentResponse = null;
69
- let responseBuffer = event.currentTarget.response;
70
-
71
- if (this.lastResponseLen === false) {
72
- currentResponse = responseBuffer;
73
- this.lastResponseLen = responseBuffer.length;
74
- } else {
75
- currentResponse = responseBuffer.substring(this.lastResponseLen);
76
- this.lastResponseLen = responseBuffer.length;
77
- }
78
-
79
- if ('string' === typeof(currentResponse)) {
80
- stratusAppInstance.processMessage(currentResponse, this);
81
- }
51
+ setBus(bus) {
52
+ this.bus = bus;
82
53
}
83
54
84
- _onreadystatechange() {
85
- const xhr = this;
86
-
87
- if (xhr.readyState === XMLHttpRequest.DONE) {
88
- stratusAppInstance.httpRequests.splice(
89
- stratusAppInstance.httpRequests.indexOf(xhr), 1
90
- );
91
- }
92
- }
93
-
94
- _stringifyReplacer(key, value) {
95
- let result = value;
96
-
97
- if (value instanceof NamedNodeMap) {
98
- result = {};
99
-
100
- for (let attr of value) {
101
- let attrName = attr.nodeName;
102
- result[attrName] = attr.ownerElement.getAttribute(attrName);
103
- }
104
- }
105
-
106
- return result;
55
+ getBus() {
56
+ return this.bus;
107
57
}
108
58
109
59
getComponentData() {
@@ -122,86 +72,7 @@ class StratusApp {
122
72
}
123
73
124
74
dispatch(eventName, eventData = {}, capture = false) {
125
- const xhr = this.getNewXMLHttpRequest();
126
- const componentData = this.getComponentData();
127
-
128
- const data = {
129
- token: this.token,
130
- componentData,
131
- eventName,
132
- eventData,
133
- capture,
134
- };
135
-
136
- this.sendRequest(xhr, data);
137
- }
138
-
139
- sendRequest(xhr, data) {
140
- xhr.data = data;
141
- xhr.send('stratus_request=' + JSON.stringify(data, this._stringifyReplacer));
142
- this.httpRequests.push(xhr);
143
- this.frontCallsResultsBuffer = {};
144
- }
145
-
146
- processMessage(text, xhr) {
147
- if ('string' !== typeof(text)) {
148
- return;
149
- }
150
-
151
- let lines = text.split('%SSS%');
152
- for (let id in lines) {
153
- let line = lines[id];
154
-
155
- line = line.trim();
156
-
157
- if (! line.length) {
158
- continue;
159
- }
160
-
161
- let message = JSON.parse(line);
162
-
163
- if (this.debug) {
164
- console.log('Message:', message);
165
- }
166
-
167
- if ('object' === typeof message.handler) {
168
- let HandlerClass = this.classes[message.handler.classId];
169
- let handler = HandlerClass[message.handler.method];
170
-
171
- handler.apply(null, Object.values(message.data));
172
- }
173
-
174
- if ('object' === typeof message.frontCall) {
175
- let frontCallResult = eval('(function() {' + message.frontCall.script + '})()');
176
-
177
- this.frontCallsResultsBuffer[message.frontCall.hash] = frontCallResult ? frontCallResult : '';
178
- }
179
-
180
- if ('boolean' === typeof(message.resend) &&
181
- true === message.resend
182
- ) {
183
- if (this.debug) {
184
- console.log('The current request should be sent again.');
185
- }
186
-
187
- let data = xhr.data;
188
-
189
- if ('object' !== typeof data.executedFrontCalls) {
190
- data.executedFrontCalls = {};
191
- }
192
-
193
- if ('object' === typeof message.executedFrontCalls) {
194
- Object.assign(data.executedFrontCalls, message.executedFrontCalls);
195
- }
196
-
197
- Object.assign(data.executedFrontCalls, this.frontCallsResultsBuffer);
198
-
199
- data.componentData = this.getComponentData();
200
-
201
- let newXhr = this.getNewXMLHttpRequest();
202
- this.sendRequest(newXhr, data);
203
- }
204
- }
75
+ this.bus.dispatch(eventName, eventData, capture);
205
76
}
206
77
}
207
78
JAVASCRIPT ;
0 commit comments