Skip to content

Instantly share code, notes, and snippets.

@kseniya292
Created February 7, 2018 18:28
Show Gist options
  • Save kseniya292/af4575b8bd4de877dbf9a66a875f1713 to your computer and use it in GitHub Desktop.
Save kseniya292/af4575b8bd4de877dbf9a66a875f1713 to your computer and use it in GitHub Desktop.
export class AppModule {
public constructor(
private readonly transferState: TransferState,
private readonly store: Store<fromRoot.State>,
) {
const isBrowser = this.transferState.hasKey<any>(NGRX_STATE);
if (isBrowser) {
this.onBrowser();
} else {
this.onServer();
}
}
onServer() {
this.transferState.onSerialize(NGRX_STATE, () => {
let state;
this.store.subscribe( ( saveState: any ) => {
console.log('Set for browser', JSON.stringify(saveState));
state = saveState;
}).unsubscribe();
return state;
});
}
onBrowser() {
const state = this.transferState.get<any>(NGRX_STATE, null);
this.transferState.remove(NGRX_STATE);
this.store.dispatch( { type: 'SET_ROOT_STATE', payload: state } );
console.log('Got state from server', JSON.stringify(state));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment