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,28 +650,30 @@ $(function (){
$('#cb_dark_name').val(''); $('#cb_dark_name').val('');
cancelGameCallback = IO.onGameUpdate(newId, function(data, gameId) { cancelGameCallback = IO.onGameUpdate(newId, function(data, gameId) {
if (data.modified > $('#cb_board').data('modified')) { updateQueue.idle.then(() => {
try { if (data.modified > $('#cb_board').data('modified')) {
const newGame = new PS.Game(JSON.stringify(data.board)); try {
const newState = JSON.parse(newGame.toJSON()); const newGame = new PS.Game(JSON.stringify(data.board));
const oldState = JSON.parse(currentGame.toJSON()); const newState = JSON.parse(newGame.toJSON());
const oldState = JSON.parse(currentGame.toJSON());
if (!deepEqual(newState, oldState)) { if (!deepEqual(newState, oldState)) {
debug('got board', newGame.moves); debug('got board', newGame.moves);
setCurrentGame(newGame, newGame.moves.length > currentGame.moves.length); setCurrentGame(newGame, newGame.moves.length > currentGame.moves.length);
}
} catch (err) {
debug('Error parsing board data', err);
} }
} catch (err) {
debug('Error parsing board data', err); const d = data || {};
$('#cb_board').data('lightName', shortenName(String(d.lightName || 'Light')));
$('#cb_board').data('darkName', shortenName(String(d.darkName || 'Dark')));
$('#cb_light_name').val(String(d.lightName || ''));
$('#cb_dark_name').val(String(d.darkName || ''));
$('#cb_board').data('modified', data.modified);
} }
});
const d = data || {};
$('#cb_board').data('lightName', shortenName(String(d.lightName || 'Light')));
$('#cb_board').data('darkName', shortenName(String(d.darkName || 'Dark')));
$('#cb_light_name').val(String(d.lightName || ''));
$('#cb_dark_name').val(String(d.darkName || ''));
$('#cb_board').data('modified', data.modified);
}
}); });
const notifyList = $('#cb_notify').data('gameList'); const notifyList = $('#cb_notify').data('gameList');