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

Make ES5 compatible by using an object instead of Map and adding a polyfill for WeakMap #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

kristerkari
Copy link

fixes #1

ping @brigand

@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 98.261% when pulling ed73765 on kristerkari:feature/es5-compatibility into e8bb27d on brigand:master.

@brigand
Copy link
Owner

brigand commented Nov 30, 2017

@kristerkari thanks for the PR! Needs some work before I can merge it.

The key/value object doesn't work well when el/element is a dom node. e.g. in this:

export const getEd = (element = document.body) => {
  if (ed[element] !== undefined) { 

If you call it with e.g. getEd() or getEd(someDiv) it's going to convert those to strings, and that could cause issues. For example, all divs would be treated as the same element. When the el prop is a string resolving to a div, it'd still share event data with all other divs.

Your reset breaks things when the component is used in multiple places, since any of the instance mounting clears all other data used by all other instances.

To solve the first issue, you'd have to implement part of Map, using an array of [key,value] tuples (arrays).

For not leaking memory, you'd have to keep counts of the number of mounted components referencing each element, and clean up the pseudo-map entry when there are no references, after rolling back the style/className changes.

Hope you continue working on this, it'd be nice to not rely on WeakMap.

@kristerkari
Copy link
Author

Yeah I was thinking about implementing a local version of Map, but thought that an object would be a simpler solution. Now I see that it was not actually that simple as I did not think about the elements being used as keys.

Doing the cleanup of references does not sound like a big thing to do.

But now that I think about it, it would probably be easier to just include the polyfills in this project as it would to be adding ~1kb minified code to the library: https://github.com/Polymer/WeakMap/blob/master/weakmap.js

What do you think @brigand?

@kristerkari
Copy link
Author

@brigand I brought WeakMap back and added a small polyfill. It's really a small amount of code, but this way the user of the library doesn't have to worry about polyfills.

I guess that we can also get rid of Map? Please let me know if this does not work.

@brigand
Copy link
Owner

brigand commented Jan 6, 2018

Thanks for getting back to this. Have you tested it in an app? If so, I'll merge it.

@kristerkari
Copy link
Author

I did not test it in an app yet, but I'll test in our app to see if it works normally in IE11 after I remove the polyfills 👍

@kristerkari kristerkari changed the title Make ES5 compatible by using an object instead of Map and WeakMap Make ES5 compatible by using an object instead of Map and adding a polyfill for WeakMap Jan 6, 2018
@kristerkari
Copy link
Author

I did some testing with the changes.

It seems that the polyfill does not for some reason get applied properly, so an error gets thrown. I was unable access dev tools on the browsers that I tested with, so I don't know yet what it throws.

I need to try to find a browser that does not support WeakMap and has working dev tools.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ES5 compatibility
3 participants