Simplify 'this' in IO pipeline using arrow functions

This commit is contained in:
Lovell Fuller
2019-07-26 19:19:21 +01:00
parent 233b015d77
commit 6cf0b3240d
4 changed files with 67 additions and 65 deletions

View File

@@ -650,6 +650,7 @@ function _setBooleanOption (key, val) {
* @private
*/
function _read () {
/* istanbul ignore else */
if (!this.options.streamOut) {
this.options.streamOut = true;
this._pipeline();
@@ -662,14 +663,13 @@ function _read () {
* @private
*/
function _pipeline (callback) {
const that = this;
if (typeof callback === 'function') {
// output=file/buffer
if (this._isStreamInput()) {
// output=file/buffer, input=stream
this.on('finish', function () {
that._flattenBufferIn();
sharp.pipeline(that.options, callback);
this.on('finish', () => {
this._flattenBufferIn();
sharp.pipeline(this.options, callback);
});
} else {
// output=file/buffer, input=file/buffer
@@ -680,41 +680,31 @@ function _pipeline (callback) {
// output=stream
if (this._isStreamInput()) {
// output=stream, input=stream
if (this.streamInFinished) {
this.once('finish', () => {
this._flattenBufferIn();
sharp.pipeline(this.options, function (err, data, info) {
sharp.pipeline(this.options, (err, data, info) => {
if (err) {
that.emit('error', err);
this.emit('error', err);
} else {
that.emit('info', info);
that.push(data);
this.emit('info', info);
this.push(data);
}
that.push(null);
});
} else {
this.on('finish', function () {
that._flattenBufferIn();
sharp.pipeline(that.options, function (err, data, info) {
if (err) {
that.emit('error', err);
} else {
that.emit('info', info);
that.push(data);
}
that.push(null);
});
this.push(null);
});
});
if (this.streamInFinished) {
this.emit('finish');
}
} else {
// output=stream, input=file/buffer
sharp.pipeline(this.options, function (err, data, info) {
sharp.pipeline(this.options, (err, data, info) => {
if (err) {
that.emit('error', err);
this.emit('error', err);
} else {
that.emit('info', info);
that.push(data);
this.emit('info', info);
this.push(data);
}
that.push(null);
this.push(null);
});
}
return this;
@@ -722,15 +712,15 @@ function _pipeline (callback) {
// output=promise
if (this._isStreamInput()) {
// output=promise, input=stream
return new Promise(function (resolve, reject) {
that.on('finish', function () {
that._flattenBufferIn();
sharp.pipeline(that.options, function (err, data, info) {
return new Promise((resolve, reject) => {
this.once('finish', () => {
this._flattenBufferIn();
sharp.pipeline(this.options, (err, data, info) => {
if (err) {
reject(err);
} else {
if (that.options.resolveWithObject) {
resolve({ data: data, info: info });
if (this.options.resolveWithObject) {
resolve({ data, info });
} else {
resolve(data);
}
@@ -740,12 +730,12 @@ function _pipeline (callback) {
});
} else {
// output=promise, input=file/buffer
return new Promise(function (resolve, reject) {
sharp.pipeline(that.options, function (err, data, info) {
return new Promise((resolve, reject) => {
sharp.pipeline(this.options, (err, data, info) => {
if (err) {
reject(err);
} else {
if (that.options.resolveWithObject) {
if (this.options.resolveWithObject) {
resolve({ data: data, info: info });
} else {
resolve(data);