Allow to set src=null to clear data - #617
Conversation
This still invokes onerror callback if binded, which is usual browser behaviour.
Error is specific for this case, using `CAIRO_STATUS_INVALID_CONTENT`.
Not sure if error should be `CAIRO_STATUS_NULL_POINTER` instead.
From Chrome's Developer Tools:
```js
> var img = document.createElement('img')
< undefined
> img.onerror = function handler(evt) { console.log('img onload/onerror handler: %s', evt.type); }
< handler(evt)
> img.src = null
< null
< img onload/onerror handler: error
```
|
Hmm, I don't think that this is quite correct behaviour. Chrome actually tries to load the same image as if you had passed in the string Safari on the other hand doesn't do anything when setting the I'm not sure where the spec for This is what I used for testing: a = new Image()
a.onload = console.log.bind(console, 'onload')
a.onerror = console.log.bind(console, 'onerror')
a.src = null |
|
Seems like there is a specification but it doesn't really say anything about it... 😭 http://stackoverflow.com/questions/15233483/is-there-a-specification-for-javascript-image-object Safari actually sets it to an empty string. At least it seems like all browsers coerce the value into a string which seems sensible. To be honest I would actually prefer a |
Conflicts: test/image.test.js
|
I just updated the branch with latest master to remove conflicts. I didn't realise about Chrome trying to load I'm OK with your proposal, let me push something for that. |
|
So I just ran into the issue of Images taking up tons of memory. It seems this PR will still cause an error to be thrown when doing Currently I ignore the error but setting the |
|
Hmm, can't you drop any references to the actual image and then V8 should garbage collect for you? |
That's what I would think. I have images being created and written to disk within a fairly large loop. My initial code involved setting Maybe v8 would normally garbage collect after the loop is done? Is there something that needs to be added in the source code to free memory which doing |
|
@chearon I actually did see that issue but ended up here as a means to find the recommended way of freeing memory. Looking at the other ticket, I think setting the callback handlers to My code looks something like this: function getImage(buffer) {
return new Promise(function(resolve, reject) {
const img = new Canvas.Image();
img.onload = function() {
img.onload = null;
img.onerror = null;
resolve(img);
};
img.onerror = function(err) {
img.onload = null;
img.onerror = null;
reject(err);
};
img.src = buffer;
});
}Seems to work pretty well although it still feels like this isn't something one should have to worry about. |
64ed3d8 to
ff0f2ab
Compare
This still invokes onerror callback if binded, which is usual browser behaviour.
Error is specific for this case, using
CAIRO_STATUS_INVALID_CONTENT.Not sure if error should be
CAIRO_STATUS_NULL_POINTERinstead.From Chrome's Developer Tools: