diff --git a/lib/png.js b/lib/png.js index c00518d..8978867 100644 --- a/lib/png.js +++ b/lib/png.js @@ -19,6 +19,10 @@ export class PNG extends Stream { this.depth = undefined; this._grayscaleData = undefined; this._propData = undefined; + this._configPostParse = { + initGrayscaleData: options.initGrayscaleData, + initPropData: options.initPropData, + }; this.data = this.width > 0 && this.height > 0 ? new Buffer(4 * this.width * this.height) : null; @@ -38,16 +42,8 @@ export class PNG extends Stream { this._parser.on('gamma', this._gamma.bind(this)); this._parser.on('parsed', function(data) { this.data = data; - this._grayscaleData = undefined; - this._propData = undefined; - if (options.initGrayscaleData) { - this.initGrayscaleData(); - } - if (options.initPropData) { - const shrinkMin = options.initPropData.shrinkMin || false; - const shrinkMax = options.initPropData.shrinkMax || false; - this.initGrayscaleData(shrinkMin, shrinkMax); - } + + this._postParsed(); this.emit('parsed', data); }.bind(this)); @@ -56,6 +52,22 @@ export class PNG extends Stream { this._packer.on('end', this.emit.bind(this, 'end')); this._parser.on('close', this._handleClose.bind(this)); this._packer.on('error', this.emit.bind(this, 'error')); + + this._postParsed = this._postParsed.bind(this); + } + + _postParsed() { + this._grayscaleData = undefined; + this._propData = undefined; + const { initGrayscaleData, initPropData } = this._configPostParse; + if (initGrayscaleData) { + this.initGrayscaleData(); + } + if (initPropData) { + const shrinkMin = initPropData.shrinkMin || false; + const shrinkMax = initPropData.shrinkMax || false; + this.initPropData(shrinkMin, shrinkMax); + } } pack() { @@ -73,14 +85,11 @@ export class PNG extends Stream { parse(data, callback) { if (callback) { - var onParsed, onError; + let onParsed, onError; - onParsed = function(parsedData) { + onParsed = function() { this.removeListener('error', onError); - this.data = parsedData; - this._grayscaleData = undefined; - this._propData = undefined; callback(null, this); }.bind(this);