ES6 SessionStore

This commit is contained in:
Luke Barnard 2017-05-12 15:58:44 +01:00
parent 6ffe7ef9b2
commit 536724e7c5
3 changed files with 49 additions and 48 deletions

View file

@ -40,7 +40,7 @@ var PageTypes = require('../../PageTypes');
var createRoom = require("../../createRoom"); var createRoom = require("../../createRoom");
import * as UDEHandler from '../../UnknownDeviceErrorHandler'; import * as UDEHandler from '../../UnknownDeviceErrorHandler';
import getSessionStore from '../../stores/SessionStore'; import sessionStore from '../../stores/SessionStore';
module.exports = React.createClass({ module.exports = React.createClass({
displayName: 'MatrixChat', displayName: 'MatrixChat',
@ -251,7 +251,7 @@ module.exports = React.createClass({
}); });
} }
this._sessionStore = getSessionStore(); this._sessionStore = sessionStore;
this._sessionStore.on('update', this._setStateFromSessionStore); this._sessionStore.on('update', this._setStateFromSessionStore);
this._setStateFromSessionStore(); this._setStateFromSessionStore();
}, },

View file

@ -22,7 +22,7 @@ var Modal = require("../../../Modal");
var sdk = require("../../../index"); var sdk = require("../../../index");
import AccessibleButton from '../elements/AccessibleButton'; import AccessibleButton from '../elements/AccessibleButton';
import getSessionStore from '../../../stores/SessionStore'; import sessionStore from '../../../stores/SessionStore';
module.exports = React.createClass({ module.exports = React.createClass({
displayName: 'ChangePassword', displayName: 'ChangePassword',
@ -68,7 +68,7 @@ module.exports = React.createClass({
}, },
componentWillMount: function() { componentWillMount: function() {
this.sessionStore = getSessionStore(); this.sessionStore = sessionStore;
this.sessionStore.on('update', this.setStateFromSessionStore); this.sessionStore.on('update', this.setStateFromSessionStore);
this.setStateFromSessionStore(); this.setStateFromSessionStore();

View file

@ -6,19 +6,19 @@ import EventEmitter from 'events';
* store that listens for actions and updates its state accordingly, informing any * store that listens for actions and updates its state accordingly, informing any
* listeners (views) of state changes via the 'update' event. * listeners (views) of state changes via the 'update' event.
*/ */
function SessionStore() { class SessionStore extends EventEmitter {
constructor() {
super();
// Initialise state // Initialise state
this._state = { this._state = {
cachedPassword: localStorage.getItem('mx_pass'), cachedPassword: localStorage.getItem('mx_pass'),
}; };
dis.register(this._onAction.bind(this)); dis.register(this._onAction.bind(this));
} }
// Inherit from EventEmitter _update() {
SessionStore.prototype = EventEmitter.prototype;
SessionStore.prototype._update = function() {
// Persist state to localStorage // Persist state to localStorage
if (this._state.cachedPassword) { if (this._state.cachedPassword) {
localStorage.setItem('mx_pass', this._state.cachedPassword); localStorage.setItem('mx_pass', this._state.cachedPassword);
@ -27,14 +27,14 @@ SessionStore.prototype._update = function() {
} }
this.emit('update'); this.emit('update');
}; }
SessionStore.prototype._setState = function(newState) { _setState(newState) {
this._state = Object.assign(this._state, newState); this._state = Object.assign(this._state, newState);
this._update(); this._update();
}; }
SessionStore.prototype._onAction = function(payload) { _onAction(payload) {
switch (payload.action) { switch (payload.action) {
case 'cached_password': case 'cached_password':
this._setState({ this._setState({
@ -47,11 +47,12 @@ SessionStore.prototype._onAction = function(payload) {
}); });
break; break;
} }
}; }
SessionStore.prototype.getCachedPassword = function() { getCachedPassword() {
return this._state.cachedPassword; return this._state.cachedPassword;
}; }
}
// Export singleton getter // Export singleton getter
let singletonSessionStore = null; let singletonSessionStore = null;