From d7a9c3404755193444550261d74cf83a58c3cf3e Mon Sep 17 00:00:00 2001 From: Jack Chapple Date: Mon, 25 May 2015 22:36:56 +0100 Subject: [PATCH 1/2] This fix addresses a glaring issue where the concatenated JS initialisation code is executed **each time** ReactJS::getMarkup() is called. I cannot see a good reason why this should be the case, and my thinking is that the concatenated code bundle should be executed once only - when ReactJS is instantiated. This way, ReactJS::getMarkup() is only responsible for executing the JS React.renderToString() method, rather than executing the whole vendor bundle first, and then calling .renderToString() - which is incredibly costly and slow! --- ReactJS.php | 63 +++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/ReactJS.php b/ReactJS.php index fc88a47..3855229 100644 --- a/ReactJS.php +++ b/ReactJS.php @@ -16,12 +16,6 @@ class ReactJS { private - /** - * Concatenated React.js + Application code + boilerplate - * @var string - */ - $react, - /** * Name of the component to render * @var string @@ -64,9 +58,11 @@ function __construct($libsrc, $appsrc) { // app's components $react[] = $appsrc; $react[] = ';'; - $this->react = implode(";\n", $react); - + + $concatenated = implode(";\n", $react); + $this->v8 = new V8Js(); + $this->executeJS($concatenated); } /** @@ -103,28 +99,14 @@ function setErrorHandler($err) { * @return string HTML string */ function getMarkup() { - try { - $js = $this->react; - $js.= sprintf( - "print(React.renderToString(React.createElement(%s, %s)))", - $this->component, - $this->data); - - ob_start(); - $this->v8->executeString($js); - return ob_get_clean(); + $js = sprintf( + "print(React.renderToString(React.createElement(%s, %s)))", + $this->component, + $this->data); - } catch (V8JsException $e) { - if ($this->errorHandler) { - call_user_func($this->errorHandler, $e); - } else { - // default error handler blows up bad - echo "
";
-        echo $e->getMessage();
-        echo "
"; - die(); - } - } + ob_start(); + $this->executeJS($js); + return ob_get_clean(); } /** @@ -163,5 +145,28 @@ function getJS($where, $return_var = null) { $where ); } + + /** + * Executes Javascript using V8JS, with primitive exception handling + * + * @param string $js JS code to be executed + * @return mixed + */ + private function executeJS($js) { + try { + return $this->v8->executeString($js); + } catch (V8JsException $e) { + if ($this->errorHandler) { + call_user_func($this->errorHandler, $e); + } else { + // default error handler blows up bad + echo "
";
+        echo $e->getMessage();
+        echo "
"; + die(); + } + } + } + } From 882f81dd1a588fbc59c3415a4defa49d620db919 Mon Sep 17 00:00:00 2001 From: Jack Chapple Date: Mon, 25 May 2015 23:06:20 +0100 Subject: [PATCH 2/2] Refactoring --- ReactJS.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ReactJS.php b/ReactJS.php index 3855229..2ceca68 100644 --- a/ReactJS.php +++ b/ReactJS.php @@ -104,9 +104,7 @@ function getMarkup() { $this->component, $this->data); - ob_start(); - $this->executeJS($js); - return ob_get_clean(); + return $this->executeJS($js); } /** @@ -150,11 +148,13 @@ function getJS($where, $return_var = null) { * Executes Javascript using V8JS, with primitive exception handling * * @param string $js JS code to be executed - * @return mixed + * @return string The execution response */ private function executeJS($js) { try { - return $this->v8->executeString($js); + ob_start(); + $this->v8->executeString($js); + return ob_get_clean(); } catch (V8JsException $e) { if ($this->errorHandler) { call_user_func($this->errorHandler, $e);