From a81cdb6d9dbfeac8e742caf406b59d56d3846a42 Mon Sep 17 00:00:00 2001 From: cTn Date: Fri, 6 Dec 2013 18:32:32 +0100 Subject: [PATCH] polishing serial backend and background page --- background.js | 6 +++--- js/serial_backend.js | 22 +++++++++------------- main.js | 3 +-- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/background.js b/background.js index e52eb6ce79..b6113d556d 100644 --- a/background.js +++ b/background.js @@ -10,9 +10,9 @@ function start_app() { window_child.onClosed.addListener(function() { // connectionId is passed from the script side through the chrome.runtime.getBackgroundPage refference // allowing us to automatically close the port when application shut down - if (connectionId != -1) { - chrome.serial.close(connectionId, function() { - console.log('CLEANUP: Connection to serial port was opened after application closed, closing the connection.'); + if (app_window.connectionId > 0) { + chrome.serial.close(app_window.connectionId, function(result) { + console.log('SERIAL: Connection closed - ' + result); }); } }); diff --git a/js/serial_backend.js b/js/serial_backend.js index 81eb6bbaca..fe2b4fafee 100644 --- a/js/serial_backend.js +++ b/js/serial_backend.js @@ -149,11 +149,10 @@ $(document).ready(function() { }); }); -function onOpen(openInfo) { - connectionId = openInfo.connectionId; - backgroundPage.connectionId = openInfo.connectionId; // also pass connectionId to the background page - - if (connectionId != -1) { +function onOpen(openInfo) { + if (openInfo.connectionId > 0) { + connectionId = openInfo.connectionId; + // update connected_to GUI.connected_to = GUI.connecting_to; @@ -229,20 +228,17 @@ function onOpen(openInfo) { } function onClosed(result) { - if (result) { // All went as expected - connectionId = -1; // reset connection id - backgroundPage.connectionId = connectionId; // also pass latest connectionId to the background page - + connectionId = -1; // reset connection id + + if (result) { // All went as expected sensor_status(sensors_detected = 0); // reset active sensor indicators $('#tabs > ul li').removeClass('active'); // de-select any selected tabs tab_initialize_default(); console.log('Connection closed successfully.'); } else { // Something went wrong - if (connectionId > 0) { - console.log('There was an error that happened during "connection-close" procedure.'); - notify('Failed to close serial port', 'red'); - } + console.log('There was an error that happened during "connection-close" procedure.'); + notify('Failed to close serial port', 'red'); } } diff --git a/main.js b/main.js index 372e5265d1..70fb6d038c 100644 --- a/main.js +++ b/main.js @@ -2,10 +2,9 @@ // This object is used to pass current connectionId to the backround page // so the onClosed event can close the port for us if it was left opened, without this // users can experience weird behavior if they would like to access the serial bus afterwards. -var backgroundPage; chrome.runtime.getBackgroundPage(function(result) { backgroundPage = result; - backgroundPage.connectionId = -1; + backgroundPage.app_window = window; }); var timers = new Array();