notify, and stop using, when another tab starts up

I'm wrestling with how to have concurrent tabs open. They can't be on
the same game because both can't connect using same MQTT dev id (and
they'd quickly get the saved state screwed up.) Only problem is
sometimes a tab in a new window doesn't have access to localstorage and
so is able to connect on its own. But later that state isn't reachable
any more: relaunch firefox and you get back the to the older state.
This commit is contained in:
Eric House 2021-02-13 12:02:26 -08:00
parent dc90823e9b
commit 5a061f5786
3 changed files with 22 additions and 1 deletions

View file

@ -569,6 +569,14 @@ updateScreen( Globals* globals, bool doSave )
}
}
static void
doExit( Globals* globals )
{
call_alert( "Control passed to another tab" );
XP_MEMSET( globals, 0, sizeof(*globals) ); /* stop everything :-) */
// emscripten_cancel_main_loop(); <-- does nothing
}
static void
looper( void* closure )
{
@ -605,6 +613,8 @@ button( void* closure, const char* msg )
} else if ( 0 == strcmp(msg, "vals") ) {
globals->cp.tvType = (globals->cp.tvType + 1) % TVT_N_ENTRIES;
draw = board_prefsChanged( board, &globals->cp );
} else if ( 0 == strcmp(msg, "exit") ) {
doExit( globals );
}
if ( draw ) {

View file

@ -55,6 +55,7 @@
position: absolute;
text-align: left;
z-index: 10000;
white-space: pre-wrap;
left: 33%;
top: 250px;
width: 33%;

View file

@ -16,7 +16,17 @@ function callButton(obj) {
}
function onHaveDevID(closure, devid) {
console.log('got ' + devid);
// Set a unique tag so we know if somebody comes along later
let tabID = Math.random();
localStorage.setItem('tabID', tabID);
window.addEventListener('storage', function () {
newTabID = localStorage.getItem('tabID');
if ( newTabID != tabID ) {
state.client.disconnect();
Module.ccall('button', null, ['number', 'string'], [state.closure, 'exit']);
}
} );
state.closure = closure;
document.getElementById("mqtt_span").textContent=devid;