mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 18:14:17 +00:00
Merge pull request #269 from atmire/201-pipeable-operators
Pipeable operators
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { Inject, Injectable, Injector } from '@angular/core';
|
import { Inject, Injectable, Injector } from '@angular/core';
|
||||||
import { Request } from '@angular/http';
|
import { Request } from '@angular/http';
|
||||||
import { RequestArgs } from '@angular/http/src/interfaces';
|
import { RequestArgs } from '@angular/http/src/interfaces';
|
||||||
import { Actions, Effect } from '@ngrx/effects';
|
import { Actions, Effect, ofType } from '@ngrx/effects';
|
||||||
// tslint:disable-next-line:import-blacklist
|
// tslint:disable-next-line:import-blacklist
|
||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
|
|
||||||
@@ -18,32 +18,40 @@ import { RequestEntry } from './request.reducer';
|
|||||||
import { RequestService } from './request.service';
|
import { RequestService } from './request.service';
|
||||||
import { DSpaceRESTv2Serializer } from '../dspace-rest-v2/dspace-rest-v2.serializer';
|
import { DSpaceRESTv2Serializer } from '../dspace-rest-v2/dspace-rest-v2.serializer';
|
||||||
import { NormalizedObjectFactory } from '../cache/models/normalized-object-factory';
|
import { NormalizedObjectFactory } from '../cache/models/normalized-object-factory';
|
||||||
|
import { catchError, flatMap, map, take, tap } from 'rxjs/operators';
|
||||||
|
|
||||||
|
export const addToResponseCacheAndCompleteAction = (request: RestRequest, responseCache: ResponseCacheService, envConfig: GlobalConfig) =>
|
||||||
|
(source: Observable<ErrorResponse>): Observable<RequestCompleteAction> =>
|
||||||
|
source.pipe(
|
||||||
|
tap((response: RestResponse) => responseCache.add(request.href, response, envConfig.cache.msToLive)),
|
||||||
|
map((response: RestResponse) => new RequestCompleteAction(request.uuid))
|
||||||
|
);
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class RequestEffects {
|
export class RequestEffects {
|
||||||
|
|
||||||
@Effect() execute = this.actions$
|
@Effect() execute = this.actions$.ofType(RequestActionTypes.EXECUTE).pipe(
|
||||||
.ofType(RequestActionTypes.EXECUTE)
|
flatMap((action: RequestExecuteAction) => {
|
||||||
.flatMap((action: RequestExecuteAction) => {
|
return this.requestService.getByUUID(action.payload).pipe(
|
||||||
return this.requestService.getByUUID(action.payload)
|
take(1)
|
||||||
.take(1);
|
);
|
||||||
})
|
}),
|
||||||
.map((entry: RequestEntry) => entry.request)
|
map((entry: RequestEntry) => entry.request),
|
||||||
.flatMap((request: RestRequest) => {
|
flatMap((request: RestRequest) => {
|
||||||
let body;
|
let body;
|
||||||
if (isNotEmpty(request.body)) {
|
if (isNotEmpty(request.body)) {
|
||||||
const serializer = new DSpaceRESTv2Serializer(NormalizedObjectFactory.getConstructor(request.body.type));
|
const serializer = new DSpaceRESTv2Serializer(NormalizedObjectFactory.getConstructor(request.body.type));
|
||||||
body = serializer.serialize(request.body);
|
body = serializer.serialize(request.body);
|
||||||
}
|
}
|
||||||
return this.restApi.request(request.method, request.href, body, request.options)
|
return this.restApi.request(request.method, request.href, body, request.options).pipe(
|
||||||
.map((data: DSpaceRESTV2Response) =>
|
map((data: DSpaceRESTV2Response) => this.injector.get(request.getResponseParser()).parse(request, data)),
|
||||||
this.injector.get(request.getResponseParser()).parse(request, data))
|
addToResponseCacheAndCompleteAction(request, this.responseCache, this.EnvConfig),
|
||||||
.do((response: RestResponse) => this.responseCache.add(request.href, response, this.EnvConfig.cache.msToLive))
|
catchError((error: RequestError) => Observable.of(new ErrorResponse(error)).pipe(
|
||||||
.map((response: RestResponse) => new RequestCompleteAction(request.uuid))
|
addToResponseCacheAndCompleteAction(request, this.responseCache, this.EnvConfig)
|
||||||
.catch((error: RequestError) => Observable.of(new ErrorResponse(error))
|
))
|
||||||
.do((response: RestResponse) => this.responseCache.add(request.href, response, this.EnvConfig.cache.msToLive))
|
);
|
||||||
.map((response: RestResponse) => new RequestCompleteAction(request.uuid)));
|
})
|
||||||
});
|
);
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@Inject(GLOBAL_CONFIG) private EnvConfig: GlobalConfig,
|
@Inject(GLOBAL_CONFIG) private EnvConfig: GlobalConfig,
|
||||||
|
Reference in New Issue
Block a user