Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
shendepu committed Nov 4, 2016
1 parent 1260349 commit bc73a65
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 137 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
lib
build

screen/ReactSSRRoot/dist
../moqui-react-ssr-demo/screen/ReactSSRRoot/dist
2 changes: 1 addition & 1 deletion component.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<component xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://moqui.org/xsd/moqui-conf-2.0.xsd"
name="react-ssr" version="0.1">
name="moqui-react-ssr" version="0.1">
</component>
12 changes: 0 additions & 12 deletions data/ZaaReactSSRSecurityData.xml

This file was deleted.

19 changes: 2 additions & 17 deletions screen/ReactSSRRoot.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<screen xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://moqui.org/xsd/xml-screen-2.0.xsd"
default-menu-index="15" default-menu-title="React SSR" require-authentication="anonymous-view" allow-extra-path="true" standalone="true">
<transition name="comments.json">
<actions>
<script>
comments = [
["author":"Peter Parker", "text":"This is a comment."],
["author":"John Doe","text":"This is *another* comment."]
]
ec.web.sendJsonResponse(comments)
</script>
</actions>
<default-response type="none"/>
</transition>

<subscreens default-item="index.html"/>

require-authentication="anonymous-view" standalone="true" default-menu-include="false">
<widgets>
<subscreens-active/>

</widgets>
</screen>
19 changes: 0 additions & 19 deletions screen/ReactSSRRoot/includes/index.html.ftl

This file was deleted.

75 changes: 0 additions & 75 deletions screen/ReactSSRRoot/index.html.xml

This file was deleted.

13 changes: 12 additions & 1 deletion screen/ReactSSRRoot/static/nashorn-polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,16 @@ if (!Object.assign) {
});
}

/* ScriptContext set below at global
println
printlnString
__REQ_URL__
*/

window.__IS_SSR__ = true;
window.__REQ_URL__ = ec.web.requestUrl.substring(45);
window.__APP_BASE_PATH__ = ec.context.get('basePath');
window.println(ec.context.get('basePath'));
// window.__REQ_URL__ = ec.web.request.getRequestURI();
window.println(__REQ_URL__)
window.println(window.__REQ_URL__)
window.println(ec.web.request.getRequestURI())
40 changes: 29 additions & 11 deletions src/main/java/com/moqui/ssr/React.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.ScriptContext;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStreamReader;

import org.moqui.resource.ResourceReference;
Expand All @@ -22,9 +23,20 @@ public class React {
private Object error;
private boolean promiseResolved;

Consumer<Object> fnResolve = object -> {
promiseResolved = true;
html = object;
};

Consumer<Object> fnReject = object -> {
promiseResolved = true;
error = object;
};

React(ExecutionContext ec, Map<String, ResourceReference> jsFileMap) {
this.ec = ec;
this.jsFileMap = jsFileMap;

}

private ThreadLocal<NashornScriptEngine> engineHolder = new ThreadLocal<NashornScriptEngine>() {
Expand All @@ -38,10 +50,16 @@ protected NashornScriptEngine initialValue() {
}

Consumer<Object> println = System.out::println;
Consumer<Object> printlnString = object -> System.out.println(object.toString());
ScriptContext sc = nashornScriptEngine.getContext();
sc.setAttribute("println", println, ScriptContext.ENGINE_SCOPE);
sc.setAttribute("println", println, ScriptContext.GLOBAL_SCOPE);
sc.setAttribute("printlnString", printlnString, ScriptContext.GLOBAL_SCOPE);
sc.setAttribute("ec", ec, ScriptContext.ENGINE_SCOPE);

String locationUrl = getUrlLocation(ec.getWeb().getRequest());
sc.setAttribute("__REQ_URL__", locationUrl, ScriptContext.ENGINE_SCOPE);
ec.getLogger().info(locationUrl);

for (Map.Entry<String, ResourceReference> entry : jsFileMap.entrySet()) {
if (entry.getValue() == null) continue;
ec.getLogger().info("Evaluating " + entry.getKey());
Expand Down Expand Up @@ -69,21 +87,14 @@ public String render() {
ec.getLogger().info("start server rendering");
promiseResolved = false;

ScriptObjectMirror promise = (ScriptObjectMirror) engineHolder.get().invokeFunction("renderServer");
Consumer<Object> fnResolve = object -> {
promiseResolved = true;
html = object;
};
NashornScriptEngine engine = engineHolder.get();

System.out.println(fnResolve);
ScriptObjectMirror promise = (ScriptObjectMirror) engine.invokeFunction("renderServer");

Consumer<Object> fnReject = object -> {

promiseResolved = true;
error = object;
};
promise.callMember("then", fnResolve, fnReject);


int interval = 50;
int i = 1;
while (!promiseResolved && i < 20) {
Expand All @@ -97,4 +108,11 @@ public String render() {
throw new IllegalStateException("failed to render react", e);
}
}

public static String getUrlLocation(HttpServletRequest request) {
StringBuilder requestUrl = new StringBuilder();
requestUrl.append(request.getRequestURI());
if (request.getQueryString() != null && request.getQueryString().length() > 0) requestUrl.append("?" + request.getQueryString());
return requestUrl.toString();
}
}

0 comments on commit bc73a65

Please sign in to comment.