Created
February 7, 2018 18:28
-
-
Save kseniya292/af4575b8bd4de877dbf9a66a875f1713 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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