Skip to content

Instantly share code, notes, and snippets.

@htrex
Last active March 21, 2017 23:24
Show Gist options
  • Save htrex/75df09fb7c839b41041aa35974aaef01 to your computer and use it in GitHub Desktop.
Save htrex/75df09fb7c839b41041aa35974aaef01 to your computer and use it in GitHub Desktop.
app.module.ts sample for ngx-warehouse debug
import { NgModule, ApplicationRef } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';
import { removeNgStyles, createNewHosts, createInputTransfer} from '@angularclass/hmr';
// Guard & Services
import { NgxWarehouseModule } from 'ngx-warehouse';
import { AuthGuard } from './_guards/auth.guard';
import { AuthenticationService, APICommonService } from './_services/index';
// Fake backend
//import { fakeBackendProvider } from './_helpers/index';
//import { MockBackend, MockConnection } from '@angular/http/testing';
//import { BaseRequestOptions } from '@angular/http';
/*
* Platform and Environment providers/directives/pipes
*/
import { ENV_PROVIDERS } from './environment';
import { ROUTES } from './app.routes';
// App is our top level component
import { App } from './app.component';
import { APP_RESOLVER_PROVIDERS } from './app.resolver';
import { AppState, InteralStateType } from './app.service';
import { AppConfig } from './app.config';
import { ErrorComponent } from './error/error.component';
// Application wide providers
const APP_PROVIDERS = [
...APP_RESOLVER_PROVIDERS,
AppState,
AppConfig
];
type StoreType = {
state: InteralStateType,
restoreInputValues: () => void,
disposeOldHosts: () => void
};
/**
* `AppModule` is the main entry point into Angular2's bootstraping process
*/
@NgModule({
bootstrap: [App],
declarations: [
App,
ErrorComponent
],
imports: [ // import Angular's modules
BrowserModule,
FormsModule,
HttpModule,
NgxWarehouseModule,
RouterModule.forRoot(ROUTES, {useHash: true})
],
providers: [ // expose our Services and Providers into Angular's dependency injection
ENV_PROVIDERS,
APP_PROVIDERS,
AuthGuard,
APICommonService,
AuthenticationService
// Fake Backend
//fakeBackendProvider,
//MockBackend,
//BaseRequestOptions
]
})
export class AppModule {
constructor(public appRef: ApplicationRef, public appState: AppState) {
}
hmrOnInit(store: StoreType) {
if (!store || !store.state) return;
console.log('HMR store', JSON.stringify(store, null, 2));
// set state
this.appState._state = store.state;
// set input values
if ('restoreInputValues' in store) {
let restoreInputValues = store.restoreInputValues;
setTimeout(restoreInputValues);
}
this.appRef.tick();
delete store.state;
delete store.restoreInputValues;
}
hmrOnDestroy(store: StoreType) {
const cmpLocation = this.appRef.components.map(cmp => cmp.location.nativeElement);
// save state
const state = this.appState._state;
store.state = state;
// recreate root elements
store.disposeOldHosts = createNewHosts(cmpLocation);
// save input values
store.restoreInputValues = createInputTransfer();
// remove styles
removeNgStyles();
}
hmrAfterDestroy(store: StoreType) {
// display new elements
store.disposeOldHosts();
delete store.disposeOldHosts;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment