From 4f348dcd199d6cf71b5c8b987f54cb11e48a2eae Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 10 Jun 2013 18:32:11 -0700 Subject: [PATCH] Watch archive file and react to events The view now refreshes on 'contents-changed' events and closes on 'removed' events --- .../lib/archive-edit-session.coffee | 7 ++++- .../archive-view/lib/archive-view.coffee | 20 ++++++++++---- .../spec/archive-view-spec.coffee | 27 +++++++++++++++++++ 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/packages/archive-view/lib/archive-edit-session.coffee b/src/packages/archive-view/lib/archive-edit-session.coffee index 3f5cd83fc..5a3580124 100644 --- a/src/packages/archive-view/lib/archive-edit-session.coffee +++ b/src/packages/archive-view/lib/archive-edit-session.coffee @@ -2,6 +2,7 @@ fsUtils = require 'fs-utils' path = require 'path' _ = require 'underscore' archive = require 'ls-archive' +File = require 'file' module.exports= class ArchiveEditSession @@ -13,12 +14,16 @@ class ArchiveEditSession new ArchiveEditSession(filePath) if archive.isPathSupported(filePath) @deserialize: ({path}={}) -> - if fsUtils.exists(path) + if fsUtils.isFile(path) new ArchiveEditSession(path) else console.warn "Could not build edit session for path '#{path}' because that file no longer exists" constructor: (@path) -> + @file = new File(@path) + + destroy: -> + @file?.off() serialize: -> deserializer: 'ArchiveEditSession' diff --git a/src/packages/archive-view/lib/archive-view.coffee b/src/packages/archive-view/lib/archive-view.coffee index 036677656..c0a5fa460 100644 --- a/src/packages/archive-view/lib/archive-view.coffee +++ b/src/packages/archive-view/lib/archive-view.coffee @@ -24,15 +24,18 @@ class ArchiveView extends ScrollView false setPath: (path) -> - return unless path? - return if @path is path + if path and @path isnt path + @path = path + @refresh() - @path = path + refresh: -> @summary.hide() @tree.hide() @loadingMessage.show() + + originalPath = @path archive.list @path, tree: true, (error, entries) => - return unless path is @path + return unless originalPath is @path if error? console.error("Error listing archive file: #{@path}", error.stack ? error) @@ -69,4 +72,11 @@ class ArchiveView extends ScrollView @focusSelectedFile() setModel: (editSession) -> - @setPath(editSession?.getPath()) + @unsubscribe(@editSession) if @editSession + if editSession + @editSession = editSession + @setPath(editSession.getPath()) + editSession.file.on 'contents-changed', => + @refresh() + editSession.file.on 'removed', => + @parent('.item-views').parent('.pane').view()?.destroyItem(editSession) diff --git a/src/packages/archive-view/spec/archive-view-spec.coffee b/src/packages/archive-view/spec/archive-view-spec.coffee index bb0199d1e..30a85c3ba 100644 --- a/src/packages/archive-view/spec/archive-view-spec.coffee +++ b/src/packages/archive-view/spec/archive-view-spec.coffee @@ -1,4 +1,5 @@ RootView = require 'root-view' +fsUtils = require 'fs-utils' describe "Archive viewer", -> beforeEach -> @@ -88,3 +89,29 @@ describe "Archive viewer", -> runs -> expect(rootView.getActiveView().getText()).toBe '' expect(rootView.getActivePaneItem().getTitle()).toBe 'f1.txt' + + describe "when the file is removed", -> + it "destroys the view", -> + rootView.open('nested.tar') + + archiveView = rootView.find('.archive-view') + + waitsFor -> archiveView.find('.entry').length > 0 + + runs -> + expect(rootView.getActivePane().getItems().length).toBe 1 + rootView.getActivePaneItem().file.trigger('removed') + expect(rootView.getActivePane()).toBeFalsy() + + describe "when the file is modified", -> + it "refreshes the view", -> + rootView.open('nested.tar') + + archiveView = rootView.find('.archive-view').view() + + waitsFor -> archiveView.find('.entry').length > 0 + + runs -> + spyOn(archiveView, 'refresh') + rootView.getActivePaneItem().file.trigger('contents-changed') + expect(archiveView.refresh).toHaveBeenCalled()