Commit c0a36ab6 by Tuukka Kivilahti

Merge branch '109-rxjs-update' into 'master'

rxjs update

Closes #109

See merge request !414
2 parents 2266beb7 198eef70
......@@ -33,7 +33,6 @@
"core-js": "^2.5.1",
"ngx-bootstrap": "^2.0.2",
"rxjs": "^6.2.2",
"rxjs-compat": "^6.2.2",
"ts-helpers": "^1.1.1",
"zone.js": "^0.8.26"
},
......
import {map, catchError} from 'rxjs/operators';
import { Injectable } from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {Observable, of } from 'rxjs';
import {MoyaLocale} from './moya-locale.model';
import {MOYA_BASE_URL} from '../../shared/tools/moya-rest.tool';
import {HttpClient} from '@angular/common/http';
import 'rxjs/add/operator/catch';
import {TranslateService} from '@ngx-translate/core';
......@@ -69,11 +71,11 @@ export class LocaleService {
return new Observable<string>(x => x.next(this.selectedLocale));
}
return this.http.get<MoyaLocale>(MOYA_BASE_URL + '/v3/locale/')
.catch(x =>
Observable.of({} as MoyaLocale)
)
.map(locale => {
return this.http.get<MoyaLocale>(MOYA_BASE_URL + '/v3/locale/').pipe(
catchError(x =>
of({} as MoyaLocale)
),
map(locale => {
if (locale && locale.userLocale) {
return this.selectLocale(locale.userLocale);
}
......@@ -89,7 +91,7 @@ export class LocaleService {
}
return this.selectLocale(DEFAULT_LOCALE);
});
}),);
}
......@@ -107,9 +109,9 @@ export class LocaleService {
};
// let's save locale to database, if it fails, we save it into localstorage. No errors to show for user.
this.http.post(MOYA_BASE_URL + '/v3/locale/', newLocale).catch(x => {
this.http.post(MOYA_BASE_URL + '/v3/locale/', newLocale).pipe(catchError(x => {
localStorage.setItem(LOCALSTORAGE_NAME, locale);
return 'ok';
}).subscribe();
})).subscribe();
}
}
import {filter} from 'rxjs/operators';
import {Component, OnInit} from '@angular/core';
import {MenuGroup} from '../models/menu-group.model';
import {MENU} from '../defines/menu';
......@@ -33,7 +35,7 @@ export class LeftMenuComponent {
constructor(private route: Router) {
route.events.filter(e => e instanceof RouterEvent).subscribe(event => {
route.events.pipe(filter(e => e instanceof RouterEvent)).subscribe(event => {
const revent = event as RouterEvent;
......
import {map} from 'rxjs/operators';
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import {Location, LocationStrategy, HashLocationStrategy, PathLocationStrategy} from '@angular/common';
import {NavigationExtras, ActivatedRoute, Router} from '@angular/router';
......@@ -22,7 +24,7 @@ export class OldMoyaComponent implements OnInit {
constructor(private location: Location, private route: ActivatedRoute) {
route.queryParamMap.map(a => a.get('p')).subscribe(x => {
route.queryParamMap.pipe(map(a => a.get('p'))).subscribe(x => {
console.log(x);
if (x) {
this.frameUrl = '/MoyaWeb/' + x.replace('::', '?');
......
import {Component, NgZone, OnInit} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {Observable} from 'rxjs';
import {Vip} from '../models/vip.model';
import {ViplistService} from '../viplist.service';
import {TranslatePipe} from '@ngx-translate/core';
......
import {forkJoin as observableForkJoin, Observable} from 'rxjs';
import {first, switchMap, map} from 'rxjs/operators';
import {Injectable} from '@angular/core';
import {Vip} from './models/vip.model';
import {User} from '../../shared/models/user.model';
import {UserService} from '../../shared/services/user.service';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/observable/forkJoin';
import 'rxjs/add/operator/first';
import {HttpClient} from '@angular/common/http';
import {MOYA_BASE_URL} from '../../shared/tools/moya-rest.tool';
......@@ -25,12 +28,12 @@ export class ViplistService {
public get(searchString?: string): Observable<Array<Vip>> {
if (!searchString) {
return this.http.get(MOYA_BASE_URL + 'v3/vip/all')
.switchMap(res => Observable.forkJoin(...(res as Array<any>).map(apiRow => this.hostPopulator(apiRow))));
return this.http.get(MOYA_BASE_URL + 'v3/vip/all').pipe(
switchMap(res => observableForkJoin(...(res as Array<any>).map(apiRow => this.hostPopulator(apiRow)))));
}
return this.http.get(MOYA_BASE_URL + 'v3/vip/search/' + searchString)
.switchMap(v => Observable.forkJoin(...(v as Array<any>).map(x => this.hostPopulator(x))));
return this.http.get(MOYA_BASE_URL + 'v3/vip/search/' + searchString).pipe(
switchMap(v => observableForkJoin(...(v as Array<any>).map(x => this.hostPopulator(x)))));
}
......@@ -46,8 +49,8 @@ export class ViplistService {
throw new Error('TODO: errori, tyhmä vippi');
}
const res: any = await this.http.delete(MOYA_BASE_URL + 'v3/vip/' + vip.id)
.first()
const res: any = await this.http.delete(MOYA_BASE_URL + 'v3/vip/' + vip.id).pipe(
first())
.toPromise();
return res.ok;
......@@ -55,19 +58,19 @@ export class ViplistService {
public getWithId(id: number): Observable<Vip> {
return this.http.get(MOYA_BASE_URL + 'v3/vip/' + id).map(x => x as Vip);
return this.http.get(MOYA_BASE_URL + 'v3/vip/' + id).pipe(map(x => x as Vip));
}
public create(vip: Vip): Observable<Vip> {
return this.http.post(MOYA_BASE_URL + 'v3/vip/create', vip).map(x => x as Vip);
return this.http.post(MOYA_BASE_URL + 'v3/vip/create', vip).pipe(map(x => x as Vip));
}
private hostPopulator(rawVip: any): Observable<Vip> {
return this.userService.get(rawVip.hostId)
.map((u: User) => {rawVip.host = u; return <Vip> rawVip; }).map(x => x as Vip);
return this.userService.get(rawVip.hostId).pipe(
map((u: User) => {rawVip.host = u; return <Vip> rawVip; }),map(x => x as Vip),);
}
......
import {throwError as observableThrowError, Observable} from 'rxjs';
import {catchError} from 'rxjs/operators';
import {HttpEvent, HttpEventType, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
export class ErrorInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<HttpEventType.Response>> {
return next.handle(req).catch(this.handleException);
return next.handle(req).pipe(catchError(this.handleException));
}
......@@ -17,6 +20,6 @@ export class ErrorInterceptor implements HttpInterceptor {
// TODO: add handlers to 403's and other "not logged in" or "invalid permissions"
// -statuscodes, and route them using some nice global parameter
return Observable.throw(error);
return observableThrowError(error);
}
}
import {of as observableOf, timer as observableTimer, Observable, forkJoin, Subscription} from 'rxjs';
import {refCount, publishLast, tap} from 'rxjs/operators';
import {Injectable, NgZone} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/timer';
import {forkJoin} from 'rxjs/observable/forkJoin';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/publishLast';
import 'rxjs/add/observable/of';
import {Subscription} from 'rxjs/Subscription';
const DEFAULT_EXPIRE_MS = 300000; // 5min
......@@ -53,7 +50,7 @@ export class CacheService {
constructor(private zone: NgZone) {
this.cache = new Map<string, CachedItem>();
this.timerObservable = Observable.timer(30000, 30000); // afther 0.5min, every 0.5min.
this.timerObservable = observableTimer(30000, 30000); // afther 0.5min, every 0.5min.
}
......@@ -88,9 +85,9 @@ export class CacheService {
console.log(cacheName, ' not in cache');
const hotSource = source
.do(val => this.cache.set(cacheName, new CachedItem(moduleName, cachePath, val)))
.publishLast().refCount();
const hotSource = source.pipe(
tap(val => this.cache.set(cacheName, new CachedItem(moduleName, cachePath, val))),
publishLast(),refCount(),);
this.cache.set(cacheName, new CachedItem(moduleName, cachePath, hotSource, true));
......@@ -112,7 +109,7 @@ export class CacheService {
console.log(cacheName, ' value in cache');
this.checkCleanTimer();
return Observable.of(cacheItem.value);
return observableOf(cacheItem.value);
}
......
import { tap } from 'rxjs/operators';
import {throwError as observableThrowError, Observable} from 'rxjs';
import {Injectable} from '@angular/core';
import {User} from '../models/user.model';
import {CacheService} from './cache.service';
import {Observable} from 'rxjs/Observable';
import {HttpClient} from '@angular/common/http';
import {MOYA_BASE_URL} from '../tools/moya-rest.tool';
......@@ -14,14 +15,13 @@ export class UserService {
public get(id: number): Observable<User> {
if (!id || id < 0) {
return Observable.throw('There should be userid');
return observableThrowError('There should be userid');
}
const path = MOYA_BASE_URL + 'v2/user/' + id;
return this.cacheService.cacheObservable('moya:UserService', path,
this.http.get<User>(path)
.do(v => {console.log('getting user outside of cache', path); }));
this.http.get<User>(path).pipe(tap(v => {console.log('getting user outside of cache', path); })));
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!