diff --git a/js/pacosako_ui.js b/js/pacosako_ui.js index d88ba60..b291bfe 100644 --- a/js/pacosako_ui.js +++ b/js/pacosako_ui.js @@ -546,6 +546,8 @@ $(function (){ } else { $('#cb_select_game')[0].selectedIndex = -1; } + + refreshSelectOptions(); } function disableNotify(){ @@ -797,7 +799,6 @@ $(function (){ let optIndex = $('#cb_select_game')[0].selectedIndex; if (optIndex === 0) { switchGameId(randomId()); - putState(); } else if (optIndex >= 1) { let opt = $('#cb_select_game option')[optIndex]; if (opt) { @@ -822,11 +823,15 @@ $(function (){ age_str = ' (' + Math.floor((now - then) / (60*1000)) + 'm)'; } else if ((now - then) < 24*60*60*1000) { age_str = ' (' + Math.floor((now - then) / (60*60*1000)) + 'h)'; - } else if ((now - then) < 14*24*60*60*1000) { + } else { + if ((now - then) >= 14*24*60*60*1000) { + /* prune entries with no activity in 14 days, but leave the current game */ + if (opt.data('gameId') !== $('#cb_board').data('gameId')) { + opt.remove(); + return; + } + } age_str = ' (' + Math.floor((now - then) / (24*60*60*1000)) + 'd)'; - } else if (opt.data('gameId') !== $('#cb_board').data('gameId')) { - opt.remove(); - return; } const newText = opt.data('title') + age_str; @@ -835,7 +840,7 @@ $(function (){ } } - window.setTimeout(function(){ + const refreshSelectOptions = (function(){ function updateSelectMeta(d, key) { d = d || {}; debug('got meta for key ' + key, d); @@ -889,26 +894,49 @@ $(function (){ } } - let cancellers = {}; - let cancelAll = gun.get(PacoSakoUUID + '/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]; + let cancelAll = function(){}; + + return function() { + cancelAll(); + + $('#cb_select_game .cb-game-option').remove(); + updateSelectMeta(null, $('#cb_board').data('gameId')); + + const cancellers = {}; + let cancelMeta = gun.get(PacoSakoUUID + '/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); + }); } - } else if (!(gameId in cancellers)) { - cancellers[gameId] = gun.get(meta[gameId]).onWithCancel(function(d) { - updateSelectMeta(d, gameId); - }); } } - } - }, { change: true }); - }, 1); + }, { change: true }); + + cancelAll = function() { + cancelMeta(); + cancelMeta = function(){}; + for (const gameId in cancellers) { + cancellers[gameId](); + delete cancellers[gameId]; + } + cancelAll = function(){}; + }; + + return cancelAll; + }; + })(); + + refreshSelectOptions(); window.setInterval(function(){ $('#cb_select_game').first().children('.cb-game-option').each(function(idx,opt){ @@ -956,7 +984,6 @@ $(function (){ switchGameId(foundId[1]); } else { switchGameId(randomId()); - putState(); } /* Low-level commands to be run from the JS console */