From ca938ec79efcdab72592260461f1b80106a8ad2f Mon Sep 17 00:00:00 2001 From: Peter Cottle Date: Sat, 4 Apr 2015 10:47:09 -0700 Subject: [PATCH] initial command line store: --- src/js/__tests__/CommandLineStore.spec.js | 22 ++++++++ src/js/actions/CommandLineActions.js | 19 +++++++ src/js/stores/CommandLineStore.js | 65 +++++++++++++++++++++++ src/js/util/debug.js | 2 + 4 files changed, 108 insertions(+) create mode 100644 src/js/__tests__/CommandLineStore.spec.js create mode 100644 src/js/actions/CommandLineActions.js create mode 100644 src/js/stores/CommandLineStore.js diff --git a/src/js/__tests__/CommandLineStore.spec.js b/src/js/__tests__/CommandLineStore.spec.js new file mode 100644 index 00000000..673aeeb4 --- /dev/null +++ b/src/js/__tests__/CommandLineStore.spec.js @@ -0,0 +1,22 @@ +var CommandLineActions = require('../actions/CommandLineActions'); +var CommandLineStore = require('../stores/CommandLineStore'); + +describe('this store', function() { + + it('starts with no entries', function() { + expect(CommandLineStore.getCommandHistoryLength()) + .toEqual(0); + }); + + it('receives new commands', function() { + var command = 'git commit; git checkout HEAD'; + CommandLineActions.submitCommand(command); + + expect(CommandLineStore.getCommandHistoryLength()) + .toEqual(1); + + expect(CommandLineStore.getCommandHistory()[0]) + .toEqual(command); + }); + +}); diff --git a/src/js/actions/CommandLineActions.js b/src/js/actions/CommandLineActions.js new file mode 100644 index 00000000..e73e46ea --- /dev/null +++ b/src/js/actions/CommandLineActions.js @@ -0,0 +1,19 @@ +"use strict"; + +var AppConstants = require('../constants/AppConstants'); +var AppDispatcher = require('../dispatcher/AppDispatcher'); + +var ActionTypes = AppConstants.ActionTypes; + +var CommandLineActions = { + + submitCommand: function(text) { + AppDispatcher.handleViewAction({ + type: ActionTypes.SUBMIT_COMMAND, + text: text + }); + } + +}; + +module.exports = CommandLineActions; diff --git a/src/js/stores/CommandLineStore.js b/src/js/stores/CommandLineStore.js new file mode 100644 index 00000000..477a5be1 --- /dev/null +++ b/src/js/stores/CommandLineStore.js @@ -0,0 +1,65 @@ +"use strict"; + +var AppConstants = require('../constants/AppConstants'); +var AppDispatcher = require('../dispatcher/AppDispatcher'); +var EventEmitter = require('events').EventEmitter; + +var assign = require('object-assign'); + +var ActionTypes = AppConstants.ActionTypes; +var COMMAND_HISTORY_KEY = 'lgb_CommandHistory'; + +var _commandHistory = []; +try { + _commandHistory = JSON.parse( + localStorage.getItem(COMMAND_HISTORY_KEY) || '[]' + ) || []; +} catch (e) { +} + +var _saveToLocalStorage = function() { + try { + localStorage.setItem( + COMMAND_HISTORY_KEY, + JSON.stringify(_commandHistory) + ); + } catch (e) { + } +}; + +var CommandLineStore = assign( +{}, +EventEmitter.prototype, +AppConstants.StoreSubscribePrototype, +{ + + getCommandHistoryLength: function() { + return _commandHistory.length; + }, + + getCommandHistory: function() { + return _commandHistory.slice(0); + }, + + dispatchToken: AppDispatcher.register(function(payload) { + var action = payload.action; + var shouldInform = false; + + switch (action.type) { + case ActionTypes.SUBMIT_COMMAND: + _commandHistory.push(action.text); + _saveToLocalStorage(); + shouldInform = true; + break; + case ActionTypes.CHANGE_FLIP_TREE_Y: + break; + } + + if (shouldInform) { + CommandLineStore.emit(AppConstants.CHANGE_EVENT); + } + }) + +}); + +module.exports = CommandLineStore; diff --git a/src/js/util/debug.js b/src/js/util/debug.js index 15e614db..ec247133 100644 --- a/src/js/util/debug.js +++ b/src/js/util/debug.js @@ -6,6 +6,8 @@ var toGlobalize = { Visuals: require('../visuals'), Git: require('../git'), CommandModel: require('../models/commandModel'), + CommandLineStore: require('../stores/CommandLineStore'), + CommandLineActions: require('../actions/CommandLineActions'), LevelActions: require('../actions/LevelActions'), LevelStore: require('../stores/LevelStore'), LocaleActions: require('../actions/LocaleActions'),