I'm cleaning up my angular2 project and for many reasons, I've decided to start with a seed. This one.
This seed uses HMR but I don't fully understand what is the purpose of that.
At the beginning, I was thinking that HMR was about dynamical loading and replacing component while the web app is running.
But since I've put my eyes on the app.service.ts
, I've got lost. Here is the code of this service :
import { Injectable } from '@angular/core';
import { HmrState } from 'angular2-hmr';
@Injectable()
export class AppState {
// @HmrState() is used by HMR to track the state of any object during a hot module replacement
@HmrState() _state = { };
constructor() {
}
// already return a clone of the current state
get state() {
return this._state = this._clone(this._state);
}
// never allow mutation
set state(value) {
throw new Error('do not mutate the `.state` directly');
}
get(prop?: any) {
// use our state getter for the clone
const state = this.state;
return state[prop] || state;
}
set(prop: string, value: any) {
// internally mutate our state
return this._state[prop] = value;
}
_clone(object) {
// simple object clone
return JSON.parse(JSON.stringify( object ));
}
}
I was thinking that service simply provides a space to store some data. After all, this is just an example.
But this line did confuse me: @HmrState() _state = { };
. Is this service using HMR to manage data that we can manage with this.appState.set('value', value);
(this is from the HomeComponent) like a little Redux's store (without actions, dispatcher, blabla) ?
What is the purpose of the decorator @HmrState()
here ?
Thanks.