diff options
author | Fatih Acet <acetfatih@gmail.com> | 2017-09-25 21:58:46 +0300 |
---|---|---|
committer | Fatih Acet <acetfatih@gmail.com> | 2017-09-27 23:19:02 +0300 |
commit | c25e83b536a268455599252bf20c34d8564b0419 (patch) | |
tree | 2ce3edfd5025a997a85fd82083f1be447de4e98e | |
parent | 8266c78cd0f7a868bc0329ac61d24af797a19644 (diff) | |
download | gitlab-ce-c25e83b536a268455599252bf20c34d8564b0419.tar.gz |
RepoEditor: Prevent extra network call for already opened files.acet-repo-editor-prevent-extra-request
-rw-r--r-- | app/assets/javascripts/repo/components/repo_sidebar.vue | 21 | ||||
-rw-r--r-- | app/assets/javascripts/repo/helpers/repo_helper.js | 4 | ||||
-rw-r--r-- | spec/javascripts/repo/components/repo_sidebar_spec.js | 14 |
3 files changed, 32 insertions, 7 deletions
diff --git a/app/assets/javascripts/repo/components/repo_sidebar.vue b/app/assets/javascripts/repo/components/repo_sidebar.vue index 3414128526d..dc1bda95a01 100644 --- a/app/assets/javascripts/repo/components/repo_sidebar.vue +++ b/app/assets/javascripts/repo/components/repo_sidebar.vue @@ -37,17 +37,24 @@ export default { let file = clickedFile; if (file.loading) return; file.loading = true; + if (file.type === 'tree' && file.opened) { file = Store.removeChildFilesOfTree(file); file.loading = false; } else { - Service.url = file.url; - Helper.getContent(file) - .then(() => { - file.loading = false; - Helper.scrollTabsRight(); - }) - .catch(Helper.loadingError); + const openFile = Helper.getFileFromPath(file.url); + if (openFile) { + file.loading = false; + Store.setActiveFiles(openFile); + } else { + Service.url = file.url; + Helper.getContent(file) + .then(() => { + file.loading = false; + Helper.scrollTabsRight(); + }) + .catch(Helper.loadingError); + } } }, diff --git a/app/assets/javascripts/repo/helpers/repo_helper.js b/app/assets/javascripts/repo/helpers/repo_helper.js index 655e4e7605b..2c960a147c2 100644 --- a/app/assets/javascripts/repo/helpers/repo_helper.js +++ b/app/assets/javascripts/repo/helpers/repo_helper.js @@ -263,6 +263,10 @@ const RepoHelper = { return Store.openedFiles.find(openedFile => Store.activeFile.url === openedFile.url); }, + getFileFromPath(path) { + return Store.openedFiles.find(file => file.url === path); + }, + loadingError() { Flash('Unable to load this content at this time.'); }, diff --git a/spec/javascripts/repo/components/repo_sidebar_spec.js b/spec/javascripts/repo/components/repo_sidebar_spec.js index abcff8e537e..db9911c7a2c 100644 --- a/spec/javascripts/repo/components/repo_sidebar_spec.js +++ b/spec/javascripts/repo/components/repo_sidebar_spec.js @@ -79,6 +79,20 @@ describe('RepoSidebar', () => { expect(Helper.getContent).toHaveBeenCalledWith(file1); }); + it('should not fetch data for already opened files', () => { + const file = { + id: 42, + url: 'foo', + }; + + spyOn(Helper, 'getFileFromPath').and.returnValue(file); + spyOn(RepoStore, 'setActiveFiles'); + const vm = createComponent(); + vm.fileClicked(file); + + expect(RepoStore.setActiveFiles).toHaveBeenCalledWith(file); + }); + it('should hide files in directory if already open', () => { spyOn(RepoStore, 'removeChildFilesOfTree').and.callThrough(); const file1 = { |