State component: entities with "bind-for" and "bind-item" attributes are not unsubscribing from state on removal
NVFedorov opened this issue · 8 comments
This is duplicate of my question on stackoverflow
I'm not sure if it's by design, but I don't understand how to properly remove entities with "bind-for" and "bind-item" attributes. They simply don't get unsubscribed from state and keep getting called on state update. For me clearing out the bound array is not an option, because I'm recreating my scene from scratch on some event and everything should be removed from it including templates. As a workaround I manually clear the state, but it's really bad solution.
Is this a bug or it's by design?
Thanks for the issue, ideally, removing them removes the subscription. If that doesn't happen, it's a bug. Here's where is it currently trying: https://github.com/supermedium/superframe/blob/master/components/state/src/index.js#L168
@ngokevin thanks for your reply. Yeah, this method also raises some questions: you see, I created a custom html element to display one of the values in state. All works well until I remove all the entities and create them again. I'm not sure about aframe components life cycle, but apparently they're not immediately removed (the remove and as consequence unsubscribe method is called some time later down the road). As a result my newly subscribed node gets unsubscribed.
Long story short : if a component is not found in subscribers array the method you pointed out will remove the last element from the array.
So, according to you the issue with bind-for and bind-item is a bug. Because the components are not getting unsubscribed. Do I need to add some tag to this issue or?
Thanks
Perhaps you can try calling the unsubscribe manually if there's some timing or race condition issues?
AFRAME.systems.state.unsubscribe(el.components.bind)
Thanks for this suggestion, I tried and it works. But anyway I'd like to know if there is a chance this issue will be fixed?
So the problem is removing an entity does not unsubscribe? Or it just takes a handful of milliseconds to unsubscribe? If it's the latter, I'd need to think since DOM removal may be a tiny bit asynchronous.
I think if you are wiping an entire scene, you may want to clear out the subscriber array yourself. Generally, I don't wipe out entire scenes and create a lot of entities from scratch, but I just return them to a pool or hide them, or do a page navigation.
Sorry, I'm kind of mixing two issues:
- There is no unsubscribe call for the bind-for and bind-item. They are not unsubscribing after removal whosoever.
- The unsubscribe method removes the last element from
subscribers
if no Component found (subscriptions.indexOf(component)
returns -1
https://github.com/supermedium/superframe/blob/master/components/state/src/index.js#L168
Oh, thanks! You can open a pull request since you found those issues, it's a matter of adding the remove callback to those components, and checking if it exists from subscribers before removing. Else I can fix it later.
okay, I'll try to fix it