mobxjs / mobx.dart

MobX for the Dart language. Hassle-free, reactive state-management for your Dart and Flutter apps.
https://mobx.netlify.app
MIT License
2.4k stars 310 forks source link

Provide an optional parameter for inferring @action/@computed/@observable #219

Open rrousselGit opened 5 years ago

rrousselGit commented 5 years ago

This proposal is for automatically inferring @action/@computed/@observable based on the type of the variable.

The idea is, by letting the inference do its job, silly mistakes where we forgot to decorate a member can be stopped.

The inference would work using the following rules:

This inference can be configured using two different ways:


I've looked through the entire mobx_examples folder and all of the examples follows these rules.

There are some exceptions, like in form_store.dart where setupValidations is not decorated. But setupValidations should probably be performed inside the constructor of FormStore and therefore setupValidations can be made private.

Another exception is in github_store.dart, where repositories is not @obvervable. But that seems dangerous considering repositories is mutated on multiple occasions and is used inside the build method of RepositoryListView.

eltonomicon commented 5 years ago

I like the @store attribute with optional parameters to enable inference. Comments:

Would inference be all-or-nothing or could you still use annotations to override inference rules?

pavanpodila commented 5 years ago

Great idea @rrousselGit. Overall I like the direction. Few other things to consider:

smiLLe commented 5 years ago

@pavanpodila you often see ignore: true . so for computed it would be @computed(ignore: true)

eltonomicon commented 5 years ago

Agreed with ignore: true, IMO this is preferable to doubling the number of annotations.

pavanpodila commented 5 years ago

Ya like the ignore approach.

rrousselGit commented 5 years ago

It should be @Computed() in that case.

Anyway I agree with ignore. If peoples wants a shorthand to make it easier to use, they can do the following:

const noComputed = Computed(ignore: true);
pavanpodila commented 4 years ago

@shyndman I was thinking we can pick this up as a good enhancement for simplifying the use of MobX for most users. The current @store directive can get a nitro-boost with this auto-inference feature? What do you think ?

shyndman commented 4 years ago

Yeah, I'm into it, along with the opt-out annotation.

Maybe we could generalize the opt-outs into a single annotation? Something like @ignore()?

pavanpodila commented 4 years ago

I prefer the @ignore approach On 27 Dec 2019, 1:21 AM +0530, Scott Hyndman notifications@github.com, wrote:

Yeah, I'm into it, along with the opt-out annotations. Maybe we could generalize the opt-outs into a single annotation? Something like @ignore()? — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.