Skip to content
Snippets Groups Projects
Unverified Commit 32b4acc3 authored by Art Lowel's avatar Art Lowel Committed by GitHub
Browse files

Merge pull request #269 from atmire/201-pipeable-operators

Pipeable operators
parents 44e58e12 f6e47389
No related branches found
No related tags found
No related merge requests found
import { Inject, Injectable, Injector } from '@angular/core';
import { Request } from '@angular/http';
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
import { Observable } from 'rxjs';
......@@ -18,32 +18,40 @@ import { RequestEntry } from './request.reducer';
import { RequestService } from './request.service';
import { DSpaceRESTv2Serializer } from '../dspace-rest-v2/dspace-rest-v2.serializer';
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()
export class RequestEffects {
@Effect() execute = this.actions$
.ofType(RequestActionTypes.EXECUTE)
.flatMap((action: RequestExecuteAction) => {
return this.requestService.getByUUID(action.payload)
.take(1);
})
.map((entry: RequestEntry) => entry.request)
.flatMap((request: RestRequest) => {
@Effect() execute = this.actions$.ofType(RequestActionTypes.EXECUTE).pipe(
flatMap((action: RequestExecuteAction) => {
return this.requestService.getByUUID(action.payload).pipe(
take(1)
);
}),
map((entry: RequestEntry) => entry.request),
flatMap((request: RestRequest) => {
let body;
if (isNotEmpty(request.body)) {
const serializer = new DSpaceRESTv2Serializer(NormalizedObjectFactory.getConstructor(request.body.type));
body = serializer.serialize(request.body);
}
return this.restApi.request(request.method, request.href, body, request.options)
.map((data: DSpaceRESTV2Response) =>
this.injector.get(request.getResponseParser()).parse(request, data))
.do((response: RestResponse) => this.responseCache.add(request.href, response, this.EnvConfig.cache.msToLive))
.map((response: RestResponse) => new RequestCompleteAction(request.uuid))
.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)));
});
return this.restApi.request(request.method, request.href, body, request.options).pipe(
map((data: DSpaceRESTV2Response) => this.injector.get(request.getResponseParser()).parse(request, data)),
addToResponseCacheAndCompleteAction(request, this.responseCache, this.EnvConfig),
catchError((error: RequestError) => Observable.of(new ErrorResponse(error)).pipe(
addToResponseCacheAndCompleteAction(request, this.responseCache, this.EnvConfig)
))
);
})
);
constructor(
@Inject(GLOBAL_CONFIG) private EnvConfig: GlobalConfig,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment