Added progress stats to reading files

This commit is contained in:
probablycorey
2013-10-07 13:47:20 -07:00
parent 13232de4b9
commit f5c9dc7362
2 changed files with 28 additions and 1 deletions

View File

@@ -193,6 +193,18 @@ describe "Project", ->
project.openAsync("bar://baz").then (item) ->
expect(item).toEqual { bar: "bar://baz" }
it "returns number of read bytes as progress indicator", ->
filePath = project.resolve 'a'
totalBytes = 0
promise = project.openAsync(filePath)
promise.progress (bytesRead) -> totalBytes = bytesRead
waitsForPromise ->
promise
runs ->
expect(totalBytes).toBe fs.statSync(filePath).size
describe ".bufferForPath(path)", ->
describe "when opening a previously opened path", ->
it "does not create a new buffer", ->

View File

@@ -62,7 +62,22 @@ class File
if not @exists()
promise = Q(null)
else if not @cachedContents? or flushCache
promise = Q.nfcall fsUtils.readFile, @getPath(), 'utf8'
deferred = Q.defer()
promise = deferred.promise
content = []
size = 0
readStream = fsUtils.createReadStream @getPath(), encoding: 'utf8'
readStream.on 'data', (chunk) ->
content.push(chunk)
size += chunk.length
deferred.notify(size)
readStream.on 'end', ->
deferred.resolve(content.join())
readStream.on 'error', (error) ->
deferred.reject(error ? "REPLACE THIS ERROR MESSAGE, fs.readStream doesn't output an error message!")
else
promise = Q(@cachedContents)