Skip to content

Commit 94e95a2

Browse files
committed
feat(hmr): createInputTransfer
1 parent 92f3de8 commit 94e95a2

File tree

2 files changed

+9
-20
lines changed

2 files changed

+9
-20
lines changed

src/app/app.module.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { BrowserModule } from '@angular/platform-browser';
33
import { FormsModule } from '@angular/forms';
44
import { HttpModule } from '@angular/http';
55
import { RouterModule } from '@angular/router';
6-
import { removeNgStyles, createNewHosts } from '@angularclass/hmr';
6+
import { removeNgStyles, createNewHosts, createInputTransfer } from '@angularclass/hmr';
77

88
/*
99
* Platform and Environment providers/directives/pipes
@@ -26,8 +26,8 @@ const APP_PROVIDERS = [
2626
];
2727

2828
type StoreType = {
29-
$inputs: any[],
3029
state: InteralStateType,
30+
restoreInputValues: () => void,
3131
disposeOldHosts: () => void
3232
};
3333

@@ -59,36 +59,25 @@ export class AppModule {
5959

6060
hmrOnInit(store: StoreType) {
6161
if (!store || !store.state) return;
62-
console.log('HMR store', store);
62+
console.log('HMR store', JSON.stringify(store, null, 2));
6363
// set state
6464
this.appState._state = store.state;
65-
6665
// set input values
67-
const inputs = document.querySelectorAll('input');
68-
if (store.$inputs && inputs.length === store.$inputs.length) {
69-
store.$inputs.forEach((value, i) => {
70-
let el = inputs[i];
71-
el.value = value;
72-
el.dispatchEvent(new CustomEvent('input', {detail: el.value}));
73-
});
74-
}
75-
66+
if ('restoreInputValues' in store) { store.restoreInputValues(); }
7667
this.appRef.tick();
7768
delete store.state;
69+
delete store.restoreInputValues;
7870
}
7971

8072
hmrOnDestroy(store: StoreType) {
8173
const cmpLocation = this.appRef.components.map(cmp => cmp.location.nativeElement);
82-
// recreate elements
8374
// save state
8475
const state = this.appState._state;
8576
store.state = state;
77+
// recreate root elements
8678
store.disposeOldHosts = createNewHosts(cmpLocation);
87-
8879
// save input values
89-
const inputs = document.querySelectorAll('input');
90-
const $inputs = [].slice.call(inputs).map(input => input.value);
91-
store.$inputs = $inputs;
80+
store.restoreInputValues = createInputTransfer();
9281
// remove styles
9382
removeNgStyles();
9483
}

src/main.browser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export function main(): Promise<any> {
1919
.bootstrapModule(AppModule)
2020
.then(decorateModuleRef)
2121
.catch(err => console.error(err));
22-
2322
}
2423

25-
24+
// needed for hmr
25+
// in prod this is replace for document ready
2626
bootloader(main);

0 commit comments

Comments
 (0)