Skip to content

Commit

Permalink
Fixed double parsed data setting bug
Browse files Browse the repository at this point in the history
  • Loading branch information
gforge committed Dec 26, 2017
1 parent 2eb338e commit a2e8db4
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions lib/png.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));

Expand All @@ -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() {
Expand All @@ -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);

Expand Down

0 comments on commit a2e8db4

Please sign in to comment.