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

ObservableMap replace triggers a change on unchanged values #1243

Closed
elado opened this issue Nov 15, 2017 · 4 comments
Closed

ObservableMap replace triggers a change on unchanged values #1243

elado opened this issue Nov 15, 2017 · 4 comments

Comments

@elado
Copy link
Contributor

elado commented Nov 15, 2017

ObservableMap .replace uses .clear, which causes reactions on all values in the map, even if they haven't changed.

import { observable, computed, autorun } from 'mobx'

class Store {
  m = observable.map({ a: 1, b: 2, c: 3 })

  @computed
  get a() {
    console.log('a recompute')
    return this.m.get('a')
  }
}

const store = new Store()

autorun(() => {
  console.log(store.a)
})

console.log('changing b...')

store.m.set('b', 4)

console.log('setting a to same value...')
// won't recompute a
store.m.set('a', 1)

console.log('replacing...')

// store.a will be recomputed even when it wasn't changed
store.m.replace({ a: 1, d: 5 })

https://codesandbox.io/s/61kwwnjvlr

I would expect it to remove missing keys instead of clear and use merge on the rest, and then it won't trigger changes on unchanged values.

https://github.com/mobxjs/mobx/blob/master/src/types/observablemap.ts#L305-L307

@mweststrate
Copy link
Member

mweststrate commented Nov 17, 2017 via email

@elado
Copy link
Contributor Author

elado commented Nov 17, 2017

yes! will do in the next few days.

@mweststrate
Copy link
Member

mweststrate commented Nov 17, 2017 via email

@elado
Copy link
Contributor Author

elado commented Nov 19, 2017

#1247

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