store icon indicating copy to clipboard operation
store copied to clipboard

Call the changedHandler after the value has been set

Open Kennethtruyers opened this issue 6 years ago • 5 comments

I'm submitting a feature request

  • Library Version: 1.1.0

Please tell us about your environment:

  • Operating System: all

  • Node Version: all

  • NPM Version: all
  • JSPM OR Webpack AND Version both
  • Browser: all

  • Language: all

Current behavior: Whenever we subscribe to a part of the state, the changed handler will be called before the actual state is changed.

@autoinject()
@connectTo({
  selector: {
    list: store => store.state.pipe(pluck('list'))
  }
})
export class App {
  store;
  list : string[] = [];
  normalBinding : string;

  constructor(store : Store<any>) {
    this.store = store;
    this.store.registerAction('addList', addList);
  }

  listChanged(newValue, oldValue) {
    // this.list does not have the newValue yet, in contrast with normal 
    // aurelia behavior where this.list would have the value when this handler is called
  }

   normalBindingChanged(newValue, oldValue){
     // this.normalBinding is already set at this moment
   }
  }
}
  • What is the expected behavior? The bound value should be set before the changed-handler is invoked

  • What is the motivation / use case for changing the behavior? It aligns better with the default Aurelia behavior and meets the expectations of users better

Kennethtruyers avatar Oct 04 '18 14:10 Kennethtruyers

@zewa666 I do not see a problem with this change as I hardly ever use the newVal argument with Aurelia's change handling since the value is already set. I think i left the execution the same as it was prior to the PR because of this comment in the code:

call onChanged first so that the handler has also access to the previous state

However, based on the other thread it seems that you are fine with the change. Hopefully it just involves moving code as the change handers are fired here and property setting is done here

jmzagorski avatar Oct 04 '18 16:10 jmzagorski

This issue is related to the fact that the connectTo decorator attaches subscriptions inside of bind and unbind but I've found sometimes this is too late and you get the problem you speak of. I find if I setup a subscription inside of the constructor itself then I don't run into these issues where the value is unavailable. It's a decent workaround.

Not sure if your provided code was just a non-function example, but your list property is not an observable so Aurelia won't call listChanged because it is not observing the property. Although, you might know this already.

Vheissu avatar Oct 26 '18 03:10 Vheissu

@zewa666 I can work on this since I am familiar with the changes I submitted for the connectTo decorator.

@Vheissu yes i agree, especially when using the aurelia-router because activate fires first and sometimes i need my selectors during the activate method, so I am always overriding the connectTo setup/teardown functions

jmzagorski avatar Oct 26 '18 13:10 jmzagorski

Hi all, what is the status of this issue?

rmja avatar Jan 13 '20 21:01 rmja

Hey @rmja. It was left open due to the reason that it's going to introduce a breaking change and the actual value of the change is relatively small. So my thinking was to either wait until there is something else thats going to create one or do this with the port to Aurelia2.

zewa666 avatar Jan 14 '20 05:01 zewa666