Skip to content

Instantly share code, notes, and snippets.

@aubinlrx
Forked from tmcw/notification_store.js
Last active August 29, 2015 14:26
Show Gist options
  • Save aubinlrx/5d9bbbbace9a38d77794 to your computer and use it in GitHub Desktop.
Save aubinlrx/5d9bbbbace9a38d77794 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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment