fix for race between polling and server response after update

This commit is contained in:
Jesse D. McDonald 2020-05-10 20:20:31 -05:00
parent f4318b2464
commit 9795c83583
1 changed files with 28 additions and 19 deletions

View File

@ -463,6 +463,8 @@ $(function (){
head: 0, /* next item to be sent */ head: 0, /* next item to be sent */
tail: 0, /* ID to assign to the next update added to the queue */ tail: 0, /* ID to assign to the next update added to the queue */
sending: false, sending: false,
idle: Promise.resolve(true), /* resolves to true if updates succeeded */
signal_idle: function() {},
add(gameId, data, modified) { add(gameId, data, modified) {
const wasEmpty = this.isEmpty(); const wasEmpty = this.isEmpty();
@ -474,6 +476,9 @@ $(function (){
if (!this.sending) { if (!this.sending) {
openNoticeBox('Saving...'); openNoticeBox('Saving...');
this.sending = true; this.sending = true;
this.idle = new Promise((resolve) => {
this.signal_idle = resolve;
});
this.sendNext(); this.sendNext();
} }
}, },
@ -543,6 +548,7 @@ $(function (){
if (queue.isEmpty()) { if (queue.isEmpty()) {
queue.sending = false; queue.sending = false;
queue.signal_idle(true);
/* close the Saving... notice*/ /* close the Saving... notice*/
noticeBox.close({ ignoreDelay: true }); noticeBox.close({ ignoreDelay: true });
} else { } else {
@ -559,6 +565,7 @@ $(function (){
} }
queue.sending = false; queue.sending = false;
queue.signal_idle(false);
/* force a reset back to the latest server data */ /* force a reset back to the latest server data */
if (update.gameId === $('#cb_board').data('gameId')) { if (update.gameId === $('#cb_board').data('gameId')) {
@ -643,6 +650,7 @@ $(function (){
$('#cb_dark_name').val(''); $('#cb_dark_name').val('');
cancelGameCallback = IO.onGameUpdate(newId, function(data, gameId) { cancelGameCallback = IO.onGameUpdate(newId, function(data, gameId) {
updateQueue.idle.then(() => {
if (data.modified > $('#cb_board').data('modified')) { if (data.modified > $('#cb_board').data('modified')) {
try { try {
const newGame = new PS.Game(JSON.stringify(data.board)); const newGame = new PS.Game(JSON.stringify(data.board));
@ -666,6 +674,7 @@ $(function (){
$('#cb_board').data('modified', data.modified); $('#cb_board').data('modified', data.modified);
} }
}); });
});
const notifyList = $('#cb_notify').data('gameList'); const notifyList = $('#cb_notify').data('gameList');
const doNotify = notifyList.includes('*') || notifyList.includes(newId); const doNotify = notifyList.includes('*') || notifyList.includes(newId);