Cache redesign part 1, and add support for alternative links

This commit is contained in:
Art Lowel
2020-12-11 14:18:44 +01:00
parent f4853972cc
commit 4e18fa35ca
522 changed files with 7537 additions and 6933 deletions

View File

@@ -1,9 +1,23 @@
import { merge as observableMerge, Observable, throwError as observableThrowError } from 'rxjs';
import { distinctUntilChanged, filter, find, flatMap, map, partition, take, tap } from 'rxjs/operators';
import { merge as observableMerge, Observable } from 'rxjs';
import {
distinctUntilChanged,
filter,
find,
flatMap,
map,
partition,
take,
tap
} from 'rxjs/operators';
import { Store } from '@ngrx/store';
import { hasValue, isEmpty, isNotEmpty, isNotUndefined, isUndefined } from '../../shared/empty.util';
import { ErrorResponse, PostPatchSuccessResponse, RestResponse } from '../cache/response.models';
import {
hasValue,
isEmpty,
isNotEmpty,
isNotUndefined,
isUndefined
} from '../../shared/empty.util';
import { PatchRequest } from '../data/request.models';
import { RequestService } from '../data/request.service';
import { HALEndpointService } from '../shared/hal-endpoint.service';
@@ -16,7 +30,9 @@ import {
StartTransactionPatchOperationsAction
} from './json-patch-operations.actions';
import { JsonPatchOperationModel } from './json-patch.model';
import { getResponseFromEntry } from '../shared/operators';
import { getFirstCompletedRemoteData } from '../shared/operators';
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
import { RemoteData } from '../data/remote-data';
/**
* An abstract class that provides methods to make JSON Patch requests.
@@ -27,6 +43,7 @@ export abstract class JsonPatchOperationsService<ResponseDefinitionDomain, Patch
protected abstract store: Store<CoreState>;
protected abstract linkPath: string;
protected abstract halService: HALEndpointService;
protected abstract rdbService: RemoteDataBuildService;
protected abstract patchRequestConstructor: any;
/**
@@ -85,20 +102,20 @@ export abstract class JsonPatchOperationsService<ResponseDefinitionDomain, Patch
tap(() => this.store.dispatch(new StartTransactionPatchOperationsAction(resourceType, resourceId, startTransactionTime))),
tap((request: PatchRequestDefinition) => this.requestService.configure(request)),
flatMap(() => {
const [successResponse$, errorResponse$] = partition((response: RestResponse) => response.isSuccessful)(this.requestService.getByUUID(requestId).pipe(
getResponseFromEntry(),
find((entry: RestResponse) => startTransactionTime < entry.timeAdded),
map((entry: RestResponse) => entry),
));
return observableMerge(
errorResponse$.pipe(
tap(() => this.store.dispatch(new RollbacktPatchOperationsAction(resourceType, resourceId))),
flatMap((error: ErrorResponse) => observableThrowError(error))),
successResponse$.pipe(
filter((response: PostPatchSuccessResponse) => isNotEmpty(response)),
tap(() => this.store.dispatch(new CommitPatchOperationsAction(resourceType, resourceId))),
map((response: PostPatchSuccessResponse) => response.dataDefinition),
distinctUntilChanged()));
return this.rdbService.buildFromRequestUUID(requestId).pipe(
getFirstCompletedRemoteData(),
find((rd: RemoteData<any>) => startTransactionTime < rd.timeCompleted),
map((rd: RemoteData<any>) => {
if (rd.hasFailed) {
this.store.dispatch(new RollbacktPatchOperationsAction(resourceType, resourceId));
throw new Error(rd.errorMessage);
} else if (hasValue(rd.payload) && isNotEmpty(rd.payload.dataDefinition)) {
this.store.dispatch(new CommitPatchOperationsAction(resourceType, resourceId));
return rd.payload.dataDefinition;
}
}),
distinctUntilChanged()
)
}))
);
}