hmans / miniplex

A đŸ‘©â€đŸ’» developer-friendly entity management system for đŸ•č games and similarly demanding applications, based on 🛠 ECS architecture.
MIT License
849 stars 39 forks source link

Do we need to cancel our query if the world they're attached to is destroyed? #316

Open LukeWood opened 1 month ago

LukeWood commented 1 month ago

Hey - I'm working on https://bulletz.io which uses miniplex to do all of it's ECS style logic. First of all, great job! Thanks for all of the work on this - I really enjoy using it.

Secondly, I'm struggling a lot with memory leaks. Unfortunately, I think that a ton of values are being captured inside of closures that are then not cleaned up. In particular, I think the queries are what is causing these things to be captured.

When I create a query, I do something like:

this.world.with('isPlayer').onEntityAdded(v => {
     const enemy = game.players.get(v.enemy) // or whatever else
})

I suspect that this is storing a reference to game, which is a temporal object (as well as world), and that these subscriptions are therefore not cleaned up.

Do I need to manually clean up all subscriptions to a world when I want to destroy it? Or is there something else I can do? This feels pretty cumbersome! Do I call disconnect() to destroy a query? I can't tell if this is mean't to be a user facing method or not based on some other implementation details.

Thanks for all the hard work and any guidance!