In Ember 3.1 I’m getting hit with the new ESlint rule about “Avoiding leaking state in ember objects”.
I understand I can set properties in init(), but doing that drastically hampers the ergonomics of these sorts of APIs – so much so that I noticed Ember’s own properties that use this pattern are ignored by the rule. (And for good reason! I’d hate to define actions in an init method…)
My question is, is there a path forward here for these types of APIs, which have been laid out by Ember itself and I’m sure are used heavily by other Ember developers? I believe static class fields might be the answer? In the mean time, what are some things other folks are doing to work around this rule? And if everyone is ignoring it, should it be enabled by default?
But yeah, I think you’re right that the long-term solution is to switch to ES6 classes and class fields. Since it’s basically syntactic sugar for setting values in the constructor, it doesn’t have the same issues with sharing state between instances.
A simple solution here would be to freeze the objects that you set this way. I find that to be an effective way to indicate to the linting rule that these properties will not be mutated at runtime (and therefore are not memory leak risks) without sacrificing the readability in the file.
^ still reads better to me than the computed(function() {}) solution, avoids the linting rule, and makes the intent pretty clear (“don’t change these values at runtime”)…
@sukima the original reason was to avoid conflicts between action names and common class hooks, such as destroy, click, etc. It also was important before closure actions were a thing, when actions had to be sent manually and you had to do things to maintain context. Now that we have closure actions, actions can be bound directly which is more similar to how other frameworks do it.
The most recent time I hit this on a component, I switched to a real ECMA class with a property initializer and was pleasantly surprised that it all just worked (once I turned on the necessary Babel plugin and updated my eslint config to use the Babel parser).