- commit
- 815dbe9b48c5e29ca417479a4371622b469b7adc
- parent
- f25a4060c9a12a9de367bf0e24d1286a0e44ad10
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2024-09-06 12:01
exit if post fails repeatedly
Diffstat
M | static/pad.js | 32 | +++++++++++++++++++++++--------- |
1 files changed, 23 insertions, 9 deletions
diff --git a/static/pad.js b/static/pad.js
@@ -13,9 +13,15 @@ var old = el.value; 13 13 14 14 var localChanges = []; 15 15 var stagedChanges = []; -1 16 var failedRequests = 0; 16 17 17 18 document.title += ' - ' + location.hash.slice(1); 18 19 -1 20 var exit = function() { -1 21 el.readOnly = true; -1 22 eventSource.close(); -1 23 }; -1 24 19 25 var sendChanges = utils.throttled(function() { 20 26 if (stagedChanges.length) { 21 27 setTimeout(sendChanges, 500); @@ -26,10 +32,21 @@ var sendChanges = utils.throttled(function() { 26 32 return fetch(url, { 27 33 method: 'POST', 28 34 body: JSON.stringify(data),29 -1 }).catch(() => {30 -1 localChanges = stagedChanges.concat(localChanges);31 -1 stagedChanges = [];32 -1 sendChanges();-1 35 }).then(response => { -1 36 if (response.ok) { -1 37 failedRequests = 0; -1 38 } else { -1 39 throw response; -1 40 } -1 41 }).catch(err => { -1 42 if (failedRequests > 3) { -1 43 exit(); -1 44 } else { -1 45 failedRequests += 1; -1 46 localChanges = stagedChanges.concat(localChanges); -1 47 stagedChanges = []; -1 48 sendChanges(); -1 49 } 33 50 }); 34 51 } 35 52 }, 500); @@ -109,11 +126,6 @@ document.querySelector('button').addEventListener('click', function() { 109 126 } 110 127 }); 111 128112 -1 window.addEventListener('offline', function() {113 -1 el.readOnly = true;114 -1 eventSource.close();115 -1 });116 -1117 129 var eventSource = new EventSource(url); 118 130 eventSource.onopen = function() { 119 131 el.readOnly = false; @@ -126,6 +138,8 @@ eventSource.onmessage = function(event) { 126 138 } 127 139 }; 128 140 -1 141 window.addEventListener('offline', exit); -1 142 129 143 window.addEventListener('beforeunload', event => { 130 144 if (localChanges.length) { 131 145 var data = [id, 'changes', localChanges];