Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created May 19, 2015 19:56
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tmcw/a64c72a979d9547647d8 to your computer and use it in GitHub Desktop.
Save tmcw/a64c72a979d9547647d8 to your computer and use it in GitHub Desktop.
var Dispatcher = require('the-dispatcher'),
Immutable = require('immutable'),
NotificationConstants = require('../constants/notification_constants'),
UserConstants = require('../constants/user_constants'),
UploadConstants = require('../constants/upload_constants'),
StylesheetConstants = require('../constants/stylesheet_constants'),
makeStore = require('makestore');
/**
* The Notification Store does nothing but store recent actions
* in order to power notifications.
*/
var _notifications = Immutable.Set([]);
var _errors = Immutable.List();
var _successes = Immutable.List();
var NotificationStore = makeStore({
notifications: () => _notifications,
errors: () => _errors,
successes: () => _successes,
dispatcherIndex: Dispatcher.register(action => {
switch (action.actionType) {
case NotificationConstants.NOTIFICATION_DISMISS:
_notifications = Immutable.Set([]);
break;
case NotificationConstants.NOTIFICATION_ERROR_DISMISS:
_errors = _errors.shift();
break;
case NotificationConstants.NOTIFICATION_ERROR:
_errors = _errors.push(action.error);
break;
case NotificationConstants.NOTIFICATION_PAYMENT_ERROR:
_errors = _errors.push(action.error);
// ASK USER TO PAY
break;
case NotificationConstants.NOTIFICATION_UPGRADE_ERROR:
_errors = _errors.push(action.error);
// ASK USER TO UPGRADE
break;
case NotificationConstants.NOTIFICATION_SUCCESS_DISMISS:
_successes = _successes.shift();
break;
case StylesheetConstants.STYLESHEET_MIGRATE_SUCCESS:
_successes = _successes.push('Stylesheet successfully migrated to latest version.');
break;
case UserConstants.USER_UPDATED:
_successes = _successes.push('Profile successfully updated.');
break;
case UploadConstants.UPLOAD_SET:
_notifications = _notifications.union(Immutable.Set(
action.value
.filter(upload => !upload.complete)
.map(upload => `Uploading ${upload.dataset}`)));
break;
default:
// avoid emitting changes for other events.
return true;
}
NotificationStore.emitChange(action.actionType);
return true;
})
});
NotificationStore.setMaxListeners(1000);
module.exports = NotificationStore;
@badnorseman
Copy link

Very nice implementation of Flux store. Do you call the Utils Ajax requests from the ActionCreators which is called from View (by user) or Component (e.g. ComponentDidMount)?
It would be great to see how your makestore.js looks like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment