Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

Commit 6bfbf16

Browse files
committed
Merge pull request #7 from jchapple/dev-init-execution-fix
Fix to address a performance-related issue wherein the Javascript init bundle is executed each time getMarkup() is called
2 parents 11d61ce + 882f81d commit 6bfbf16

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

ReactJS.php

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@
1616
class ReactJS {
1717

1818
private
19-
/**
20-
* Concatenated React.js + Application code + boilerplate
21-
* @var string
22-
*/
23-
$react,
24-
2519
/**
2620
* Name of the component to render
2721
* @var string
@@ -64,9 +58,11 @@ function __construct($libsrc, $appsrc) {
6458
// app's components
6559
$react[] = $appsrc;
6660
$react[] = ';';
67-
$this->react = implode(";\n", $react);
68-
61+
62+
$concatenated = implode(";\n", $react);
63+
6964
$this->v8 = new V8Js();
65+
$this->executeJS($concatenated);
7066
}
7167

7268
/**
@@ -103,28 +99,12 @@ function setErrorHandler($err) {
10399
* @return string HTML string
104100
*/
105101
function getMarkup() {
106-
try {
107-
$js = $this->react;
108-
$js.= sprintf(
109-
"print(React.renderToString(React.createElement(%s, %s)))",
110-
$this->component,
111-
$this->data);
112-
113-
ob_start();
114-
$this->v8->executeString($js);
115-
return ob_get_clean();
102+
$js = sprintf(
103+
"print(React.renderToString(React.createElement(%s, %s)))",
104+
$this->component,
105+
$this->data);
116106

117-
} catch (V8JsException $e) {
118-
if ($this->errorHandler) {
119-
call_user_func($this->errorHandler, $e);
120-
} else {
121-
// default error handler blows up bad
122-
echo "<pre>";
123-
echo $e->getMessage();
124-
echo "</pre>";
125-
die();
126-
}
127-
}
107+
return $this->executeJS($js);
128108
}
129109

130110
/**
@@ -163,5 +143,30 @@ function getJS($where, $return_var = null) {
163143
$where
164144
);
165145
}
146+
147+
/**
148+
* Executes Javascript using V8JS, with primitive exception handling
149+
*
150+
* @param string $js JS code to be executed
151+
* @return string The execution response
152+
*/
153+
private function executeJS($js) {
154+
try {
155+
ob_start();
156+
$this->v8->executeString($js);
157+
return ob_get_clean();
158+
} catch (V8JsException $e) {
159+
if ($this->errorHandler) {
160+
call_user_func($this->errorHandler, $e);
161+
} else {
162+
// default error handler blows up bad
163+
echo "<pre>";
164+
echo $e->getMessage();
165+
echo "</pre>";
166+
die();
167+
}
168+
}
169+
}
170+
166171
}
167172

0 commit comments

Comments
 (0)