Skip to content

Commit 39d5b7a

Browse files
authored
Merge pull request #71 from ItaloBC/ItaloBC-ReloadOnOldDrip
Reload the Page if we miss the Drip
2 parents 986d780 + fe3c184 commit 39d5b7a

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22

33
return [
4-
'dripIntervalInMilliSeconds' => 300000, // every 5 minutes
4+
'dripIntervalInMilliSeconds' => 300000, // Drip every 5 minutes
55
'domain' => null, // defaults to url('/')
6-
'route' => 'genealabs/laravel-caffeine/drip', // can be customized
6+
'route' => 'genealabs/laravel-caffeine/drip', // Can be customized
7+
'thresholdDifference' => 10000, // When the drip will be considered old to reload the page
8+
'checkLastDripInterval' => 2000 // How often we will check if the drip is old
79
];

src/Dripper.php

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,27 @@ class Dripper extends Model
1111
{
1212
public function getHtmlAttribute() : string
1313
{
14-
return '<script>setInterval(function(){'
15-
. "var e=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject('Microsoft.XMLHTTP');"
16-
. "e.open('GET','{$this->url}',!0);"
17-
. "e.setRequestHeader('X-Requested-With','XMLHttpRequest');"
18-
. "e.send();}, {$this->interval});</script>";
14+
15+
return '<script>'
16+
. "let ld = new Date();"
17+
. "function caffeineSendDrip () {"
18+
. " let e = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('Microsoft.XMLHTTP');"
19+
. " e.onreadystatechange = function () {"
20+
. " if (e.readyState === 4 && e.status === 204) {"
21+
. " ld = new Date();"
22+
. " }"
23+
. " };"
24+
. " e.open('GET', '{$this->url}', !0);"
25+
. " e.setRequestHeader('X-Requested-With', 'XMLHttpRequest');"
26+
. " e.send();"
27+
. "}"
28+
. "setInterval(function () { caffeineSendDrip(); }, $this->interval);"
29+
. "setInterval(function () {"
30+
. " if (new Date() - ld >= $this->interval + $this->threshold) {"
31+
. " location.reload(true);"
32+
. " }"
33+
. "}, $this->checkInterval);"
34+
. "</script>";
1935
}
2036

2137
public function getIntervalAttribute() : string
@@ -25,6 +41,22 @@ public function getIntervalAttribute() : string
2541
300000
2642
);
2743
}
44+
45+
public function getThresholdAttribute() : int
46+
{
47+
return config(
48+
'genealabs-laravel-caffeine.thresholdDifference',
49+
10000
50+
);
51+
}
52+
53+
public function getCheckIntervalAttribute() : int
54+
{
55+
return config(
56+
'genealabs-laravel-caffeine.checkLastDripInterval',
57+
2000
58+
);
59+
}
2860

2961
public function getUrlAttribute() : string
3062
{

0 commit comments

Comments
 (0)