diff --git a/js/pacosako_ui.js b/js/pacosako_ui.js index ffb5888..32d8f4a 100644 --- a/js/pacosako_ui.js +++ b/js/pacosako_ui.js @@ -463,6 +463,8 @@ $(function (){ head: 0, /* next item to be sent */ tail: 0, /* ID to assign to the next update added to the queue */ sending: false, + idle: Promise.resolve(true), /* resolves to true if updates succeeded */ + signal_idle: function() {}, add(gameId, data, modified) { const wasEmpty = this.isEmpty(); @@ -474,6 +476,9 @@ $(function (){ if (!this.sending) { openNoticeBox('Saving...'); this.sending = true; + this.idle = new Promise((resolve) => { + this.signal_idle = resolve; + }); this.sendNext(); } }, @@ -543,6 +548,7 @@ $(function (){ if (queue.isEmpty()) { queue.sending = false; + queue.signal_idle(true); /* close the Saving... notice*/ noticeBox.close({ ignoreDelay: true }); } else { @@ -559,6 +565,7 @@ $(function (){ } queue.sending = false; + queue.signal_idle(false); /* force a reset back to the latest server data */ if (update.gameId === $('#cb_board').data('gameId')) { @@ -643,28 +650,30 @@ $(function (){ $('#cb_dark_name').val(''); cancelGameCallback = IO.onGameUpdate(newId, function(data, gameId) { - if (data.modified > $('#cb_board').data('modified')) { - try { - const newGame = new PS.Game(JSON.stringify(data.board)); - const newState = JSON.parse(newGame.toJSON()); - const oldState = JSON.parse(currentGame.toJSON()); + updateQueue.idle.then(() => { + if (data.modified > $('#cb_board').data('modified')) { + try { + const newGame = new PS.Game(JSON.stringify(data.board)); + const newState = JSON.parse(newGame.toJSON()); + const oldState = JSON.parse(currentGame.toJSON()); - if (!deepEqual(newState, oldState)) { - debug('got board', newGame.moves); - setCurrentGame(newGame, newGame.moves.length > currentGame.moves.length); + if (!deepEqual(newState, oldState)) { + debug('got board', newGame.moves); + 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');