Skip to content

Instantly share code, notes, and snippets.

@tappleby
Created March 5, 2015 01:36
Show Gist options
  • Save tappleby/21fecabc738f78bbf2fe to your computer and use it in GitHub Desktop.
Save tappleby/21fecabc738f78bbf2fe to your computer and use it in GitHub Desktop.
Example session actions, store and cache w/ flummox.
var { Actions } = require('flummox');
var Promise = require('bluebird');
var assign = require('object-assign');
class SessionActions extends Actions {
constructor(api, flux) {
super();
this.api = api;
this.flux = flux;
}
fetchSession(username, password) {
return this.api.auth.login(username, password).then(res => res.body);
}
login(username, password) {
var fetchSession = this.fetchSession(username, password);
var fetchUser = fetchSession.then(res => {
// Normally you wouldn't have to worry about the access token but it is not set on
// the api client until both fetchSession and fetchUser resolve.
this.receiveSession(res.access_token, res.user_id);
return this.flux.getActions('core.users').fetchUser(res.user_id);
});
return Promise.join(
fetchSession,
fetchUser,
(session, user) => assign({}, session, { user }));
}
logout() {
// No payload.
return true;
}
receiveSession(accessToken, userId) {
return { accessToken, userId };
}
}
module.exports = SessionActions;
class SessionCache {
constructor(sessionActions, sessionStore, cacheKey) {
this.sessionActions = sessionActions;
this.sessionStore = sessionStore;
this.sessionStore.on('change', this._handleSessionChange.bind(this));
this.cacheKey = cacheKey;
}
load() {
var data = sessionStorage.getItem(this.cacheKey);
if (data) {
var { userId, accessToken } = JSON.parse(data);
this.sessionActions.receiveSession(accessToken, userId);
}
}
_handleSessionChange() {
if (this.sessionStore.isLoggedIn()) {
var data = {
accessToken: this.sessionStore.getAccessToken(),
userId: this.sessionStore.getUserId()
};
sessionStorage.setItem(this.cacheKey, JSON.stringify(data));
} else {
sessionStorage.removeItem(this.cacheKey)
}
}
}
module.exports = SessionCache;
var { Store } = require('flummox');
class SessionStore extends Store {
constructor(sessionActions) {
super();
this.registerAsync(
sessionActions.login,
this._handleLoginBegin,
this._handleLoginSuccess,
this._handleLoginFailed
);
this.register(sessionActions.logout, this._handleLogout);
this.register(sessionActions.receiveSession, this._handleLoginSuccess);
this.state = {};
}
// Getters
isLoading() {
return !!this.state.loading;
}
isLoggedIn() {
return !!this.state.accessToken;
}
getUserId() {
return this.state.userId;
}
getAccessToken() {
return this.state.accessToken;
}
hasErrors() {
return !!this.state.errorMessage;
}
getErrorMessage() {
return this.state.errorMessage;
}
// Handlers
_handleLoginBegin() {
this.replaceState({ loading: true });
}
_handleLoginSuccess(payload) {
this.replaceState({
accessToken: payload.access_token || payload.accessToken,
userId: payload.user_id || payload.userId,
loading: false
});
}
_handleLoginFailed(err) {
this.replaceState({
errorMessage: err.message
});
}
_handleLogout() {
this.replaceState({ });
}
}
module.exports = SessionStore;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment