import AppDispatcher from '../dispatcher/AppDispatcher'; import {EventEmitter} from 'events'; import AppConstants from '../constants/AppConstants'; let _articles = []; function _addArticle(article) { _articles.push(article); } function _removeArticle(id) { delete _articles[id]; } class ArticleStore extends EventEmitter { constructor() { super(); this.dispatchToken = AppDispatcher.register(this.dispatcherCallback.bind(this)) } getAll() { return _articles; } emitChange() { this.emit(CHANGE_EVENT); } addChangeListener(callback) { this.on(CHANGE_EVENT, callback); } removeChangeListener(callback) { this.removeListener(CHANGE_EVENT, callback); } dispatcherCallback(action) { switch (action.type) { case ActionTypes.ARTICLE_CREATE: _addArticle(action.article); this.emitChange(); break; case ActionTypes.ARTICLE_DELETE: _removeArticle(action.articleId); this.emitChange(); break; } return true; } } export default new ArticleStore();