Fossil SCM
Cleaned up the is-first-call handling of the /chat message poller.
Commit
147ff84e21b9fc4302d5f98139ca12e88293570d6795f3a02a8009c2e40a52ef
Parent
c97364320972ec7…
2 files changed
+14
-12
+12
+14
-12
| --- src/chat.js | ||
| +++ src/chat.js | ||
| @@ -1216,12 +1216,13 @@ | ||
| 1216 | 1216 | btn.addEventListener('click',()=>loadOldMessages(-1)); |
| 1217 | 1217 | D.append(Chat.e.messagesWrapper, toolbar); |
| 1218 | 1218 | toolbar.disabled = true /*will be enabled when msg load finishes */; |
| 1219 | 1219 | })()/*end history loading widget setup*/; |
| 1220 | 1220 | |
| 1221 | - const afterFetch = function(isFirstCall){ | |
| 1222 | - if(isFirstCall){ | |
| 1221 | + const afterFetch = function f(){ | |
| 1222 | + if(true===f.isFirstCall){ | |
| 1223 | + f.isFirstCall = false; | |
| 1223 | 1224 | Chat.ajaxEnd(); |
| 1224 | 1225 | Chat.e.messagesWrapper.classList.remove('loading'); |
| 1225 | 1226 | setTimeout(function(){ |
| 1226 | 1227 | Chat.scrollMessagesTo(1); |
| 1227 | 1228 | }, 250); |
| @@ -1233,18 +1234,20 @@ | ||
| 1233 | 1234 | "Reload this page to reactivate it."); |
| 1234 | 1235 | delete Chat.intervalTimer; |
| 1235 | 1236 | } |
| 1236 | 1237 | poll.running = false; |
| 1237 | 1238 | }; |
| 1238 | - const poll = async function(){ | |
| 1239 | - if(poll.running) return; | |
| 1240 | - poll.running = true; | |
| 1241 | - if(0===poll.runCount++){ | |
| 1239 | + afterFetch.isFirstCall = true; | |
| 1240 | + const poll = async function f(){ | |
| 1241 | + if(f.running) return; | |
| 1242 | + f.running = true; | |
| 1243 | + Chat._isBatchLoading = f.isFirstCall; | |
| 1244 | + if(true===f.isFirstCall){ | |
| 1245 | + f.isFirstCall = false; | |
| 1242 | 1246 | Chat.ajaxStart(); |
| 1243 | 1247 | Chat.e.messagesWrapper.classList.add('loading'); |
| 1244 | 1248 | } |
| 1245 | - Chat._isBatchLoading = (1===poll.runCount); | |
| 1246 | 1249 | F.fetch("chat-poll",{ |
| 1247 | 1250 | timeout: 420 * 1000/*FIXME: get the value from the server*/, |
| 1248 | 1251 | urlParams:{ |
| 1249 | 1252 | name: Chat.mxMsg |
| 1250 | 1253 | }, |
| @@ -1256,23 +1259,22 @@ | ||
| 1256 | 1259 | Chat._isBatchLoading = false; |
| 1257 | 1260 | console.error(err); |
| 1258 | 1261 | /* ^^^ we don't use Chat.reportError() here b/c the polling |
| 1259 | 1262 | fails exepectedly when it times out, but is then immediately |
| 1260 | 1263 | resumed, and reportError() produces a loud error message. */ |
| 1261 | - afterFetch(1===poll.runCount); | |
| 1264 | + afterFetch(); | |
| 1262 | 1265 | }, |
| 1263 | 1266 | onload:function(y){ |
| 1264 | 1267 | newcontent(y); |
| 1265 | 1268 | Chat._isBatchLoading = false; |
| 1266 | - afterFetch(1===poll.runCount); | |
| 1269 | + afterFetch(); | |
| 1267 | 1270 | } |
| 1268 | 1271 | }); |
| 1269 | 1272 | }; |
| 1270 | - poll.runCount = 0; | |
| 1273 | + poll.isFirstCall = true; | |
| 1271 | 1274 | Chat._gotServerError = poll.running = false; |
| 1272 | - Chat.intervalTimer = setInterval(poll, 1000); | |
| 1273 | 1275 | if( window.fossil.config.chat.fromcli ){ |
| 1274 | 1276 | Chat.chatOnlyMode(true); |
| 1275 | 1277 | } |
| 1276 | - | |
| 1278 | + Chat.intervalTimer = setInterval(poll, 1000); | |
| 1277 | 1279 | F.page.chat = Chat/* enables testing the APIs via the dev tools */; |
| 1278 | 1280 | })(); |
| 1279 | 1281 |
| --- src/chat.js | |
| +++ src/chat.js | |
| @@ -1216,12 +1216,13 @@ | |
| 1216 | btn.addEventListener('click',()=>loadOldMessages(-1)); |
| 1217 | D.append(Chat.e.messagesWrapper, toolbar); |
| 1218 | toolbar.disabled = true /*will be enabled when msg load finishes */; |
| 1219 | })()/*end history loading widget setup*/; |
| 1220 | |
| 1221 | const afterFetch = function(isFirstCall){ |
| 1222 | if(isFirstCall){ |
| 1223 | Chat.ajaxEnd(); |
| 1224 | Chat.e.messagesWrapper.classList.remove('loading'); |
| 1225 | setTimeout(function(){ |
| 1226 | Chat.scrollMessagesTo(1); |
| 1227 | }, 250); |
| @@ -1233,18 +1234,20 @@ | |
| 1233 | "Reload this page to reactivate it."); |
| 1234 | delete Chat.intervalTimer; |
| 1235 | } |
| 1236 | poll.running = false; |
| 1237 | }; |
| 1238 | const poll = async function(){ |
| 1239 | if(poll.running) return; |
| 1240 | poll.running = true; |
| 1241 | if(0===poll.runCount++){ |
| 1242 | Chat.ajaxStart(); |
| 1243 | Chat.e.messagesWrapper.classList.add('loading'); |
| 1244 | } |
| 1245 | Chat._isBatchLoading = (1===poll.runCount); |
| 1246 | F.fetch("chat-poll",{ |
| 1247 | timeout: 420 * 1000/*FIXME: get the value from the server*/, |
| 1248 | urlParams:{ |
| 1249 | name: Chat.mxMsg |
| 1250 | }, |
| @@ -1256,23 +1259,22 @@ | |
| 1256 | Chat._isBatchLoading = false; |
| 1257 | console.error(err); |
| 1258 | /* ^^^ we don't use Chat.reportError() here b/c the polling |
| 1259 | fails exepectedly when it times out, but is then immediately |
| 1260 | resumed, and reportError() produces a loud error message. */ |
| 1261 | afterFetch(1===poll.runCount); |
| 1262 | }, |
| 1263 | onload:function(y){ |
| 1264 | newcontent(y); |
| 1265 | Chat._isBatchLoading = false; |
| 1266 | afterFetch(1===poll.runCount); |
| 1267 | } |
| 1268 | }); |
| 1269 | }; |
| 1270 | poll.runCount = 0; |
| 1271 | Chat._gotServerError = poll.running = false; |
| 1272 | Chat.intervalTimer = setInterval(poll, 1000); |
| 1273 | if( window.fossil.config.chat.fromcli ){ |
| 1274 | Chat.chatOnlyMode(true); |
| 1275 | } |
| 1276 | |
| 1277 | F.page.chat = Chat/* enables testing the APIs via the dev tools */; |
| 1278 | })(); |
| 1279 |
| --- src/chat.js | |
| +++ src/chat.js | |
| @@ -1216,12 +1216,13 @@ | |
| 1216 | btn.addEventListener('click',()=>loadOldMessages(-1)); |
| 1217 | D.append(Chat.e.messagesWrapper, toolbar); |
| 1218 | toolbar.disabled = true /*will be enabled when msg load finishes */; |
| 1219 | })()/*end history loading widget setup*/; |
| 1220 | |
| 1221 | const afterFetch = function f(){ |
| 1222 | if(true===f.isFirstCall){ |
| 1223 | f.isFirstCall = false; |
| 1224 | Chat.ajaxEnd(); |
| 1225 | Chat.e.messagesWrapper.classList.remove('loading'); |
| 1226 | setTimeout(function(){ |
| 1227 | Chat.scrollMessagesTo(1); |
| 1228 | }, 250); |
| @@ -1233,18 +1234,20 @@ | |
| 1234 | "Reload this page to reactivate it."); |
| 1235 | delete Chat.intervalTimer; |
| 1236 | } |
| 1237 | poll.running = false; |
| 1238 | }; |
| 1239 | afterFetch.isFirstCall = true; |
| 1240 | const poll = async function f(){ |
| 1241 | if(f.running) return; |
| 1242 | f.running = true; |
| 1243 | Chat._isBatchLoading = f.isFirstCall; |
| 1244 | if(true===f.isFirstCall){ |
| 1245 | f.isFirstCall = false; |
| 1246 | Chat.ajaxStart(); |
| 1247 | Chat.e.messagesWrapper.classList.add('loading'); |
| 1248 | } |
| 1249 | F.fetch("chat-poll",{ |
| 1250 | timeout: 420 * 1000/*FIXME: get the value from the server*/, |
| 1251 | urlParams:{ |
| 1252 | name: Chat.mxMsg |
| 1253 | }, |
| @@ -1256,23 +1259,22 @@ | |
| 1259 | Chat._isBatchLoading = false; |
| 1260 | console.error(err); |
| 1261 | /* ^^^ we don't use Chat.reportError() here b/c the polling |
| 1262 | fails exepectedly when it times out, but is then immediately |
| 1263 | resumed, and reportError() produces a loud error message. */ |
| 1264 | afterFetch(); |
| 1265 | }, |
| 1266 | onload:function(y){ |
| 1267 | newcontent(y); |
| 1268 | Chat._isBatchLoading = false; |
| 1269 | afterFetch(); |
| 1270 | } |
| 1271 | }); |
| 1272 | }; |
| 1273 | poll.isFirstCall = true; |
| 1274 | Chat._gotServerError = poll.running = false; |
| 1275 | if( window.fossil.config.chat.fromcli ){ |
| 1276 | Chat.chatOnlyMode(true); |
| 1277 | } |
| 1278 | Chat.intervalTimer = setInterval(poll, 1000); |
| 1279 | F.page.chat = Chat/* enables testing the APIs via the dev tools */; |
| 1280 | })(); |
| 1281 |
+12
| --- src/fossil.fetch.js | ||
| +++ src/fossil.fetch.js | ||
| @@ -166,10 +166,22 @@ | ||
| 166 | 166 | opt.onerror(new Error("XHR timeout of "+x.timeout+"ms expired.")); |
| 167 | 167 | }; |
| 168 | 168 | x.onreadystatechange = function(){ |
| 169 | 169 | if(XMLHttpRequest.DONE !== x.readyState) return; |
| 170 | 170 | try{opt.aftersend()}catch(e){/*ignore*/} |
| 171 | + if(false && 0===x.status){ | |
| 172 | + /* For reasons unknown, we _sometimes_ trigger x.status==0 in FF | |
| 173 | + when the /chat page starts up, but not in Chrome nor in other | |
| 174 | + apps. Insofar as has been determined, this happens before a | |
| 175 | + request is actually sent and it appears to have no | |
| 176 | + side-effects on the app other than to generate an error | |
| 177 | + (i.e. no requests/responses are missing). This is a silly | |
| 178 | + workaround which may or may not bite us later. If so, it can | |
| 179 | + be removed at the cost of an unsightly console error message | |
| 180 | + in FF. */ | |
| 181 | + return; | |
| 182 | + } | |
| 171 | 183 | if(200!==x.status){ |
| 172 | 184 | let err; |
| 173 | 185 | try{ |
| 174 | 186 | const j = JSON.parse(x.response); |
| 175 | 187 | if(j.error) err = new Error(j.error); |
| 176 | 188 |
| --- src/fossil.fetch.js | |
| +++ src/fossil.fetch.js | |
| @@ -166,10 +166,22 @@ | |
| 166 | opt.onerror(new Error("XHR timeout of "+x.timeout+"ms expired.")); |
| 167 | }; |
| 168 | x.onreadystatechange = function(){ |
| 169 | if(XMLHttpRequest.DONE !== x.readyState) return; |
| 170 | try{opt.aftersend()}catch(e){/*ignore*/} |
| 171 | if(200!==x.status){ |
| 172 | let err; |
| 173 | try{ |
| 174 | const j = JSON.parse(x.response); |
| 175 | if(j.error) err = new Error(j.error); |
| 176 |
| --- src/fossil.fetch.js | |
| +++ src/fossil.fetch.js | |
| @@ -166,10 +166,22 @@ | |
| 166 | opt.onerror(new Error("XHR timeout of "+x.timeout+"ms expired.")); |
| 167 | }; |
| 168 | x.onreadystatechange = function(){ |
| 169 | if(XMLHttpRequest.DONE !== x.readyState) return; |
| 170 | try{opt.aftersend()}catch(e){/*ignore*/} |
| 171 | if(false && 0===x.status){ |
| 172 | /* For reasons unknown, we _sometimes_ trigger x.status==0 in FF |
| 173 | when the /chat page starts up, but not in Chrome nor in other |
| 174 | apps. Insofar as has been determined, this happens before a |
| 175 | request is actually sent and it appears to have no |
| 176 | side-effects on the app other than to generate an error |
| 177 | (i.e. no requests/responses are missing). This is a silly |
| 178 | workaround which may or may not bite us later. If so, it can |
| 179 | be removed at the cost of an unsightly console error message |
| 180 | in FF. */ |
| 181 | return; |
| 182 | } |
| 183 | if(200!==x.status){ |
| 184 | let err; |
| 185 | try{ |
| 186 | const j = JSON.parse(x.response); |
| 187 | if(j.error) err = new Error(j.error); |
| 188 |