Code coverage report for src/flux/notifications.coffee

Statements: 25.64% (10 / 39)      Branches: 0% (0 / 12)      Functions: 12.5% (1 / 8)      Lines: 25.64% (10 / 39)      Ignored: none     

All files » src/flux/ » notifications.coffee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79                                                                                                                     99×                      
var CrudConfig, Notifications, NotificationsConfig, POLL_INTERVAL, STORAGE_KEY, _, actions, extendConfig, makeSimpleStore, ref, ref1, store,
  slice = [].slice;
 
ref = require('./helpers'), CrudConfig = ref.CrudConfig, makeSimpleStore = ref.makeSimpleStore, extendConfig = ref.extendConfig;
 
_ = require('underscore');
 
STORAGE_KEY = 'ox-notifications';
 
POLL_INTERVAL = 5 * 60 * 1000;
 
NotificationsConfig = {
  activeNotifications: {},
  _asyncStatus: {},
  startPolling: function(windowImpl) {
    if (windowImpl == null) {
      windowImpl = window;
    }
    if (this.polling) {
      return;
    }
    this.polling = windowImpl.setInterval(Notifications.actions.pollForUpdate, POLL_INTERVAL);
    this.windowImpl = windowImpl;
    return Notifications.actions.pollForUpdate();
  },
  pollForUpdate: function() {
    if (this.windowImpl.document.hidden !== true) {
      return Notifications.actions.loadUpdates();
    }
  },
  loadUpdates: function() {},
  loadedUpdates: function(notifications) {
    var currentIds, i, len, newActiveNotices, notice, observedIds, outdatedIds;
    observedIds = this._getObservedNoticeIds();
    newActiveNotices = {};
    currentIds = [];
    for (i = 0, len = notifications.length; i < len; i++) {
      notice = notifications[i];
      currentIds.push(notice.id);
      if (observedIds.indexOf(notice.id) !== -1) {
        continue;
      }
      newActiveNotices[notice.id] = notice;
    }
    this.activeNotifications = newActiveNotices;
    this.emitChange();
    outdatedIds = _.difference(observedIds, _.without.apply(_, [currentIds].concat(slice.call(_.keys(newActiveNotices)))));
    if (!_.isEmpty(outdatedIds)) {
      return this._setObservedNoticeIds(_.without.apply(_, [observedIds].concat(slice.call(outdatedIds))));
    }
  },
  _getObservedNoticeIds: function() {
    return JSON.parse(this.windowImpl.localStorage.getItem(STORAGE_KEY) || '[]');
  },
  _setObservedNoticeIds: function(newIds) {
    return this.windowImpl.localStorage.setItem(STORAGE_KEY, JSON.stringify(newIds));
  },
  acknowledge: function(notice_id) {
    this._setObservedNoticeIds(this._getObservedNoticeIds().concat(notice_id));
    delete this.activeNotifications[notice_id];
    return this.emitChange();
  },
  exports: {
    getActiveNotifications: function() {
      return _.values(this.activeNotifications);
    }
  }
};
 
extendConfig(NotificationsConfig, new CrudConfig);
 
Notifications = (ref1 = makeSimpleStore(NotificationsConfig), actions = ref1.actions, store = ref1.store, ref1);
 
module.exports = {
  NotificationActions: actions,
  NotificationStore: store
};