Skip to content

Instantly share code, notes, and snippets.

@alexytiger
Last active January 18, 2020 04:34
Show Gist options
  • Save alexytiger/241cb763e1ad04cfdf570a1a3b556a59 to your computer and use it in GitHub Desktop.
Save alexytiger/241cb763e1ad04cfdf570a1a3b556a59 to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { Actions, ofType, createEffect, ROOT_EFFECTS_INIT } from '@ngrx/effects';
import { serializeError } from 'serialize-error';
import { of } from 'rxjs';
import { switchMap, map, tap, catchError } from 'rxjs/operators';
import { IpfsDaemonService } from '../../services/ipfs-daemon.services';
import { IpfsDaemonActions, ErrorActions } from '../actions';
@Injectable()
export class IpfsDaemonEffects {
constructor(
private ipfsSrv: IpfsDaemonService,
private readonly actions$: Actions
) { }
onConnect$ = createEffect(
() =>
this.actions$.pipe(
ofType(ROOT_EFFECTS_INIT),
switchMap(() =>
this.ipfsSrv.getVersion().pipe(
tap(version => console.log(`IPFS node version: ${version}`)),
map(_ => IpfsDaemonActions.connectSuccess()),
catchError((err: Error) => of(this.handleError(err)))
)
)
)
);
private handleError(error: Error) {
const friendlyErrorMessage = serializeError(error).message;
return ErrorActions.errorMessage({ errorMsg: friendlyErrorMessage });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment