Watch archive file and react to events

The view now refreshes on 'contents-changed' events and
closes on 'removed' events
This commit is contained in:
Kevin Sawicki
2013-06-10 18:32:11 -07:00
parent a3d896f456
commit 4f348dcd19
3 changed files with 48 additions and 6 deletions

View File

@@ -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'

View File

@@ -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)

View File

@@ -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()