Browse Source

Merge pull request #706 from LinusU/allow-to-unbind-callback-handlers

Allow to unbind onload/onerror callback handlers
v1.x
Linus Unnebäck 9 years ago
parent
commit
e1033557af
  1. 12
      src/Image.cc
  2. 52
      test/image.test.js

12
src/Image.cc

@ -268,6 +268,12 @@ NAN_SETTER(Image::SetOnload) {
if (value->IsFunction()) {
Image *img = Nan::ObjectWrap::Unwrap<Image>(info.This());
img->onload = new Nan::Callback(value.As<Function>());
} else if (value->IsNull()) {
Image *img = Nan::ObjectWrap::Unwrap<Image>(info.This());
if (img->onload) {
delete img->onload;
}
img->onload = NULL;
}
}
@ -292,6 +298,12 @@ NAN_SETTER(Image::SetOnerror) {
if (value->IsFunction()) {
Image *img = Nan::ObjectWrap::Unwrap<Image>(info.This());
img->onerror = new Nan::Callback(value.As<Function>());
} else if (value->IsNull()) {
Image *img = Nan::ObjectWrap::Unwrap<Image>(info.This());
if (img->onerror) {
delete img->onerror;
}
img->onerror = NULL;
}
}

52
test/image.test.js

@ -150,4 +150,56 @@ describe('Image', function () {
image.src = new Buffer(0);
image.src = new Buffer('');
});
it('should unbind Image#onload', function() {
var img = new Image
, onloadCalled = 0;
img.onload = function() {
onloadCalled += 1;
};
img.src = png_checkers;
assert.equal(img.src, png_checkers);
assert.strictEqual(true, img.complete);
assert.strictEqual(2, img.width);
assert.strictEqual(2, img.height);
assert.equal(onloadCalled, 1);
onloadCalled = 0;
img.onload = null;
img.src = png_clock;
assert.equal(img.src, png_clock);
assert.strictEqual(true, img.complete);
assert.strictEqual(320, img.width);
assert.strictEqual(320, img.height);
assert.equal(onloadCalled, 0);
});
it('should unbind Image#onerror', function() {
var img = new Image
, onerrorCalled = 0;
img.onload = function() {
assert.fail('called onload');
};
img.onerror = function() {
onerrorCalled += 1;
};
img.src = png_clock + 's1';
assert.equal(img.src, png_clock + 's1');
assert.equal(onerrorCalled, 1);
onerrorCalled = 0;
img.onerror = null;
img.src = png_clock + 's3';
assert.equal(img.src, png_clock + 's3');
assert.equal(onerrorCalled, 0);
});
});

Loading…
Cancel
Save