From f5c9dc736210d770a7c5bb7fd45ca035464abdd2 Mon Sep 17 00:00:00 2001 From: probablycorey Date: Mon, 7 Oct 2013 13:47:20 -0700 Subject: [PATCH] Added progress stats to reading files --- spec/project-spec.coffee | 12 ++++++++++++ src/file.coffee | 17 ++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/spec/project-spec.coffee b/spec/project-spec.coffee index 81dad80e2..36dd6dbe0 100644 --- a/spec/project-spec.coffee +++ b/spec/project-spec.coffee @@ -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", -> diff --git a/src/file.coffee b/src/file.coffee index 67ae240c9..5668b655f 100644 --- a/src/file.coffee +++ b/src/file.coffee @@ -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)