diff options
author | Phil Hughes <me@iamphill.com> | 2018-03-28 13:19:14 +0300 |
---|---|---|
committer | Phil Hughes <me@iamphill.com> | 2018-03-28 16:08:36 +0300 |
commit | af20c4423c85c263f34a09c276a7bb917e3554a5 (patch) | |
tree | 94ec7a83723596ebe40b71ed54372ee29d1da784 /app/assets/javascripts/ide/stores/mutations | |
parent | c5a591e61d3476531b67eac4315af6c60efc8228 (diff) |
refactor ADD_PENDING_TAB to stop multiple state changes
Diffstat (limited to 'app/assets/javascripts/ide/stores/mutations')
-rw-r--r-- | app/assets/javascripts/ide/stores/mutations/file.js | 59 |
1 files changed, 21 insertions, 38 deletions
diff --git a/app/assets/javascripts/ide/stores/mutations/file.js b/app/assets/javascripts/ide/stores/mutations/file.js index 25faac7c828..f8d0262e120 100644 --- a/app/assets/javascripts/ide/stores/mutations/file.js +++ b/app/assets/javascripts/ide/stores/mutations/file.js @@ -96,50 +96,33 @@ export default { 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); + let openFiles = state.openFiles.map(f => + Object.assign(f, { active: f.path === file.path, opened: false }), + ); - Object.assign(state, { - openFiles: state.openFiles.map(f => Object.assign(f, { active: false })), - }); + if (!pendingTab) { + const openFile = openFiles.find(f => f.path === file.path); - if (pendingTab) { - Object.assign(state, { - openFiles: state.openFiles.map(f => { - if (f.pending && f.path === file.path) { - return Object.assign(f, { active: true }); - } + openFiles = openFiles.concat(openFile ? null : file).reduce((acc, f) => { + if (!f) return acc; - return f; - }), - }); - } else { - const openFile = state.openFiles.find(f => f.path === file.path); - const openFiles = state.openFiles - .concat(openFile ? null : file) - .filter(f => f) - .reduce((acc, f) => { - if (f.path === file.path) { - return acc.concat({ - ...f, - active: true, - pending: true, - key: `pending-${f.key}`, - }); - } + if (f.path === file.path) { + return acc.concat({ + ...f, + active: true, + pending: true, + opened: true, + key: `${keyPrefix}-${f.key}`, + }); + } - return acc.concat(f); - }, []); - - Object.assign(state, { - entries: Object.assign(state.entries, { - [file.path]: Object.assign(state.entries[file.path], { - opened: false, - }), - }), - openFiles, - }); + return acc.concat(f); + }, []); } + + Object.assign(state, { openFiles }); }, [types.REMOVE_PENDING_TAB](state, file) { Object.assign(state, { |