ChatStore.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. 'use strict';
  2. const AppDispatcher = require('../dispatcher/AppDispatcher');
  3. const EventEmitter = require('eventemitter2').EventEmitter2;
  4. const ChatConstants = require('../constants/ChatConstants');
  5. const Immutable = require('immutable');
  6. const {List, Map} = Immutable;
  7. const CHANGE_EVENT = 'change';
  8. var _messages = List();
  9. var _unseenCount = 0;
  10. var _isChatHidden = true;
  11. const ChatStore = Object.assign({}, EventEmitter.prototype, {
  12. getState() {
  13. return {
  14. messages: _messages,
  15. unseenCount: _unseenCount,
  16. isChatHidden: _isChatHidden
  17. };
  18. }
  19. });
  20. function toggleVisibility() {
  21. _isChatHidden = !_isChatHidden;
  22. _unseenCount = 0;
  23. }
  24. function submitMessage(message, className, received) {
  25. _messages = _messages.push(Map({
  26. message: message,
  27. className: className
  28. }));
  29. if (received && _isChatHidden) {
  30. _unseenCount += 1;
  31. }
  32. }
  33. AppDispatcher.register(payload => {
  34. const action = payload.action;
  35. switch (action.actionType) {
  36. case ChatConstants.TOGGLE_VISIBILITY:
  37. toggleVisibility();
  38. break;
  39. case ChatConstants.SUBMIT_MESSAGE:
  40. submitMessage(action.message, action.className, action.received);
  41. break;
  42. default:
  43. return true;
  44. }
  45. ChatStore.emit(CHANGE_EVENT);
  46. return true;
  47. });
  48. module.exports = ChatStore;