medikoo / event-emitter

Environment agnostic event emitter solution for JavaScript
MIT License
241 stars 23 forks source link

Use WeakMap to save the listeners #3

Open DavidBruant opened 10 years ago

DavidBruant commented 10 years ago

__ee__ only goes so far. This property being set prevents me from freezing my object before it's set.

medikoo commented 10 years ago

I thought about it, but I'm afraid that using WeakMap may affect performance, and in case of that utility performance is crucial.

Still I'll double check that, and leave it open until we have full picture

filipedeschamps commented 9 years ago

Having the same problem: https://github.com/medikoo/event-emitter/issues/5

medikoo commented 9 years ago

It will work as advertised only with native WeakMap, as polyfill again will need to define property on a key object.

Other risk implied with that solution is that two different listener maps may be created for same object if for some reason someone has two versions of event-emitter in his setup (imagine two packages, one requiring v0.2.1 and other v0.2.2 of event-emitter). Unfortunately it's not that unlikely to happen, and I've already approached similar issue with other package I maintain.

Solution I see:

Switch to WeakMap, at least for ES6 environments it'll assure that we can handle events on frozen objects. However keep listeners map accessible on a global object (via some obscure variable), so in case of two different event emitters initialized in a process, same map is used. Unfortunately it'll imply other problem, as we need to assign something to global object, we need to retrieve it, and the only reliable way to do it, is via new Function("return this")(), and this breaks CSP. Are we ok with that?

filipedeschamps commented 9 years ago

No knowledge about WeakMap, but I will do my researches. Thank you :+1: