socialnetworknews-web

A open-source Paper.li clone
git clone git://archive.git.mtrnord.blog/SocialNetworkNews/socialnetworknews-web.git
Log | Files | Refs | README | LICENSE

commit 8c8ba28ec238d0e2a90a5c5e3488724028c7ae94
parent 22363ee808130effdb79ac8dc3420b0f3ac26823
Author: MTRNord <mtrnord1@gmail.com>
Date:   Sat, 19 May 2018 14:25:47 +0200

Fix rxjs code

Diffstat:
Mpackage.json | 2+-
Msrc/app/callback/twitter/twitter.component.ts | 66+++++++++++++++++++++++++++++++++++++-----------------------------
Msrc/app/navbar/navbar.component.ts | 79+++++++++++++++++++++++++++++++++++++++++++------------------------------------
Msrc/app/papers-list/papers-list.component.ts | 32++++++++++++++++++++------------
Msrc/app/tweet-list/tweet-list.component.ts | 69+++++++++++++++++++++++++++++++++++++++++----------------------------
5 files changed, 142 insertions(+), 106 deletions(-)

diff --git a/package.json b/package.json @@ -38,7 +38,7 @@ "ng-http-loader": "^1.0.0", "ngx-toastr": "^8.7.3", "raven-js": "^3.22.3", - "rxjs": "^6.0.0", + "rxjs": "^6.1.0", "ts-loader": "^4.0.0", "web-animations-js": "^2.3.1", "zone.js": "^0.8.24" diff --git a/src/app/callback/twitter/twitter.component.ts b/src/app/callback/twitter/twitter.component.ts @@ -1,4 +1,5 @@ import {throwError as observableThrowError, of} from 'rxjs'; +import {delay, mergeMap, retryWhen, take, concat} from 'rxjs/operators'; import {Component, Inject, OnInit} from '@angular/core'; import {ActivatedRoute, Router} from '@angular/router'; import {ToastrService} from 'ngx-toastr'; @@ -30,38 +31,45 @@ export class TwitterComponent implements OnInit { this.oauth_verifier = params.oauth_verifier; this.http.get(`${this.url}/login/twitter/callback?oauth_token=${this.oauth_token}&oauth_verifier=${this.oauth_verifier}`, { observe: 'response' }) - // TODO: Better variable naming - .retryWhen(oerror => { - return oerror - .mergeMap((error: any) => { - if (String(error.status).startsWith('50')) { - return of(error.status).delay(1000); - } else if (error.status === 404) { - return observableThrowError({error: 'Sorry, there was an error. The Server just doesn\'t find any data for the requested time :('}); - } - return observableThrowError({error: 'Unknown error'}); + .pipe( + // TODO: Better variable naming + retryWhen(oerror => { + return oerror + .pipe( + mergeMap((error: any) => { + if (String(error.status).startsWith('50')) { + return of(error.status) + .pipe( + delay(1000) + ); + } else if (error.status === 404) { + return observableThrowError({error: 'Sorry, there was an error. The Server just doesn\'t find any data for the requested time :('}); + } + return observableThrowError({error: 'Unknown error'}); + }), + take(5), + // TODO: Allow to link to a Status Page + concat(observableThrowError({error: 'Sorry, there was an error (after 5 retries). This probably means we can\'t reach our API Server :('})) + ); }) - .take(5) - // TODO: Allow to link to a Status Page - .concat(observableThrowError({error: 'Sorry, there was an error (after 5 retries). This probably means we can\'t reach our API Server :('})); - }) + ) .subscribe( - data => { - this.apiService.userUUID = data.headers.get('UUID'); + data => { + this.apiService.userUUID = data.headers.get('UUID'); - // TODO redirect to the Profile instead of home page - this.apiService.inCallback = false; - this.router.navigate(['/']); - }, - err => { - this.toastr.error(err['error'], 'Error connecting API', { - positionClass: 'toast-top-center', - disableTimeOut: true - }); - throw err; - }, - () => console.log('done making callback') - ); + // TODO redirect to the Profile instead of home page + this.apiService.inCallback = false; + this.router.navigate(['/']); + }, + err => { + this.toastr.error(err['error'], 'Error connecting API', { + positionClass: 'toast-top-center', + disableTimeOut: true + }); + throw err; + }, + () => console.log('done making callback') + ); }); } diff --git a/src/app/navbar/navbar.component.ts b/src/app/navbar/navbar.component.ts @@ -1,4 +1,5 @@ import {throwError as observableThrowError, of} from 'rxjs'; +import {delay, mergeMap, retryWhen, take, concat} from 'rxjs/operators'; import { Component, OnInit } from '@angular/core'; import {ToastrService} from 'ngx-toastr'; import {ApiService} from '../api.service'; @@ -21,43 +22,49 @@ export class NavbarComponent implements OnInit { authCheck() { this.apiService.checkIfUserIsAuthenticated() - // TODO: Better variable naming - .retryWhen(oerror => { - return oerror - .mergeMap((error: any) => { - if (String(error.status).startsWith('50')) { - return of(error.status).delay(1000); - } else if (error.status === 404) { - return observableThrowError({error: 'Sorry, there was an error. The Server just doesn\'t find any data for the requested time :('}); - } else if (error.status === 401) { - return observableThrowError({error: '401'}); - } - return observableThrowError({error: 'Unknown error'}); - }) - .take(5) - // TODO: Allow to link to a Status Page - .concat(observableThrowError({error: 'Sorry, there was an error (after 5 retries). This probably means we can\'t reach our API Server :('})); - }) + .pipe( + // TODO: Better variable naming + retryWhen(oerror => { + return oerror + .pipe( + mergeMap((error: any) => { + if (String(error.status).startsWith('50')) { + return of(error.status).pipe( + delay(1000) + ); + } else if (error.status === 404) { + return observableThrowError({error: 'Sorry, there was an error. The Server just doesn\'t find any data for the requested time :('}); + } else if (error.status === 401) { + return observableThrowError({error: '401'}); + } + return observableThrowError({error: 'Unknown error'}); + }), + take(5), + // TODO: Allow to link to a Status Page + concat(observableThrowError({error: 'Sorry, there was an error (after 5 retries). This probably means we can\'t reach our API Server :('})) + ); + }) + ) .subscribe( - data => { - this.authenticated = true; - if (!this.apiService.userUUID) { - this.apiService.userUUID = data.headers.get('UUID'); - } - }, - err => { - if (err['error'] === '401') { - this.authenticated = false; - } else { - this.toastr.error(err['error'], 'Error connecting API', { - positionClass: 'toast-top-center', - disableTimeOut: true - }); - throw err; - } - }, - () => console.log('done checking if authenticated') - ); + data => { + this.authenticated = true; + if (!this.apiService.userUUID) { + this.apiService.userUUID = data.headers.get('UUID'); + } + }, + err => { + if (err['error'] === '401') { + this.authenticated = false; + } else { + this.toastr.error(err['error'], 'Error connecting API', { + positionClass: 'toast-top-center', + disableTimeOut: true + }); + throw err; + } + }, + () => console.log('done checking if authenticated') + ); } } diff --git a/src/app/papers-list/papers-list.component.ts b/src/app/papers-list/papers-list.component.ts @@ -4,6 +4,7 @@ import {ApiService, Paper} from '../api.service'; import * as Raven from 'raven-js'; import {ToastrService} from 'ngx-toastr'; import {Lightbox} from '../utils/lightbox'; +import {concat, delay, mergeMap, retryWhen, take} from 'rxjs/operators'; @Component({ selector: 'app-papers-list', @@ -27,18 +28,25 @@ export class PapersListComponent implements OnInit { getPapers() { this.apiService.getPapers() // TODO: Better variable naming - .retryWhen(oerror => { - return oerror - .mergeMap((error: any) => { - if (String(error.status).startsWith('50')) { - return of(error.status).delay(1000); - } - return observableThrowError({error: 'Unknown error'}); - }) - .take(5) - // TODO: Allow to link to a Status Page - .concat(observableThrowError({error: 'Sorry, there was an error (after 5 retries). This probably means we can\'t reach our API Server :('})); - }) + .pipe( + retryWhen(oerror => { + return oerror + .pipe( + mergeMap((error: any) => { + if (String(error.status).startsWith('50')) { + return of(error.status) + .pipe( + delay(1000) + ); + } + return observableThrowError({error: 'Unknown error'}); + }), + take(5), + // TODO: Allow to link to a Status Page + concat(observableThrowError({error: 'Sorry, there was an error (after 5 retries). This probably means we can\'t reach our API Server :('})) + ); + }) + ) .subscribe( data => this.data = data, err => { diff --git a/src/app/tweet-list/tweet-list.component.ts b/src/app/tweet-list/tweet-list.component.ts @@ -6,6 +6,7 @@ import {ApiService, Paper, TweetsEntity} from '../api.service'; import {ToastrService} from 'ngx-toastr'; import {Meta, Title} from '@angular/platform-browser'; import {Lightbox} from '../utils/lightbox'; +import {concat, delay, mergeMap, retryWhen, take} from 'rxjs/operators'; @@ -37,20 +38,27 @@ export class TweetListComponent implements OnInit { getPaper() { this.apiService.getPaper(this.uuid) // TODO: Better variable naming - .retryWhen(oerror => { - return oerror - .mergeMap((error: any) => { - if (String(error.status).startsWith('50')) { - return of(error.status).delay(1000); - } else if (error.status === 404) { - return observableThrowError({error: 'Sorry, there was an error. The Server just doesn\'t find the requested paper :('}); - } - return observableThrowError({error: 'Unknown error'}); - }) - .take(5) - // TODO: Allow to link to a Status Page - .concat(observableThrowError({error: 'Sorry, there was an error (after 5 retries). This probably means we can\'t reach our API Server :('})); - }) + .pipe( + retryWhen(oerror => { + return oerror + .pipe( + mergeMap((error: any) => { + if (String(error.status).startsWith('50')) { + return of(error.status) + .pipe( + delay(1000) + ); + } else if (error.status === 404) { + return observableThrowError({error: 'Sorry, there was an error. The Server just doesn\'t find the requested paper :('}); + } + return observableThrowError({error: 'Unknown error'}); + }), + take(5), + // TODO: Allow to link to a Status Page + concat(observableThrowError({error: 'Sorry, there was an error (after 5 retries). This probably means we can\'t reach our API Server :('})) + ); + }) + ) .subscribe( data => { this.paperData = data; @@ -79,20 +87,25 @@ export class TweetListComponent implements OnInit { getYesterday() { this.apiService.getYesterday(this.uuid) // TODO: Better variable naming - .retryWhen(oerror => { - return oerror - .mergeMap((error: any) => { - if (String(error.status).startsWith('50')) { - return of(error.status).delay(1000); - } else if (error.status === 404) { - return observableThrowError({error: 'Sorry, there was an error. The Server just doesn\'t find any data for the requested time :('}); - } - return observableThrowError({error: 'Unknown error'}); - }) - .take(5) - // TODO: Allow to link to a Status Page - .concat(observableThrowError({error: 'Sorry, there was an error (after 5 retries). This probably means we can\'t reach our API Server :('})); - }) + .pipe( + retryWhen(oerror => { + return oerror + .pipe( + mergeMap((error: any) => { + if (String(error.status).startsWith('50')) { + return of(error.status) + .pipe(delay(1000)); + } else if (error.status === 404) { + return observableThrowError({error: 'Sorry, there was an error. The Server just doesn\'t find any data for the requested time :('}); + } + return observableThrowError({error: 'Unknown error'}); + }), + take(5), + // TODO: Allow to link to a Status Page + concat(observableThrowError({error: 'Sorry, there was an error (after 5 retries). This probably means we can\'t reach our API Server :('})) + ); + }) + ) .subscribe( data => { this.data = this.sort(data.tweets); }, err => {