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

Protected Observables #17

Closed
mattmccray opened this issue Aug 20, 2015 · 3 comments
Closed

Protected Observables #17

mattmccray opened this issue Aug 20, 2015 · 3 comments

Comments

@mattmccray
Copy link

Would it be useful to have support for "protected" observables? Meaning, reactive values that cannot be set outside of a transaction block.

import {makeReactive, transaction} from 'mobservable'

export let authInfo= makeReactive({
  authenticated: false,
  authenticatedAt: null,
  currentUser: null
}, {
  protected: true
})

export function login(user, pass) {
  return api
    .authenticate(user, pass)
    .then( user => {
      transaction( _ => {
        authInfo.authenticated = true
        authInfo.authenticatedAt = new Date()
        authInfo.currentUser = user
      })
      return authInfo.currentUser
    })
}

export function logout() {
  transaction( _ => {
    authInfo.authenticated = false
    authInfo.authenticatedAt = null
    authInfo.currentUser = null
  })
}

Using the above example, if we were to try directly assigning to a protected value (authInfo.authenticated = true) it should fail.

But would it ignore the input or throw an error? Would mobservable take a performance hit to add this? Is this even a good idea? 😨 💬

@mweststrate
Copy link
Member

It is quite do-able to implement this, but I'm still reluctant to say whether such safe guards are the responsibility of Mobservable. So for now I will close the issue, until similar feature requests or more 👍's come in.

Thanks for the proposal nonetheless!

@mattmccray
Copy link
Author

For anyone that stumbles across this -- The feature I'm describing is basically now supported with v2.2's actions: https://mobxjs.github.io/mobx/refguide/action.html

@mweststrate
Copy link
Member

Also note that intercept allows you to guard the access to very specific observables if you want more fine grained control.

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

2 participants