Last active
April 27, 2019 13:47
-
-
Save alexytiger/6386c6a6ceab5e3f17ad3812de1bef90 to your computer and use it in GitHub Desktop.
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, Inject } from '@angular/core'; | |
import { Actions, ofType, createEffect } from '@ngrx/effects'; | |
import { of, from, EMPTY as empty } from 'rxjs'; | |
import { exhaustMap, switchMap, map, tap, catchError } from 'rxjs/operators'; | |
import { MetamaskWeb3Provider } from '../../services/tokens'; | |
import { ProviderService } from '../../services/provider.services'; | |
import { Web3ProviderActions, SpinnerActions, ErrorActions } from '../actions'; | |
@Injectable() | |
export class Web3ProviderEffects { | |
constructor( | |
@Inject(MetamaskWeb3Provider) private web3Provider, | |
private providerSrv: ProviderService, | |
private readonly actions$: Actions | |
) {} | |
getAccount$ = createEffect(() => | |
this.actions$.pipe( | |
ofType(Web3ProviderActions.initSuccess), | |
switchMap(() => | |
this.providerSrv.getAccount().pipe( | |
map((address: string) => Web3ProviderActions.account({ address })), | |
catchError((err: Error) => | |
of(ErrorActions.errorMessage({ errorMsg: err.message })) | |
) | |
) | |
) | |
) | |
); | |
getBalance$ = createEffect(() => | |
this.actions$.pipe( | |
ofType(Web3ProviderActions.initSuccess), | |
switchMap(() => | |
this.providerSrv.getBalance().pipe( | |
map((balance: string) => | |
Web3ProviderActions.balanceSuccess({ balance }) | |
), | |
catchError((err: Error) => | |
of(ErrorActions.errorMessage({ errorMsg: err.message })) | |
) | |
) | |
) | |
) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment