Skip to content

Commit

Permalink
Merge pull request #71 from ItaloBC/ItaloBC-ReloadOnOldDrip
Browse files Browse the repository at this point in the history
Reload the Page if we miss the Drip
  • Loading branch information
mikebronner authored Dec 9, 2017
2 parents 986d780 + fe3c184 commit 39d5b7a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
6 changes: 4 additions & 2 deletions config/genealabs-laravel-caffeine.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

return [
'dripIntervalInMilliSeconds' => 300000, // every 5 minutes
'dripIntervalInMilliSeconds' => 300000, // Drip every 5 minutes
'domain' => null, // defaults to url('/')
'route' => 'genealabs/laravel-caffeine/drip', // can be customized
'route' => 'genealabs/laravel-caffeine/drip', // Can be customized
'thresholdDifference' => 10000, // When the drip will be considered old to reload the page
'checkLastDripInterval' => 2000 // How often we will check if the drip is old
];
42 changes: 37 additions & 5 deletions src/Dripper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,27 @@ class Dripper extends Model
{
public function getHtmlAttribute() : string
{
return '<script>setInterval(function(){'
. "var e=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject('Microsoft.XMLHTTP');"
. "e.open('GET','{$this->url}',!0);"
. "e.setRequestHeader('X-Requested-With','XMLHttpRequest');"
. "e.send();}, {$this->interval});</script>";

return '<script>'
. "let ld = new Date();"
. "function caffeineSendDrip () {"
. " let e = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('Microsoft.XMLHTTP');"
. " e.onreadystatechange = function () {"
. " if (e.readyState === 4 && e.status === 204) {"
. " ld = new Date();"
. " }"
. " };"
. " e.open('GET', '{$this->url}', !0);"
. " e.setRequestHeader('X-Requested-With', 'XMLHttpRequest');"
. " e.send();"
. "}"
. "setInterval(function () { caffeineSendDrip(); }, $this->interval);"
. "setInterval(function () {"
. " if (new Date() - ld >= $this->interval + $this->threshold) {"
. " location.reload(true);"
. " }"
. "}, $this->checkInterval);"
. "</script>";
}

public function getIntervalAttribute() : string
Expand All @@ -25,6 +41,22 @@ public function getIntervalAttribute() : string
300000
);
}

public function getThresholdAttribute() : int
{
return config(
'genealabs-laravel-caffeine.thresholdDifference',
10000
);
}

public function getCheckIntervalAttribute() : int
{
return config(
'genealabs-laravel-caffeine.checkLastDripInterval',
2000
);
}

public function getUrlAttribute() : string
{
Expand Down

0 comments on commit 39d5b7a

Please sign in to comment.