-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
RangeError when passing an observable HTMLElement to toJS #566
Comments
Working on a fix for this, but having a hard time writing tests, since there is no DOM in the test runner. |
@maxdeviant, what do you mean by “observable HTMLElement”? Please, don't say that you are doing something like
You can run it in chrome like this: |
Strange, that this error occurs, it have to be prevented by __alreadySeen check. |
@andykog Well, my original case was something like this: class MyStore {
@observable
public images = asMap<HTMLImageElement[]>({
123: new Image()
});
}
// somewhere else in my code
console.log(toJS(myStoreInstance.images));
// throws the RangeError What I am really interested in is when entries are added to/removed from the map, not necessarily observing the element itself. |
I noticed that when I tried a simple It appears that there are ways to check for an HTML element without |
Well instanceof check can be made safely like this:
|
Ah, good point. I had it done like this: function isHTMLElement(source) {
try {
return source instanceof HTMLElement;
} catch (err) {
return (
source !== null &&
typeof source === "object" &&
source.nodeType === 1 &&
typeof source.style === "object" &&
typeof source.ownerDocument === "object"
);
}
} Also, is there a way to add a test that only runs when executed in a browser? It doesn't appear that |
My approach is a bit more performant :-)
Nope, but you can detect DOM the same way:
|
I'm not entirely sure about this PR;
|
@mweststrate, this bothers me in current toJS behaviour as well, but to change it we have to at least somehow mark classes with properties decorated with |
@andykog they return |
@mweststrate You are correct in that my PR is not really generic, since it grew out of an issue that we were encountering in our codebase. So if there is a better solution that can be achieved that fixes the issue, I am all for that. |
Closing this issue as it has been solved in 2.7.0. Feel free to re-open if this isn't the case. |
When you pass an observable
HTMLElement
totoJS
, a RangeError occurs:The text was updated successfully, but these errors were encountered: