PatrickJS / angular-hmr

:fire: Angular Hot Module Replacement for Hot Module Reloading
https://patrickjs.com
Apache License 2.0
506 stars 45 forks source link

HmrState decorator does not accept new value during set operation #18

Closed samoshkin closed 8 years ago

samoshkin commented 8 years ago

Given component below:

export default class AppComponent implements OnInit {
  @HmrState() state = {
    counter: 1
  };

  // when object's property is mutated, updated value is persisted
  mutateInPlace() {
    this.state.counter = this.state.counter + 1;
    // at this point this.state.counter equals to 2; hmr works
  }

  // when variable is reassigned with new value (in favor of immutability), it's being discarded
  assignNewInstance() {
    this.state = {
       counter: this.state.counter + 1;
    };
    // at this point this.state.counter is still 1; weird; hmr does not work
  }
}

Most likely the issue comes from HmrDecorator.ts.

if (!currentValue) {
  HmrStore._initialValues[key] = newValue;
} else {
  newValue = Object.assign(newValue, currentValue); // << overwrites new value with old one?? 
}
return HmrStore.set(key, newValue);

I guess, it should have been as follows:

newValue = Object.assign({}, currentValue, newValue);
PatrickJS commented 8 years ago

thanks, I removed this and exposed the api. here's an example https://github.com/AngularClass/angular2-seed/blob/e5c2c3c2b8d8a96420d59d21f8e040a8f9d634c1/src/main.browser.ts#L34...L51