then-read-stream
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -29,12 +29,30 @@ "use strict"; | ||
StreamReader.prototype.read = function (buffer, offset, length, position) { | ||
var _this = this; | ||
if (position === void 0) { position = null; } | ||
this.request = { | ||
buffer: buffer, | ||
offset: offset, | ||
length: length, | ||
position: position, | ||
deferred: new Deferred() | ||
}; | ||
this.tryRead(); | ||
return this.request.deferred.promise; | ||
if (this.request) | ||
throw new Error("Concurrent read operation"); | ||
var readBuffer = this.s.read(length); | ||
if (readBuffer) { | ||
readBuffer.copy(buffer, offset); | ||
return es6_promise_1.Promise.resolve(length); | ||
} | ||
else { | ||
this.request = { | ||
buffer: buffer, | ||
offset: offset, | ||
length: length, | ||
position: position, | ||
deferred: new Deferred() | ||
}; | ||
this.s.once("readable", function () { | ||
_this.tryRead(); | ||
}); | ||
return this.request.deferred.promise.then(function (n) { | ||
_this.request = null; | ||
return n; | ||
})["catch"](function (err) { | ||
_this.request = null; | ||
throw err; | ||
}); | ||
} | ||
}; | ||
@@ -47,5 +65,2 @@ StreamReader.prototype.tryRead = function () { | ||
this.request.deferred.resolve(this.request.length); | ||
process.nextTick(function () { | ||
_this.request = null; | ||
}); | ||
} | ||
@@ -52,0 +67,0 @@ else { |
{ | ||
"name": "then-read-stream", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Read from a readable stream just like a file", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -54,11 +54,29 @@ import {Promise} from "es6-promise"; | ||
this.request = { | ||
buffer, | ||
offset, | ||
length, | ||
position, | ||
deferred: new Deferred<number>() | ||
}; | ||
this.tryRead(); | ||
return this.request.deferred.promise; | ||
if (this.request) | ||
throw new Error("Concurrent read operation"); | ||
const readBuffer = this.s.read(length); | ||
if (readBuffer) { | ||
readBuffer.copy(buffer, offset); | ||
return Promise.resolve<number>(length); | ||
} else { | ||
this.request = { | ||
buffer, | ||
offset, | ||
length, | ||
position, | ||
deferred: new Deferred<number>() | ||
}; | ||
this.s.once("readable", () => { | ||
this.tryRead(); | ||
}); | ||
return this.request.deferred.promise.then((n) => { | ||
this.request = null; | ||
return n; | ||
}).catch( (err) => { | ||
this.request = null; | ||
throw err; | ||
}); | ||
} | ||
} | ||
@@ -71,5 +89,2 @@ | ||
this.request.deferred.resolve(this.request.length); | ||
process.nextTick(() => { | ||
this.request = null; | ||
}); | ||
} else { | ||
@@ -76,0 +91,0 @@ this.s.once("readable", () => { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13860
314