fixed background polling interrupting calls to start or quit an app

This commit is contained in:
R. Aidan Campbell 2016-08-26 13:56:24 -04:00
parent 6b8ac65d7f
commit 5c925faf34

View File

@ -1,4 +1,5 @@
var hosts = {}; // hosts is an associative array of NvHTTP objects, keyed by server UID var hosts = {}; // hosts is an associative array of NvHTTP objects, keyed by server UID
var activePolls = {}; // hosts currently being polled. An associated array of polling IDs, keyed by server UID
var pairingCert; var pairingCert;
var myUniqueid; var myUniqueid;
var api; var api;
@ -60,7 +61,7 @@ function beginBackgroundPollingOfHost(host) {
host.initialPing(function () { // initial attempt was a success host.initialPing(function () { // initial attempt was a success
$("#hostgrid-" + host.serverUid).removeClass('host-cell-inactive'); $("#hostgrid-" + host.serverUid).removeClass('host-cell-inactive');
window.setInterval(function() { activePolls[host.serverUid] = window.setInterval(function() {
// every 5 seconds, poll at the address we know it was live at // every 5 seconds, poll at the address we know it was live at
host.refreshServerInfoAtAddress(host.address).then(function (onSuccess){ host.refreshServerInfoAtAddress(host.address).then(function (onSuccess){
$("#hostgrid-" + host.serverUid).removeClass('host-cell-inactive'); $("#hostgrid-" + host.serverUid).removeClass('host-cell-inactive');
@ -70,7 +71,7 @@ function beginBackgroundPollingOfHost(host) {
}, 5000); }, 5000);
}, function () { // initial attempt was a failure }, function () { // initial attempt was a failure
$("#hostgrid-" + host.serverUid).addClass('host-cell-inactive'); $("#hostgrid-" + host.serverUid).addClass('host-cell-inactive');
window.setInterval(function() { activePolls[host.serverUid] = window.setInterval(function() {
if(host.refreshServerInfoAtAddress(host.address)) { if(host.refreshServerInfoAtAddress(host.address)) {
$("#hostgrid-" + host.serverUid).removeClass('host-cell-inactive'); $("#hostgrid-" + host.serverUid).removeClass('host-cell-inactive');
} else { } else {
@ -80,6 +81,12 @@ function beginBackgroundPollingOfHost(host) {
}); });
} }
function stopBackgroundPollingOfHost(host) {
console.log('stopping background polling of server: ' + host.toString());
window.clearInterval(activePolls[host.serverUid]);
delete activePolls[host.serverUid];
}
function snackbarLog(givenMessage) { function snackbarLog(givenMessage) {
console.log(givenMessage); console.log(givenMessage);
var data = { var data = {
@ -137,9 +144,9 @@ function pairTo(nvhttpHost, onSuccess, onFailure) {
return; return;
} }
api = nvhttpHost; var _api = nvhttpHost;
api.refreshServerInfo().then(function (ret) { _api.refreshServerInfo().then(function (ret) {
if (api.paired) { if (_api.paired) {
onSuccess(); onSuccess();
return; return;
} }
@ -150,15 +157,15 @@ function pairTo(nvhttpHost, onSuccess, onFailure) {
pairingDialog.showModal(); pairingDialog.showModal();
console.log('sending pairing request to ' + nvhttpHost.address + ' with random number ' + randomNumber); console.log('sending pairing request to ' + nvhttpHost.address + ' with random number ' + randomNumber);
api.pair(randomNumber).then(function (paired) { _api.pair(randomNumber).then(function (paired) {
if (!paired) { if (!paired) {
if (api.currentGame != 0) { if (_api.currentGame != 0) {
$('#pairingDialogText').html('Error: ' + nvhttpHost.address + ' is in app. Cannot pair until the app is stopped.'); $('#pairingDialogText').html('Error: ' + nvhttpHost.address + ' is in app. Cannot pair until the app is stopped.');
} else { } else {
$('#pairingDialogText').html('Error: failed to pair with ' + nvhttpHost.address + '. failure reason unknown.'); $('#pairingDialogText').html('Error: failed to pair with ' + nvhttpHost.address + '. failure reason unknown.');
} }
console.log('failed API object: '); console.log('failed API object: ');
console.log(api.toString()); console.log(_api.toString());
onFailure(); onFailure();
return; return;
} }
@ -170,14 +177,15 @@ function pairTo(nvhttpHost, onSuccess, onFailure) {
snackbarLog('Failed pairing to: ' + nvhttpHost.address); snackbarLog('Failed pairing to: ' + nvhttpHost.address);
console.log('pairing failed, and returned ' + failedPairing); console.log('pairing failed, and returned ' + failedPairing);
console.log('failed API object: '); console.log('failed API object: ');
console.log(api.toString()); console.log(_api.toString());
onFailure(); onFailure();
}); });
}, function (failedRefreshInfo) { }, function (failedRefreshInfo) {
snackbarLog('Failed to connect to ' + nvhttpHost.address + '! Are you sure the host is on?'); snackbarLog('Failed to connect to ' + nvhttpHost.address + '! Are you sure the host is on?');
console.log('Returned error was: ' + failedRefreshInfo); console.log('Returned error was: ' + failedRefreshInfo);
console.log('failed API object: '); console.log('failed API object: ');
console.log(api.toString()); console.log(_api.toString());
onFailure();
}); });
} }
@ -186,7 +194,6 @@ function hostChosen(sourceEvent) {
if(sourceEvent && sourceEvent.srcElement) { if(sourceEvent && sourceEvent.srcElement) {
if (sourceEvent.srcElement.innerText == "") { if (sourceEvent.srcElement.innerText == "") {
console.log('user clicked image. we gotta hack to parse out the host.'); console.log('user clicked image. we gotta hack to parse out the host.');
// TODO: parse server UID from the cell ID: 'hostgrid-' + host.serverUid
var serverUid = sourceEvent.currentTarget.id.substring("hostgrid-".length); var serverUid = sourceEvent.currentTarget.id.substring("hostgrid-".length);
} else { } else {
console.log('parsing host from grid element.'); console.log('parsing host from grid element.');
@ -203,10 +210,11 @@ function hostChosen(sourceEvent) {
} }
api.refreshServerInfo().then(function (ret) { api.refreshServerInfo().then(function (ret) {
if(!api.paired) { if(!api.paired) {
pairTo(api, function(){ showApps(); saveHosts(); }, function(){}); pairTo(api, function(){ showApps(api); saveHosts();}, function(){});
} else { } else {
showApps(); showApps(api);
} }
stopBackgroundPollingOfHost(api);
}, function (failedRefreshInfo) { }, function (failedRefreshInfo) {
snackbarLog('Failed to connect to ' + api.address + '! Are you sure the host is on?'); snackbarLog('Failed to connect to ' + api.address + '! Are you sure the host is on?');
console.log('Returned error was: ' + failedRefreshInfo); console.log('Returned error was: ' + failedRefreshInfo);
@ -240,10 +248,10 @@ function addHostToGrid(host) {
function continueAddHost() { function continueAddHost() {
var inputHost = $('#dialogInputHost').val(); var inputHost = $('#dialogInputHost').val();
var nvhttpHost = new NvHTTP(inputHost, myUniqueid, inputHost); var _nvhttpHost = new NvHTTP(inputHost, myUniqueid, inputHost);
pairTo(nvhttpHost, pairTo(_nvhttpHost,
function() { function() {
addHostToGrid(nvhttpHost); addHostToGrid(_nvhttpHost);
saveHosts(); saveHosts();
document.querySelector('#addHostDialog').close(); document.querySelector('#addHostDialog').close();
}, },
@ -334,6 +342,9 @@ function showHostsAndSettingsMode() {
$("#main-content").removeClass("fullscreen"); $("#main-content").removeClass("fullscreen");
$("#listener").removeClass("fullscreen"); $("#listener").removeClass("fullscreen");
$("body").css('backgroundColor', 'white'); $("body").css('backgroundColor', 'white');
if(api && !activePolls[api.serverUid]) {
beginBackgroundPollingOfHost(api);
}
} }
function showAppsMode() { function showAppsMode() {