Commit af20c442 authored by Phil Hughes's avatar Phil Hughes

refactor ADD_PENDING_TAB to stop multiple state changes

parent c5a591e6
...@@ -143,7 +143,7 @@ export const openPendingTab = ({ commit, getters, dispatch, state }, file) => { ...@@ -143,7 +143,7 @@ export const openPendingTab = ({ commit, getters, dispatch, state }, file) => {
return false; return false;
} }
commit(types.ADD_PENDING_TAB, file); commit(types.ADD_PENDING_TAB, { file });
dispatch('scrollToTab'); dispatch('scrollToTab');
......
...@@ -96,50 +96,33 @@ export default { ...@@ -96,50 +96,33 @@ export default {
changed, changed,
}); });
}, },
[types.ADD_PENDING_TAB](state, file) { [types.ADD_PENDING_TAB](state, { file, keyPrefix = 'pending' }) {
const pendingTab = state.openFiles.find(f => f.path === file.path && f.pending); const pendingTab = state.openFiles.find(f => f.path === file.path && f.pending);
let openFiles = state.openFiles.map(f =>
Object.assign(f, { active: f.path === file.path, opened: false }),
);
Object.assign(state, { if (!pendingTab) {
openFiles: state.openFiles.map(f => Object.assign(f, { active: false })), const openFile = openFiles.find(f => f.path === file.path);
});
if (pendingTab) { openFiles = openFiles.concat(openFile ? null : file).reduce((acc, f) => {
Object.assign(state, { if (!f) return acc;
openFiles: state.openFiles.map(f => {
if (f.pending && f.path === file.path) {
return Object.assign(f, { active: true });
}
return f; if (f.path === file.path) {
}), return acc.concat({
}); ...f,
} else { active: true,
const openFile = state.openFiles.find(f => f.path === file.path); pending: true,
const openFiles = state.openFiles opened: true,
.concat(openFile ? null : file) key: `${keyPrefix}-${f.key}`,
.filter(f => f) });
.reduce((acc, f) => { }
if (f.path === file.path) {
return acc.concat({
...f,
active: true,
pending: true,
key: `pending-${f.key}`,
});
}
return acc.concat(f); return acc.concat(f);
}, []); }, []);
Object.assign(state, {
entries: Object.assign(state.entries, {
[file.path]: Object.assign(state.entries[file.path], {
opened: false,
}),
}),
openFiles,
});
} }
Object.assign(state, { openFiles });
}, },
[types.REMOVE_PENDING_TAB](state, file) { [types.REMOVE_PENDING_TAB](state, file) {
Object.assign(state, { Object.assign(state, {
......
...@@ -183,7 +183,7 @@ describe('Multi-file store file mutations', () => { ...@@ -183,7 +183,7 @@ describe('Multi-file store file mutations', () => {
}); });
it('adds file into openFiles as pending', () => { it('adds file into openFiles as pending', () => {
mutations.ADD_PENDING_TAB(localState, localFile); mutations.ADD_PENDING_TAB(localState, { file: localFile });
expect(localState.openFiles.length).toBe(2); expect(localState.openFiles.length).toBe(2);
expect(localState.openFiles[1].pending).toBe(true); expect(localState.openFiles[1].pending).toBe(true);
...@@ -191,7 +191,7 @@ describe('Multi-file store file mutations', () => { ...@@ -191,7 +191,7 @@ describe('Multi-file store file mutations', () => {
}); });
it('updates open file to pending', () => { it('updates open file to pending', () => {
mutations.ADD_PENDING_TAB(localState, localState.openFiles[0]); mutations.ADD_PENDING_TAB(localState, { file: localState.openFiles[0] });
expect(localState.openFiles.length).toBe(1); expect(localState.openFiles.length).toBe(1);
}); });
...@@ -202,14 +202,14 @@ describe('Multi-file store file mutations', () => { ...@@ -202,14 +202,14 @@ describe('Multi-file store file mutations', () => {
pending: true, pending: true,
}); });
mutations.ADD_PENDING_TAB(localState, localFile); mutations.ADD_PENDING_TAB(localState, { file: localFile });
expect(localState.openFiles[1].pending).toBe(true); expect(localState.openFiles[1].pending).toBe(true);
expect(localState.openFiles[1].active).toBe(true); expect(localState.openFiles[1].active).toBe(true);
}); });
it('sets all openFiles to not active', () => { it('sets all openFiles to not active', () => {
mutations.ADD_PENDING_TAB(localState, localFile); mutations.ADD_PENDING_TAB(localState, { file: localFile });
expect(localState.openFiles.length).toBe(2); expect(localState.openFiles.length).toBe(2);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment