Skip to content

Commit 40454aa

Browse files
committed
Temp
1 parent 0eefa46 commit 40454aa

File tree

5 files changed

+179
-139
lines changed

5 files changed

+179
-139
lines changed

src/Asset/StratusInitScript.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@ class StratusInitScript extends Script
2020
*/
2121
public function getSource(): string
2222
{
23-
$jsClasses = '';
24-
$jsInstances = '';
23+
$bus = $this->page->getBus();
24+
25+
$jsClasses = <<<JAVASCRIPT
26+
class Bus {
27+
{$bus->getJavaScriptClassMembers()}
28+
};
29+
JAVASCRIPT;
30+
31+
$jsInstances = $bus->getJavaScriptCreateInstanceScript();
2532

2633
foreach ($this->page->getJavaScriptClasses() as $className => $jsClassId) {
2734
$jsClassMembers = call_user_func([$className, 'getJavaScriptClassMembers']);

src/Asset/StratusScript.php

+6-135
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ class StratusApp {
2727
this.token = token;
2828
this.classes = {};
2929
this.components = {};
30-
this.frontCallsResultsBuffer = {};
3130
this.debug = false;
3231
this.rootElement = document;
33-
this.httpRequests = [];
32+
this.bus = null;
3433
}
3534
3635
getClass(id) {
@@ -49,61 +48,12 @@ class StratusApp {
4948
this.components[component.id] = component;
5049
}
5150
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;
8253
}
8354
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;
10757
}
10858
10959
getComponentData() {
@@ -122,86 +72,7 @@ class StratusApp {
12272
}
12373
12474
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);
20576
}
20677
}
20778
JAVASCRIPT;

src/Bus/BusInterface.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33

44
namespace ThenLabs\StratusPHP\Bus;
55

6+
use ThenLabs\StratusPHP\JavaScript\JavaScriptClassInterface;
7+
use ThenLabs\StratusPHP\JavaScript\JavaScriptInstanceInterface;
8+
69
/**
710
* @author Andy Daniel Navarro Taño <[email protected]>
811
*/
9-
interface BusInterface
12+
interface BusInterface extends JavaScriptClassInterface, JavaScriptInstanceInterface
1013
{
1114
/**
1215
* This method should be called before starts to write in the bus.

src/Bus/StreamingBus.php

+159
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,163 @@ public function close()
3333
{
3434
die;
3535
}
36+
37+
public static function getJavaScriptClassMembers(): string
38+
{
39+
return <<<JAVASCRIPT
40+
constructor(app) {
41+
this.app = app;
42+
this.httpRequests = [];
43+
this.frontCallsResultsBuffer = {};
44+
}
45+
46+
dispatch(eventName, eventData = {}, capture = false) {
47+
const xhr = this.getNewXMLHttpRequest();
48+
const componentData = this.app.getComponentData();
49+
50+
const data = {
51+
token: this.app.token,
52+
componentData,
53+
eventName,
54+
eventData,
55+
capture,
56+
};
57+
58+
this.sendRequest(xhr, data);
59+
}
60+
61+
getNewXMLHttpRequest() {
62+
const xhr = new XMLHttpRequest();
63+
xhr.lastResponseLen = 0;
64+
65+
xhr.onprogress = this._onprogress;
66+
xhr.onreadystatechange = this._onreadystatechange;
67+
68+
xhr.open('POST', this.app.controller, true);
69+
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
70+
71+
return xhr;
72+
}
73+
74+
_onprogress(event) {
75+
if (! event.currentTarget) return;
76+
77+
let currentResponse = null;
78+
let responseBuffer = event.currentTarget.response;
79+
80+
if (this.lastResponseLen === false) {
81+
currentResponse = responseBuffer;
82+
this.lastResponseLen = responseBuffer.length;
83+
} else {
84+
currentResponse = responseBuffer.substring(this.lastResponseLen);
85+
this.lastResponseLen = responseBuffer.length;
86+
}
87+
88+
if ('string' === typeof(currentResponse)) {
89+
bus.processMessage(currentResponse, this);
90+
}
91+
}
92+
93+
_onreadystatechange() {
94+
const xhr = this;
95+
96+
if (xhr.readyState === XMLHttpRequest.DONE) {
97+
bus.httpRequests.splice(
98+
bus.httpRequests.indexOf(xhr), 1
99+
);
100+
}
101+
}
102+
103+
sendRequest(xhr, data) {
104+
xhr.data = data;
105+
xhr.send('stratus_request=' + JSON.stringify(data, this._stringifyReplacer));
106+
this.httpRequests.push(xhr);
107+
this.frontCallsResultsBuffer = {};
108+
}
109+
110+
_stringifyReplacer(key, value) {
111+
let result = value;
112+
113+
if (value instanceof NamedNodeMap) {
114+
result = {};
115+
116+
for (let attr of value) {
117+
let attrName = attr.nodeName;
118+
result[attrName] = attr.ownerElement.getAttribute(attrName);
119+
}
120+
}
121+
122+
return result;
123+
}
124+
125+
processMessage(text, xhr) {
126+
if ('string' !== typeof(text)) {
127+
return;
128+
}
129+
130+
let lines = text.split('%SSS%');
131+
for (let id in lines) {
132+
let line = lines[id];
133+
134+
line = line.trim();
135+
136+
if (! line.length) {
137+
continue;
138+
}
139+
140+
let message = JSON.parse(line);
141+
142+
if (this.app.debug) {
143+
console.log('Message:', message);
144+
}
145+
146+
if ('object' === typeof message.handler) {
147+
let HandlerClass = this.app.classes[message.handler.classId];
148+
let handler = HandlerClass[message.handler.method];
149+
150+
handler.apply(null, Object.values(message.data));
151+
}
152+
153+
if ('object' === typeof message.frontCall) {
154+
let frontCallResult = eval('(function() {' + message.frontCall.script + '})()');
155+
156+
this.frontCallsResultsBuffer[message.frontCall.hash] = frontCallResult ? frontCallResult : '';
157+
}
158+
159+
if ('boolean' === typeof(message.resend) &&
160+
true === message.resend
161+
) {
162+
if (this.app.debug) {
163+
console.log('The current request should be sent again.');
164+
}
165+
166+
let data = xhr.data;
167+
168+
if ('object' !== typeof data.executedFrontCalls) {
169+
data.executedFrontCalls = {};
170+
}
171+
172+
if ('object' === typeof message.executedFrontCalls) {
173+
Object.assign(data.executedFrontCalls, message.executedFrontCalls);
174+
}
175+
176+
Object.assign(data.executedFrontCalls, this.frontCallsResultsBuffer);
177+
178+
data.componentData = this.app.getComponentData();
179+
180+
let newXhr = this.getNewXMLHttpRequest();
181+
this.sendRequest(newXhr, data);
182+
}
183+
}
184+
}
185+
JAVASCRIPT;
186+
}
187+
188+
public function getJavaScriptCreateInstanceScript(): string
189+
{
190+
return <<<JAVASCRIPT
191+
window.bus = new Bus(stratusAppInstance);
192+
stratusAppInstance.setBus(bus);
193+
JAVASCRIPT;
194+
}
36195
}

tests/SeleniumTestCase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public static function executeScript(string $script)
166166
public static function waitForResponse()
167167
{
168168
do {
169-
$httpRequestsLen = static::executeScript('return stratusAppInstance.httpRequests.length');
169+
$httpRequestsLen = static::executeScript('return stratusAppInstance.bus.httpRequests.length');
170170
} while ($httpRequestsLen > 0);
171171
}
172172
}

0 commit comments

Comments
 (0)