mirror of
https://github.com/pcottle/learnGitBranching.git
synced 2025-06-28 17:00:04 +02:00
initial command line store:
This commit is contained in:
parent
ebfad44042
commit
ca938ec79e
4 changed files with 108 additions and 0 deletions
22
src/js/__tests__/CommandLineStore.spec.js
Normal file
22
src/js/__tests__/CommandLineStore.spec.js
Normal file
|
@ -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);
|
||||
});
|
||||
|
||||
});
|
19
src/js/actions/CommandLineActions.js
Normal file
19
src/js/actions/CommandLineActions.js
Normal file
|
@ -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;
|
65
src/js/stores/CommandLineStore.js
Normal file
65
src/js/stores/CommandLineStore.js
Normal file
|
@ -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;
|
|
@ -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'),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue