Implement SessionStore
This wraps session-related state into a basic flux store. The localStorage item 'mx_pass' is the only thing managed by this store for now but it could easily be extended to track other items (like the teamToken which is passed around through props a lot)
This commit is contained in:
parent
8725ef3863
commit
1176573f39
6 changed files with 104 additions and 32 deletions
63
src/stores/SessionStore.js
Normal file
63
src/stores/SessionStore.js
Normal file
|
@ -0,0 +1,63 @@
|
|||
import dis from '../dispatcher';
|
||||
import EventEmitter from 'events';
|
||||
|
||||
/**
|
||||
* A class for storing application state to do with the session. This is a simple flux
|
||||
* store that listens for actions and updates its state accordingly, informing any
|
||||
* listeners (views) of state changes via the 'update' event.
|
||||
*/
|
||||
function SessionStore() {
|
||||
// Initialise state
|
||||
this._state = {
|
||||
cachedPassword: localStorage.getItem('mx_pass'),
|
||||
};
|
||||
|
||||
dis.register(this._onAction.bind(this));
|
||||
}
|
||||
|
||||
// Inherit from EventEmitter
|
||||
SessionStore.prototype = EventEmitter.prototype;
|
||||
|
||||
SessionStore.prototype._update = function() {
|
||||
// Persist state to localStorage
|
||||
if (this._state.cachedPassword) {
|
||||
localStorage.setItem('mx_pass', this._state.cachedPassword);
|
||||
} else {
|
||||
localStorage.removeItem('mx_pass', this._state.cachedPassword);
|
||||
}
|
||||
|
||||
this.emit('update');
|
||||
};
|
||||
|
||||
SessionStore.prototype._setState = function(newState) {
|
||||
this._state = Object.assign(this._state, newState);
|
||||
this._update();
|
||||
};
|
||||
|
||||
SessionStore.prototype._onAction = function(payload) {
|
||||
switch (payload.action) {
|
||||
case 'cached_password':
|
||||
this._setState({
|
||||
cachedPassword: payload.cachedPassword,
|
||||
});
|
||||
break;
|
||||
case 'password_changed':
|
||||
this._setState({
|
||||
cachedPassword: null,
|
||||
});
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
SessionStore.prototype.getCachedPassword = function() {
|
||||
return this._state.cachedPassword;
|
||||
};
|
||||
|
||||
// Export singleton getter
|
||||
let singletonSessionStore = null;
|
||||
export default function getSessionStore() {
|
||||
if (!singletonSessionStore) {
|
||||
singletonSessionStore = new SessionStore();
|
||||
}
|
||||
return singletonSessionStore;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue