From a0725d0bf159283e278168e3bd1dc0fe3e6da7ab Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Mon, 31 Dec 2018 00:23:06 +0100 Subject: Temporary branch: last-pagelet-event-optimization --- include/classes/BigPipe/BigPipe.php | 4 --- include/classes/Document.php | 34 +++++++++++++++++++ static/bigpipe.js | 36 +++++++++++++++----- template/document.php | 44 +++++++++++++++++++++++++ test.php | 66 +++++++++++++++++++++++++++++++++++++ 5 files changed, 172 insertions(+), 12 deletions(-) create mode 100644 include/classes/Document.php create mode 100644 template/document.php create mode 100644 test.php diff --git a/include/classes/BigPipe/BigPipe.php b/include/classes/BigPipe/BigPipe.php index d84083e..737e860 100644 --- a/include/classes/BigPipe/BigPipe.php +++ b/include/classes/BigPipe/BigPipe.php @@ -89,10 +89,6 @@ class BigPipe { } } } - - if(self::enabled()) { - echo "\n"; - } } } ?> \ No newline at end of file diff --git a/include/classes/Document.php b/include/classes/Document.php new file mode 100644 index 0000000..9b9893a --- /dev/null +++ b/include/classes/Document.php @@ -0,0 +1,34 @@ +pagelets[] = $Pagelet; + + $this->contentCallbacks[$Pagelet->getID()] = $callback; + + if(!BigPipe::enabled()) { + $Pagelet->addHTML($callback($Pagelet)); + } + } + + public function render($content_html, $sidebar_html) { + require 'template/document.php'; + BigPipe::flushOutputBuffer(); + + if(BigPipe::enabled()) { + foreach($this->pagelets as $Pagelet) { + $Pagelet->addHTML($this->contentCallbacks[$Pagelet->getID()]($Pagelet)); + $Pagelet->flush(); + } + } + + BigPipe::completeResponse(); + echo "\n"; + } +} +?> \ No newline at end of file diff --git a/static/bigpipe.js b/static/bigpipe.js index cc048a5..41bd4fd 100644 --- a/static/bigpipe.js +++ b/static/bigpipe.js @@ -272,7 +272,27 @@ BigPipe = (function() { wait: [], interval: null, + eventNode: document, + eventName: "DOMContentLoaded", + eventFunc: function() { + BigPipe.onLastPageletArrived(); + BigPipe.unregisterEventListener(); + }, + + registerEventListener() { + this.eventNode.addEventListener(this.eventName, this.eventFunc, false); + }, + + unregisterEventListener() { + this.eventNode.removeEventListener(this.eventName, this.eventFunc, false); + }, + onPageletArrive(data, codeContainer) { + if(this.phase === 0) { + this.phase = 1; + this.registerEventListener(); + } + let pageletHTML = codeContainer.innerHTML; pageletHTML = pageletHTML.substring(5, pageletHTML.length - 4); codeContainer.parentNode.removeChild(codeContainer); @@ -281,10 +301,6 @@ BigPipe = (function() { this.pagelets.push(pagelet); - if(this.phase === 0) { - this.phase = 1; - } - if(pagelet.NEED.length === 0 || pagelet.NEED.every(function(needID) { return BigPipe.done.indexOf(needID) !== -1; })) { @@ -339,12 +355,16 @@ BigPipe = (function() { // Public-Access //============================================================================== return { - onPageletArrive(data, codeContainer) { - BigPipe.onPageletArrive(data, codeContainer); + setLastPageletEventNode(eventNode) { + BigPipe.eventNode = eventNode; }, - onLastPageletArrived() { - BigPipe.onLastPageletArrived(); + setLastPageletEventName(eventName) { + BigPipe.eventName = eventName; + }, + + onPageletArrive(data, codeContainer) { + BigPipe.onPageletArrive(data, codeContainer); }, reset() { diff --git a/template/document.php b/template/document.php new file mode 100644 index 0000000..f556bb6 --- /dev/null +++ b/template/document.php @@ -0,0 +1,44 @@ + + + + + + + + + Example + + +
+
+

BigPipe

+

This is an example page to demonstrate how BigPipe works.

+ +
+
+
+ +
+ +
+
+ Imprint | Footer +
+
+ diff --git a/test.php b/test.php new file mode 100644 index 0000000..04a674a --- /dev/null +++ b/test.php @@ -0,0 +1,66 @@ +>> pagelets.php +#=============================================================================== +# Autoload register for classes +#=============================================================================== +spl_autoload_register(function($classname) { + $classname = str_replace('\\', '/', $classname); + require "include/classes/{$classname}.php"; +}); + +#=============================================================================== +# Enable debugging mode +#=============================================================================== +Application::$debugging = TRUE; + +#=============================================================================== +# Check if BigPipe should be disabled +#=============================================================================== +if(isset($_GET['bigpipe']) AND $_GET['bigpipe'] === '0') { + # You can use this method to disable the pipeline for Googlebot or something + # else. If BigPipe is "disabled", then all pagelets will be rendered without + # being pipelined through the javascript library. The content of the pagelet + # will be present at the original position within the HTML response (and all + # external stylesheets and javascripts will be displayed as simple or + #