Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dispose support #487

Merged
merged 1 commit into from
Aug 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -228,15 +229,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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this check was useless because child_process.connected is always false without forked process or not using ipc.
( please look arround here https://github.com/nodejs/node/blob/master/lib/internal/child_process.js#L337)
So, I change this line to check proc.exitCode at https://github.com/aheckmann/gm/pull/487/files#diff-b26080cc6f2472dce354dbedffc64d57R329

}

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');
}
}