-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Save event handler on node or instance itself #807
Comments
I'm not so clear as to why what you're proposing (which I think is quite sensible btw), is going to require a modification to the native API, can you explain some more? |
In case someone wants to bind events to html nodes like this: var div = document.getElementById('SomeDiv')
SVG.on(div, 'eventname', fn) it would save the handlers on the html node: div.events['eventname'] = fn And in my opinion any property change on a native object which is not covered by the spec is a modification of the native api. This is no modification: |
appendChild() won't remove events from element so front() method is safe (unless I'm missing something in your code..) You don't have to remove events on remove() if off() is working properly (but I would expect it from methods name), and if lib is used properly. I don't know if it's possible but can you just attach native event handlers to elements? Regarding this:
It will make a problem if:
...but could be solved with array of handlers... |
Actually we use native event handlers. However, to make And ofc, my code was just pseudocode. It would be more like this: div.events['eventname']['namespace'] = [fn1, fn2] Just like at the moment but only directly on the element |
Is there anything wrong with binding the new event to the dataset of the element in question? This is covered in the spec and is where you probably should put things like this, maybe an events array to the dataset and just run all of the events on click. |
That way when the element gets removed from the dom, its dataset will be flushed too. |
I thought about that, too. But the stuff we save on the element are listeners (functions) and you can only save strings in the dataset |
The pleasure of maintaining a separate bookkeeping. :) There are two solutions I can think of right now. 1. Store on element instance 2. Destroy With the current API, there is no way for us to know if a user is removing an element to insert it back later or to actually get rid of it. That's why we can't unbind events on |
Regarding your first point: When we bind an event to an HTML node (with So either we create an instance property anyway even if there is none or we use a new one. Wouldnt make much of a difference. However I dont like reusing the same property if it does not mean the same. I am not a fan of your second solution. That are 2 methods which basically do the same thing. They remove an element from the dom. There is no difference from the user perspective. Js user do not tend to think about mempory management so they might not care which one they use. Furthermore it does not solute the problen when you bind events to native elements again |
True, when HTML elements are thrown into the mix, we are screwed. In that case, neither is a viable solution. What about, instead of polluting the native API even more, create an object (our namespace) to store things related to SVG.js? So: node.instance Becomes: node.svgjs.instance Then we can store the events in there: node.svgjs.events We need to add at least one property on the original |
I am quiet for so long because I just dont know :D. We might keep the advantages when we use |
Surely it adds more hassle and it looks a bit less clean, I'm also on the fence about this one :D. In your last example, would you create an object called |
Yes ofc we could do this but I wanted to simplify access to the events so thats always the same path you need to take instead of |
Problem
Issue #805 revealed a memory leak on our library. It can be resolved easily but it made me thinking.
When someone binds an event to an element, the element gets saved in an array so we have a connection from element to handler. However when the element is removed from the dom, the array is not cleaned up.
@kuzmanovicnenad mentioned that it would be good to remove all event handlers when we remove the element from the dom. However, this approach has the problem that dom nodes can be reattached to the dom. A simple
front()
call deletes the node and reattaches it at the end of the node list.So this solution is not so great.
Solution
Thats why I propose the solution, that the event handlers are simply not saved in an array anymore but on the element itself. We could have a property
_events
on any instance where we can save this sort of stuff. But this holds a problem, too. We allow binding of events on normal HTML nodes as well so we would need to save the handlers on the node which means: Altering the native API - baah.But I dont see a better solution to prevent memory leaks. Because: When all references to a node are dropped and the node is not in the dom anymore, everything saved on the node is cleaned automatically. One thing less to think about.
@saivan @wout @kuzmanovicnenad any thoughts on this?
The text was updated successfully, but these errors were encountered: