diff --git a/ReactJS.php b/ReactJS.php index 7e04267..ce7b67e 100644 --- a/ReactJS.php +++ b/ReactJS.php @@ -38,7 +38,19 @@ class ReactJS { * Custom error handler * @var callable */ - $errorHandler; + $errorHandler, + + /** + * JS Code to execute only once + * @var string + */ + $bootstrap, + + /** + * Has the bootstrap JS code been executed ? + * @var bool + */ + $booted = false; /** * Initialize by passing JS code as a string. @@ -59,10 +71,9 @@ function __construct($libsrc, $appsrc) { $react[] = $appsrc; $react[] = ';'; - $concatenated = implode(";\n", $react); + $this->bootstrap = implode(";\n", $react); $this->v8 = new V8Js(); - $this->executeJS($concatenated); } /** @@ -152,6 +163,7 @@ function getJS($where, $return_var = null) { */ private function executeJS($js) { try { + $this->bootIfRequired(); ob_start(); $this->v8->executeString($js); return ob_get_clean(); @@ -168,4 +180,13 @@ private function executeJS($js) { } } + /** + * Execute the bootstrap code if it hasn't been yet + */ + private function bootIfRequired() { + if ($this->booted === false) { + $this->v8->executeString($this->bootstrap); + $this->booted = true; + } + } }