Skip to content
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

configure enforceActions does not work! #1680

Closed
up209d opened this issue Aug 13, 2018 · 7 comments
Closed

configure enforceActions does not work! #1680

up209d opened this issue Aug 13, 2018 · 7 comments

Comments

@up209d
Copy link

up209d commented Aug 13, 2018

I was using umd mobx 5.0.3 and I couldn't get configure({ enforceActions: true }) works. It just does nothing. There is no error thrown because of changing observable object outside of an action.

Could anyone take a look into my sample code to see where is my problem here? Otherwise, it might be a bug, isn't it? Thank you.

https://codepen.io/up209d/pen/KBENbz

@kuitos
Copy link
Member

kuitos commented Aug 13, 2018

I guess enforceActions: 'strict' was what u want, but not enforceActions: true.
check the doc https://mobx.js.org/refguide/api.html#-enforceactions-boolean

@up209d
Copy link
Author

up209d commented Aug 13, 2018

That's not true as it was stated:

In sloppy (true) mode, MobX will only throw if an observed observable is modified, in "strict" mode it will even throw for non-observed observables

If it is not a bug, I guess that feature just doesn't work with production build as well as umd version cuz it has meaning only in developing phase.

@mweststrate
Copy link
Member

mweststrate commented Aug 13, 2018 via email

@up209d
Copy link
Author

up209d commented Aug 13, 2018

I did assign observable on githubProjects and state of Store class in the decorate, and you can see I did fire fetchProjects and in the callback of that method, I change githubProjects and state not in an action, and no error was thrown. Am I doing wrong?

const { observable, action, configure, computed, decorate } = mobx;

configure({ enforceActions: true })

class Store {
    githubProjects = []
    state = "pending" // "pending" / "done" / "error"
    fetchProjects(cb) {
        this.githubProjects = []
        this.state = "pending"
        fetch('https://api.github.com/users').then(
            projects => {
              projects.json().then((data)=>{
                this.githubProjects = data
                this.state = "done"
                cb(data);
              })
            },
            error => {
                this.state = "error"
            }
        )
    }
    get count() {
      return this.githubProjects.length;
    }
}

decorate(Store,{
  githubProjects: observable,
  state: observable
})

const store = new Store()
console.log(store.state, store.count)

store.fetchProjects(()=>{
  console.log(store.state, store.count)
})



@mweststrate
Copy link
Member

In sloppy (true) mode, MobX will only throw if an observed observable is modified, in "strict" mode it will even throw for non-observed observables

Maybe not the most optimal formulation (PR welcome!), but in other words, in "strict" mode it will throw on all mutations, of both non-observed and observed observables. While in loose mode (true for now, PR for changing that to "loose" welcome as well ;-)), non observed observables can be modified outside actions. The reason for that is that observables that are not being observed, cannot cause unexpected side effects, and are from that perspective safe to modify.

@urugator
Copy link
Collaborator

Not sure if relevant, but some time ago I proposed perhaps a bit more matching/self-explanatory nomenclature:
#1473 (comment)

@mweststrate
Copy link
Member

In MobX 4.4 / 5.1 the config values have changed to never, observed and always

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants