16

I am trying to do the obvious thing with WeakMaps: I want to create a weak reference. In particular, I want to have a list of event-listeners without that list influencing the life of the listener.

So I was very excited to find WeakMaps, until I saw they were only built to satisfy one (fairly rare) use-case, extending objects that were otherwise sealed. I can't think when I ever wanted to do that, but I need lists of listeners all the time.

Is this possible to use WeakMaps in some clever way I haven't thought of to do this?

Bergi
  • 513,640
  • 108
  • 821
  • 1,164
Malvolio
  • 38,966
  • 24
  • 87
  • 125
  • I'm not sure there's a way to store a list of event listeners that is iterable while keeping it weak (WeakMap/WeakSet don't allow iteration), but is there something in particular that you're doing with this list? – Qantas 94 Heavy Apr 19 '15 at 13:00
  • Exactly what I said: I'm building a list of listeners to an event, without making those listeners eternal. – Malvolio Apr 19 '15 at 21:10
  • 1
    Just curious: What kind of architecture do you envision? Who is holding the references to the event listeners, is keeping them alive? This wouldn't work in the current designs where event listeners execute side effects. – Bergi Apr 19 '15 at 21:42
  • 1
    What actual problem are you trying to solve? As WeakMaps don't work (see my answer), I might be able to suggest some alternative solutions. – Bergi Apr 19 '15 at 21:44
  • i don't see the problem with keeping a function around in a list, it's not like functions use up many resources, and you might need it again later... – dandavis Apr 24 '15 at 05:39
  • 2
    @dandavis -- functions can use up lots of resources if there are any closure references contained within. – Michael Hays Apr 25 '15 at 00:41
  • @MichaelHays -- and if there aren't any closures, what would the function actually _do_? – Malvolio Apr 25 '15 at 03:52
  • 1
    Operate on its parameters. Return a result. Control external resources. The same sorts of things functions do in languages that don't have closures, I'd imagine. ;-) @dandavis has a good point -- if your handlers do not have closures (perhaps they look things up on a table before taking action), then the overhead of keeping around a list of functions is not expensive. This is, of course, what you should do in a language that doesn't support weak references. – Michael Hays Apr 25 '15 at 04:36
  • @MichaelHays -- Operating on its parameters and returning a result isn't particularly interesting in an event handler. The only useful external resource is the DOM, so I have been working on ways for the _actual_ function to be attached to a DOM element that has the same lifespan the function does, and then have the event-handler look the function up that way, but it's a pain in the neck. A true weak-reference would be much preferable. – Malvolio Apr 25 '15 at 08:03
  • 1
    I'm not here to argue. My comment was to dandavis -- specifically that functions can and do use up a lot of resources. You seem to be agreeing with me. So I'm not sure what the point is that you are trying to make to me. Maybe you meant to address your original comment to dandavis? – Michael Hays Apr 25 '15 at 17:27
  • usually everything i need in an event handler is passed to the event object, minus other library function that stick around anyway. if you close up a bunch of other stuff, it's little wonder you're running into issues. – dandavis Apr 25 '15 at 18:10

1 Answers1

15

No, it is impossible to use WeakMaps to create a weak reference. WeakMaps are not iterable, to use them you always need the key. This was a deliberate decision (also here), so that garbage collection does not influence the semantics of your program - which is exactly what you want.

Real weak references might come with ES8, see here and there for drafts.

Community
  • 1
  • 1
Bergi
  • 513,640
  • 108
  • 821
  • 1,164