Skip to content
This repository has been archived by the owner on Apr 30, 2024. It is now read-only.

WIP - History for requests #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/common/ipc-channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ export default {
RECEIVE_RESPONSE: 'receive-response',
UNEXPECTED_ERROR: 'unexpected-exception',
CANCEL_REQUEST: 'cancel-request',
REQUEST_CANCELLED: 'request-cancelled'
REQUEST_CANCELLED: 'request-cancelled',
NEXT_STATE: 'next-state',
PREVIOUS_STATE: 'previous-state',
SET_STATE: 'set-state'
};
48 changes: 48 additions & 0 deletions src/main/history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { clone, equals } from 'ramda';
import { initialState } from '../renderer/store/index';

class StateHistory {
constructor() {
this.states = [];
this.id = 0;
this.states.push(clone(initialState));
}

getCurrentState() {
return this.states[this.id];
}

currentEquals(state) {
return equals(state.request, this.getCurrentState().request) && equals(state.auth, this.getCurrentState().auth);
}

push(state) {
this.states.push(state);
this.id = this.states.length - 1;
}

length() {
return this.states.length;
}

previousState() {
if (this.id > 0) this.id -= 1;
return this.states[this.id];
}

nextState() {
if (this.id < this.states.length - 1) this.id += 1;
return this.states[this.id];
}

getStates() {
return this.states;
}

setStates(states) {
this.states = states;
this.id = this.states.length - 1;
}
}

export default StateHistory;
45 changes: 43 additions & 2 deletions src/main/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { clone, equals } from 'ramda';
import fs from 'fs';
import { app, BrowserWindow, ipcMain, Menu } from 'electron'; // eslint-disable-line import/no-extraneous-dependencies
import Channels from '../common/ipc-channels';
import RequestDispatcher from './request-dispatcher';
import StateHistory from './history';
import { initialState } from '../renderer/store';

/**
* Set `__static` path to static files in production
Expand All @@ -12,6 +16,8 @@ if (process.env.NODE_ENV !== 'development') {

let mainWindow;
const winURL = process.env.NODE_ENV === 'development' ? 'http://localhost:9080' : `file://${__dirname}/index.html`;
const stateHistory = new StateHistory();
const historyPath = '/tmp/.requestRockerHistory.json';

function createWindow() {
/**
Expand All @@ -26,7 +32,9 @@ function createWindow() {
mainWindow.loadURL(winURL);

mainWindow.on('closed', () => {
mainWindow = null;
fs.writeFile(historyPath, JSON.stringify(stateHistory.getStates()), err => {
if (!err) mainWindow = null;
});
});

const template = [
Expand Down Expand Up @@ -59,7 +67,15 @@ function createWindow() {
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
}

app.on('ready', createWindow);
app.on('ready', () => {
createWindow();
fs.access(historyPath, fs.F_OK, err => {
if (!err) {
const rawdata = fs.readFileSync(historyPath);
stateHistory.setStates(JSON.parse(rawdata));
}
});
});

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
Expand Down Expand Up @@ -90,6 +106,31 @@ ipcMain.on(Channels.CANCEL_REQUEST, async event => {
}
});

ipcMain.on(Channels.NEXT_STATE, async (event, state, forcePush = false) => {
let newState;
if (
(!stateHistory.currentEquals(state) && !equals(state, initialState)) ||
(forcePush && !equals(state, initialState))
) {
stateHistory.push(clone(state));
newState = stateHistory.nextState();
} else {
newState = stateHistory.nextState();
}
event.sender.send(Channels.SET_STATE, newState);
});

ipcMain.on(Channels.PREVIOUS_STATE, async (event, state) => {
let newState;
if (!stateHistory.currentEquals(state) && !equals(state, initialState)) {
stateHistory.push(clone(state));
newState = stateHistory.previousState();
} else {
newState = stateHistory.previousState();
}
event.sender.send(Channels.SET_STATE, newState);
});

/**
* Auto Updater
*
Expand Down
12 changes: 11 additions & 1 deletion src/renderer/components/request-editor-title.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<div class="e-fullheight__header">
<h2>Request</h2>
<div class="e-fullheight__actions">
<button
class="e-btn e-btn-link previous-state"
@click="previousState">
previous state
</button>
<button
class="e-btn e-btn-link next-state"
@click="nextState">
next state
</button>
<button
class="e-btn e-btn-link reset-state"
@click="resetState">
Expand All @@ -17,7 +27,7 @@ import { mapActions } from 'vuex';
export default {
name: 'RequestEditorTitle',
methods: {
...mapActions(['resetState'])
...mapActions(['previousState', 'nextState', 'resetState'])
}
};
</script>
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ ipcRenderer.on(Channels.UNEXPECTED_ERROR, (event, errorMessage) => {
ipcRenderer.on(Channels.REQUEST_CANCELLED, () => {
store.dispatch(Action.requestCancelled);
});

ipcRenderer.on(Channels.SET_STATE, (event, state, newState) => {
store.dispatch(Action.setState, state, newState);
});
3 changes: 3 additions & 0 deletions src/renderer/store/action-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export default {
selectContentType: 'selectContentType',
setRequestHeaders: 'setRequestHeaders',
resetState: 'resetState',
setState: 'setState',
nextState: 'nextState',
previousState: 'previousState',
indicateBackendError: 'indicateBackendError',
clearErrors: 'clearErrors',
cancelRequest: 'cancelRequest',
Expand Down
12 changes: 11 additions & 1 deletion src/renderer/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export default {
authParams: state.auth.params
};

await ipcRenderer.send(Channels.SEND_REQUEST, payload);
ipcRenderer.send(Channels.NEXT_STATE, state, true);
ipcRenderer.send(Channels.SEND_REQUEST, payload);
},
async [Action.cancelRequest]() {
await ipcRenderer.send(Channels.CANCEL_REQUEST);
Expand Down Expand Up @@ -89,6 +90,15 @@ export default {
[Action.resetState]({ commit }) {
commit(Mutation.RESET_STATE);
},
[Action.nextState]({ state }) {
ipcRenderer.send(Channels.NEXT_STATE, state);
},
[Action.previousState]({ state }) {
ipcRenderer.send(Channels.PREVIOUS_STATE, state);
},
[Action.setState]({ commit }, state, newState) {
commit(Mutation.SET_STATE, state, newState);
},
[Action.indicateBackendError]({ commit }, errorMessage) {
commit(Mutation.ADD_ERROR_MESSAGE, errorMessage);
commit(Mutation.REQUEST_FINISHED_OR_ABORTED);
Expand Down
1 change: 1 addition & 0 deletions src/renderer/store/mutation-types.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default {
UPDATE_URL: 'UPDATE_URL',
RESET_STATE: 'RESET_STATE',
SET_STATE: 'SET_STATE',
UPDATE_RESPONSE: 'UPDATE_RESPONSE',
SELECT_AUTH_TYPE: 'SELECT_AUTH_TYPE',
SET_AUTH_PARAMS: 'SET_AUTH_PARAMS',
Expand Down
9 changes: 9 additions & 0 deletions src/renderer/store/mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ export default {
[Mutation.RESET_STATE]() {
this.replaceState(clone(initialState));
},
[Mutation.SET_STATE](state, newState) {
newState.errors = [];
newState.validatorErrors = [];
newState.uuid = null;
newState.response = {};
newState.sentRequestHeaders = null;
newState.sendingInProgress = false;
this.replaceState(newState);
},
[Mutation.SET_SENT_REQUEST_HEADERS](state, requestHeaders) {
state.sentRequestHeaders = requestHeaders;
},
Expand Down
22 changes: 22 additions & 0 deletions test/unit/specs/renderer/store/actions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe('actions', () => {

store.dispatch(Action.sendRequest);

expect(ipcSpy).to.be.calledWith('next-state', sinon.match(store.state));
expect(ipcSpy).to.be.calledWith(
'send-request',
sinon.match({ requestDetails: { url: 'https://request.url' } })
Expand All @@ -88,6 +89,7 @@ describe('actions', () => {

store.dispatch(Action.sendRequest);

expect(ipcSpy).to.be.calledWith('next-state', sinon.match(store.state));
expect(ipcSpy).to.be.calledWith('send-request', sinon.match({ authType: Auth.WSSE, authParams }));
});

Expand All @@ -112,6 +114,7 @@ describe('actions', () => {
store.commit(Mutation.SELECT_HTTP_METHOD, method);
store.dispatch(Action.sendRequest);

expect(ipcSpy).to.be.calledWith('next-state', sinon.match(store.state));
expect(ipcSpy).to.be.calledWith('send-request', sinon.match({ requestDetails: { body: expectedBody } }));
});
});
Expand All @@ -123,6 +126,7 @@ describe('actions', () => {
]);
store.dispatch(Action.sendRequest);

expect(ipcSpy).to.be.calledWith('next-state', sinon.match(store.state));
expect(ipcSpy).to.be.calledWith(
'send-request',
sinon.match({
Expand All @@ -136,6 +140,7 @@ describe('actions', () => {

store.dispatch(Action.sendRequest);

expect(ipcSpy).to.be.calledWith('next-state', sinon.match(store.state));
expect(ipcSpy).to.be.calledWith('send-request', sinon.match({ uuid: 'some-random-generated-uuid' }));
});

Expand Down Expand Up @@ -163,6 +168,7 @@ describe('actions', () => {

store.dispatch(Action.sendRequest);

expect(ipcSpy).to.be.calledWith('next-state', sinon.match(store.state));
expect(ipcSpy).to.be.calledWith(
'send-request',
sinon.match({ requestDetails: { url: 'http://request.url' } })
Expand Down Expand Up @@ -311,6 +317,22 @@ describe('actions', () => {
});
});

describe('nextState', () => {
it('should send the next state', () => {
const ipcSpy = sinon.spy(ipcRenderer, 'send');
Actions[Action.nextState]({ state: 'state' });
expect(ipcSpy).to.be.calledWith('next-state', 'state');
});
});

describe('previousState', () => {
it('should send the previous state', () => {
const ipcSpy = sinon.spy(ipcRenderer, 'send');
Actions[Action.previousState]({ state: 'state' });
expect(ipcSpy).to.be.calledWith('previous-state', 'state');
});
});

describe('clearErrors', () => {
it('should commit the CLEAR_ERRORS mutation', () => {
const commit = sinon.spy();
Expand Down