diff --git a/js/pacosako_ui.js b/js/pacosako_ui.js index ee20f89..7bfa67b 100644 --- a/js/pacosako_ui.js +++ b/js/pacosako_ui.js @@ -796,60 +796,78 @@ $(function (){ } window.setTimeout(function(){ - gun.get(PacoSakoUUID).get('meta').map().on(function(d,key){ - if (key.match(/^[0-9a-f]{16}$/)) { - d = d || {}; - debug('got meta for key ' + key, d); - const lightName = shortenName(String(d.lightName || 'Light')); - const darkName = shortenName(String(d.darkName || 'Dark')); - const moves = !d.moves ? '' : - (', ' + d.moves + (d.moves === 1 ? ' turn' : ' turns')); + function updateSelectMeta(d, key) { + d = d || {}; + debug('got meta for key ' + key, d); + const lightName = shortenName(String(d.lightName || 'Light')); + const darkName = shortenName(String(d.darkName || 'Dark')); + const moves = !d.moves ? '' : + (', ' + d.moves + (d.moves === 1 ? ' turn' : ' turns')); - let opt = $('#cb_game_' + key); + let opt = $('#cb_game_' + key); - if (!d.lightName && !d.darkName && !d.moves && key !== $('#cb_board').data('gameId')) { - if (opt.length >= 1) { - opt.remove(); - } - } else { - if (opt.length === 0) { - opt = $(''); - opt.attr('id', 'cb_game_' + key); - } - - let stat = ''; - if (d.status) { - stat = ', ' + d.status; - } - - opt.data('gameId', key); - opt.data('title', lightName + ' vs. ' + darkName + moves + stat); - opt.data('then', d.timestamp || new Date(Gun.state()).getTime()); - opt.addClass('cb-game-option'); - opt.appendTo('#cb_select_game'); - updateTitle(opt); - - let select = $('#cb_select_game'); - let list = select.children('.cb-game-option').get(); - list.sort(function(a,b) { - const then_a = $(a).data('then'); - const then_b = $(b).data('then'); - return (then_a < then_b) ? 1 : (then_a === then_b) ? 0 : -1; - }); - - for (const e of list) { - $(e).appendTo(select); - } + if (!d.lightName && !d.darkName && !d.moves && key !== $('#cb_board').data('gameId')) { + if (opt.length >= 1) { + opt.remove(); + } + } else { + if (opt.length === 0) { + opt = $(''); + opt.attr('id', 'cb_game_' + key); } - let selOpt = $('#cb_game_' + $('#cb_board').data('gameId')); - if (selOpt.length === 1) { - $('#cb_select_game')[0].selectedIndex = selOpt.index(); - } else { - $('#cb_select_game')[0].selectedIndex = -1; + let stat = ''; + if (d.status) { + stat = ', ' + d.status; + } + + opt.data('gameId', key); + opt.data('title', lightName + ' vs. ' + darkName + moves + stat); + opt.data('then', d.timestamp || new Date(Gun.state()).getTime()); + opt.addClass('cb-game-option'); + opt.appendTo('#cb_select_game'); + updateTitle(opt); + + let select = $('#cb_select_game'); + let list = select.children('.cb-game-option').get(); + list.sort(function(a,b) { + const then_a = $(a).data('then'); + const then_b = $(b).data('then'); + return (then_a < then_b) ? 1 : (then_a === then_b) ? 0 : -1; + }); + + for (const e of list) { + $(e).appendTo(select); } } - }); + + let selOpt = $('#cb_game_' + $('#cb_board').data('gameId')); + if (selOpt.length === 1) { + $('#cb_select_game')[0].selectedIndex = selOpt.index(); + } else { + $('#cb_select_game')[0].selectedIndex = -1; + } + } + + let cancellers = {}; + let cancelAll = gun.get(PacoSakoUUID).get('meta').onWithCancel(function(meta) { + for (const gameId in meta) { /* use of 'in' here is deliberate */ + /* 'gameId' may include extra GUN fields like '_' */ + if (gameId.match(/^[0-9a-f]{16}$/)) { + if (!Gun.obj.is(meta[gameId])) { + updateSelectMeta(null, gameId); + if (gameId in cancellers) { + cancellers[gameId](); + delete cancellers[gameId]; + } + } else if (!(gameId in cancellers)) { + cancellers[gameId] = gun.get(meta[gameId]).onWithCancel(function(d) { + updateSelectMeta(d, gameId); + }); + } + } + } + }, { change: true }); }, 1); window.setInterval(function(){