Last active
December 30, 2019 01:14
-
-
Save alexytiger/2f9c6e1f4fd840766ce9febcc77b8388 to your computer and use it in GitHub Desktop.
e-book
This file contains hidden or 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 { take, tap, filter, switchMap, catchError } from 'rxjs/operators'; | |
import * as fromRoot from '../store'; | |
@Injectable({ | |
providedIn: 'root', | |
}) | |
export class EthInitGuard implements CanActivate { | |
constructor(private store: Store<fromRoot.AppState>) {} | |
canActivate(): Observable<boolean> { | |
return this.checkStore().pipe( | |
switchMap(() => of(true)), | |
catchError(() => of(false)) | |
); | |
} | |
checkStore(): Observable<boolean> { | |
return this.store.pipe( | |
select(fromRoot.getMetaMaskEnable), | |
tap(connected => { | |
if (!connected) { | |
this.store.dispatch(fromRoot.Web3ProviderActions.init()); | |
} | |
}), | |
filter(connected => connected), | |
take(1) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment