Last active
March 2, 2019 19:33
-
-
Save alexytiger/973951109b47a65742399aeb4ebf9e2a 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
import { Injectable } from '@angular/core'; | |
import { CanActivate } from '@angular/router'; | |
import { Store, select } from '@ngrx/store'; | |
import { Observable, of } from 'rxjs'; | |
import { map, take, tap, filter, switchMap, catchError } from 'rxjs/operators'; | |
import {EthAnchorModule} from '../eth.anchor.module'; | |
import * as fromEth from '../../ethereum'; | |
@Injectable({ | |
providedIn: EthAnchorModule, | |
}) | |
export class EthInitGuard implements CanActivate { | |
constructor(private store: Store<fromEth.AppState>) {} | |
canActivate(): Observable<boolean> { | |
return this.checkStore().pipe( | |
switchMap(() => of(true)), | |
catchError(() => of(false)) | |
); | |
} | |
checkStore(): Observable<boolean> { | |
return this.store.pipe( | |
select(fromEth.getConStatus), | |
tap(connected => { | |
if (!connected) { | |
this.store.dispatch(new fromEth.InitEth()); | |
} | |
}), | |
filter(connected => connected), | |
take(1) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment