dispatch/client/src/js/stores/inputHistory.js
2015-06-02 00:09:36 +02:00

64 lines
1002 B
JavaScript

var Reflux = require('reflux');
var actions = require('../actions/inputHistory');
var HISTORY_MAX_LENGTH = 128;
var history = [];
var index = -1;
var stored = localStorage.inputHistory;
if (stored) {
history = JSON.parse(stored);
}
function getState() {
if (index !== -1) {
return history[index];
}
return null;
}
var inputHistoryStore = Reflux.createStore({
init() {
this.listenToMany(actions);
},
add(line) {
if (line.trim() && line !== history[0]) {
history.unshift(line);
if (history.length > HISTORY_MAX_LENGTH) {
history.pop();
}
localStorage.inputHistory = JSON.stringify(history);
}
},
reset() {
if (index !== -1) {
index = -1;
this.trigger(history[index]);
}
},
increment() {
if (index !== history.length - 1) {
index++;
this.trigger(history[index]);
}
},
decrement() {
if (index !== -1) {
index--;
this.trigger(history[index]);
}
},
getInitialState: getState,
getState
});
module.exports = inputHistoryStore;