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

Fix event handling in web-bluetooth api #62

Merged
merged 1 commit into from Jun 18, 2017
Merged

Conversation

urish
Copy link
Contributor

@urish urish commented Jun 17, 2017

Currently, all events are stored in a global objects called events, indexed by the target Object. This causes a bug where all event listeners are shared between all objects. The reason is that JavaScript only support string indices for objects, and therefore, events[this] actually becomes events[this.toString()], and the toString() methods of all objects return '[object Object]'.

So with the current implementation, all the event listeners are always stored inside events['[object Object]'], regardless of the actual object. The proposed fix simply stores the event listeners inside the object itself, in a property called __events. Another option would be to use Symbol or WeakMap, if we target ES6 only.

Currently, all events are stored in a global objects called `events`, indexed by the target Object. This causes a bug where all event listeners are shared between all objects. The reason is that JavaScript only support string indices for objects, and therefore, `events[this]` actually becomes `events[this.toString()]`, and the `toString()` methods of all objects return '[object Object]'. 

So with the current implementation, all the event listeners are always stored inside `events['[object Object]']`, regardless of the actual object. The proposed fix simply stores the event listeners inside the object itself, in a property called `__events`. Another option would be to use Symbol or WeakMap, if we target ES6 only.
@thegecko
Copy link
Owner

Thanks @urish, I overlooked keying using objects wasn't unique. I was trying to keep the events internal, hence the variable, but I'm sure users know not to rely on properties prefixed with __.

I believe this also fixes #59

@thegecko thegecko merged commit 7f36c25 into thegecko:master Jun 18, 2017
@urish
Copy link
Contributor Author

urish commented Jun 18, 2017

thanks for merging! Any chance for a release?

@thegecko
Copy link
Owner

Way ahead of you ;)

urish added a commit to urish/muse-lsl that referenced this pull request Jun 18, 2017
@urish
Copy link
Contributor Author

urish commented Jun 18, 2017

😍

urish/muse-lsl@d383b88

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.

None yet

2 participants