ChatStore.js 998 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 _isChatHidden = false;
  10. var ChatStore = Object.assign({}, EventEmitter.prototype, {
  11. getState() {
  12. return {
  13. messages: _messages,
  14. isChatHidden: _isChatHidden
  15. };
  16. }
  17. });
  18. AppDispatcher.register(payload => {
  19. var action = payload.action;
  20. switch (action.actionType) {
  21. case ChatConstants.TOGGLE_CHAT:
  22. _isChatHidden = !_isChatHidden;
  23. break;
  24. case ChatConstants.SUBMIT_MESSAGE:
  25. _messages = _messages.push(Map({
  26. message: action.message,
  27. className: action.className
  28. }));
  29. break;
  30. default:
  31. return true;
  32. }
  33. ChatStore.emit(CHANGE_EVENT);
  34. return true;
  35. });
  36. module.exports = ChatStore;