Skip to content

Commit

Permalink
Merge pull request #487 from dlwr/dispose-support
Browse files Browse the repository at this point in the history
Add dispose support
  • Loading branch information
aheckmann authored Aug 1, 2016
2 parents 13b4226 + a797086 commit f70800b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 10 deletions.
55 changes: 45 additions & 10 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ module.exports = function (proto) {
, self = this
, proc, err
, timeout = parseInt(this._options.timeout)
, disposers = this._options.disposers
, timeoutId;

debug(cmd);
Expand All @@ -236,15 +237,18 @@ module.exports = function (proto) {

if (timeout) {
timeoutId = setTimeout(function(){
err = new Error('gm() resulted in a timeout.');
cb(err);
if (proc.connected) {
proc.stdin.pause();
proc.kill();
}
dispose('gm() resulted in a timeout.');
}, timeout);
}

if (disposers) {
disposers.forEach(function(disposer) {
disposer.events.forEach(function(event) {
disposer.emitter.on(event, dispose);
});
});
}

if (self.sourceBuffer) {
proc.stdin.write(this.sourceBuffer);
proc.stdin.end();
Expand Down Expand Up @@ -311,12 +315,22 @@ module.exports = function (proto) {
if (cb.called) return;
if (timeoutId) clearTimeout(timeoutId);
cb.called = 1;
if (args[0] !== 'identify' && bin !== 'identify') {
self._in = [];
self._out = [];
}
if (args[0] !== 'identify' && bin !== 'identify') {
self._in = [];
self._out = [];
}
callback.call(self, err, stdout, stderr, cmd);
}

function dispose (msg) {
var message = msg ? msg : 'gm() was disposed';
err = new Error(message);
cb(err);
if (proc.exitCode === null) {
proc.stdin.pause();
proc.kill();
}
}
}

/**
Expand Down Expand Up @@ -408,4 +422,25 @@ module.exports = function (proto) {

return rgx.test(this.source);
}

/**
* add disposer (like 'close' of http.IncomingMessage) in order to dispose gm() with any event
*
* @param {EventEmitter} emitter
* @param {Array} events
* @return {Object} gm
* @example
* command.addDisposer(req, ['close', 'end', 'finish']);
*/

proto.addDisposer = function addDisposer (emitter, events) {
if (!this._options.disposers) {
this._options.disposers = [];
}
this._options.disposers.push({
emitter: emitter,
events: events
});
return this;
};
}
45 changes: 45 additions & 0 deletions test/dispose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var assert = require('assert');

module.exports = function (img, dir, finish, gm) {
var EventEmitter = require('events').EventEmitter;
EventEmitter.prototype._maxListeners = 100;

assert.equal(undefined, gm.prototype._options.disposers);
assert.equal(undefined, img._options.disposers);

emitter = new EventEmitter();

disposer = {
emitter: emitter,
events: ['pleaseDispose', 'readyToDispose']
};

var g = gm('test').options({ disposers: [ disposer ] });
assert.deepEqual([disposer], g._options.disposers);

var sub = gm.subClass({ disposers: [ disposer ]});
assert.deepEqual([disposer], sub.prototype._options.disposers);

if (!gm.integration) {
return finish();
}

gm(dir + '/photo.JPG').options({ disposers: [ disposer ]})
.thumb(1000, 1000, dir + '/dispose.png', function (err) {
assert.ok(err, "Expecting a disposed error");
});

emitter.emit('pleaseDispose');

noDispose();

function noDispose() {
gm(dir + '/photo.JPG').options({ disposers: [ disposer ]})
.thumb(1000, 1000, dir + '/dispose.png', function (err) {
delete emitter;
delete disposer;
finish(err);
});
emitter.emit('disposeOK');
}
}

0 comments on commit f70800b

Please sign in to comment.