Proper cleanup on logout

This commit is contained in:
Paulus Schoutsen 2017-01-29 22:56:04 -08:00
parent a1057681f1
commit 264731961b
2 changed files with 49 additions and 19 deletions

View File

@ -29,11 +29,11 @@
></home-assistant-main> ></home-assistant-main>
</template> </template>
<template is='dom-if' if='[[!showMain]]'> <template is='dom-if' if='[[!showMain]]' restamp>
<login-form <login-form
hass='[[hass]]' hass='[[hass]]'
connection-promise='{{connectionPromise}}' connection-promise='{{connectionPromise}}'
show-loading='[[computeShowLoading(connectionPromise, iconsLoaded)]]'> show-loading='[[computeShowLoading(connectionPromise, hass, iconsLoaded)]]'>
</login-form> </login-form>
</template> </template>
</template> </template>
@ -86,8 +86,10 @@ Polymer({
return hass && hass.states && hass.config && iconsLoaded; return hass && hass.states && hass.config && iconsLoaded;
}, },
computeShowLoading: function (connectionPromise) { computeShowLoading: function (connectionPromise, hass, iconsLoaded) {
return connectionPromise != null; // Show loading when connecting or when connected but not all pieces loaded yet
return (connectionPromise != null ||
(hass && (!hass.states || !hass.config || !iconsLoaded)));
}, },
loadIcons: function () { loadIcons: function () {
@ -104,7 +106,11 @@ Polymer({
}); });
}, },
connectionChanged: function (conn) { connectionChanged: function (conn, oldConn) {
if (oldConn) {
this.unsubConnection();
this.unsubConnection = null;
}
if (!conn) { if (!conn) {
this.hass = null; this.hass = null;
return; return;
@ -144,21 +150,40 @@ Polymer({
}, },
}, this.$.storage.getStoredState()); }, this.$.storage.getStoredState());
conn.addEventListener('ready', function () { var reconnected = function () {
this.hass = Object.assign({}, this.hass, { connected: true }); this.hass = Object.assign({}, this.hass, { connected: true });
}.bind(this)); }.bind(this);
conn.addEventListener('disconnected', function () { conn.addEventListener('ready', reconnected);
var disconnected = function () {
this.hass = Object.assign({}, this.hass, { connected: false }); this.hass = Object.assign({}, this.hass, { connected: false });
}.bind(this)); }.bind(this)
conn.addEventListener('disconnected', disconnected);
var unsubEntities;
window.HAWS.subscribeEntities(conn, function (states) { window.HAWS.subscribeEntities(conn, function (states) {
this.hass = Object.assign({}, this.hass, { states: states }); this.hass = Object.assign({}, this.hass, { states: states });
}.bind(this)); }.bind(this)).then(function (unsub) {
unsubEntities = unsub;
});
var unsubConfig;
window.HAWS.subscribeConfig(conn, function (config) { window.HAWS.subscribeConfig(conn, function (config) {
this.hass = Object.assign({}, this.hass, { config: config }); this.hass = Object.assign({}, this.hass, { config: config });
}.bind(this)); }.bind(this)).then(function (unsub) {
unsubConfig = unsub;
});
this.unsubConnection = function () {
conn.removeEventListener('ready', reconnected);
conn.removeEventListener('disconnected', disconnected);
unsubEntities();
unsubConfig();
}
}, },
handleConnectionPromise: function (prom) { handleConnectionPromise: function (prom) {
@ -210,11 +235,16 @@ Polymer({
}, },
handleLogout: function () { handleLogout: function () {
this.connection.close();
delete localStorage.authToken; delete localStorage.authToken;
this.connection = null; var conn = this.connection;
this.connectionPromise = null; this.connectionPromise = null;
this.hass = null; try {
this.connection = null;
} catch (err) {
// home-assistant-main crashes when hass is set to null.
// However, after it is done, home-assistant-main is removed from the DOM by this element.
}
conn.close();
}, },
ready: function () { ready: function () {

View File

@ -48,7 +48,7 @@
<img src="/static/icons/favicon-192x192.png" height="192" /> <img src="/static/icons/favicon-192x192.png" height="192" />
<a href="#" id="hideKeyboardOnFocus"></a> <a href="#" id="hideKeyboardOnFocus"></a>
<div class='interact'> <div class='interact'>
<div id='loginform' hidden$="[[showLoading]]"> <div id='loginform' hidden$="[[showSpinner]]">
<paper-input-container <paper-input-container
id="passwordDecorator" id="passwordDecorator"
invalid="[[errorMessage]]" invalid="[[errorMessage]]"
@ -67,7 +67,7 @@
<paper-button id='loginButton'>Log In</paper-button> <paper-button id='loginButton'>Log In</paper-button>
</div> </div>
</div> </div>
<div id="validatebox" hidden$="[[!showLoading]]"> <div id="validatebox" hidden$="[[!showSpinner]]">
<paper-spinner active="true"></paper-spinner><br /> <paper-spinner active="true"></paper-spinner><br />
<div class="validatemessage">Loading data</div> <div class="validatemessage">Loading data</div>
</div> </div>
@ -102,14 +102,14 @@ Polymer({
value: false, value: false,
}, },
forceShowLoading: { showLoading: {
type: Boolean, type: Boolean,
value: false, value: false,
}, },
showLoading: { showSpinner: {
type: Boolean, type: Boolean,
computed: 'computeShowSpinner(forceShowLoading, isValidating)', computed: 'computeShowSpinner(showLoading, isValidating)',
}, },
password: { password: {