Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean entity service #2551

Merged
merged 5 commits into from
Jun 29, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Clean up the code
  • Loading branch information
KlapTrap committed Jun 27, 2018
commit b6f816ec8871636656ce93cedcf27a9500a8715c
36 changes: 21 additions & 15 deletions src/frontend/app/core/entity-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
import { Store, compose } from '@ngrx/store';
import { tag } from 'rxjs-spy/operators/tag';
import { interval, Observable, combineLatest } from 'rxjs';
import { filter, map, publishReplay, refCount, share, tap, withLatestFrom, switchMap } from 'rxjs/operators';
import { filter, map, publishReplay, refCount, share, tap, withLatestFrom, switchMap, first } from 'rxjs/operators';

import { EntityMonitor } from '../shared/monitors/entity-monitor';
import { ValidateEntitiesStart } from '../store/actions/request.actions';
Expand Down Expand Up @@ -110,30 +110,36 @@ export class EntityService<T = any> {
waitForEntity$: Observable<EntityInfo<T>>;

updatingSection$: Observable<UpdatingSection>;

private getEntityObservable = (
entityMonitor: EntityMonitor<T>,
actionDispatch: Function
): Observable<EntityInfo> => {
const cleanEntityInfo$ = this.getCleanEntityInfoObs(entityMonitor);
return entityMonitor.entityRequest$.pipe(
withLatestFrom(entityMonitor.entity$),
tap(([entityRequestInfo, entity]) => {
map(([entityRequestInfo, entity]) => {
if (actionDispatch && this.shouldCallAction(entityRequestInfo, entity)) {
actionDispatch();
return true;
}
}),
switchMap(() => combineLatest(
entityMonitor.entity$,
entityMonitor.entityRequest$
).pipe(
filter((entityRequestInfo) => {
return !!entityRequestInfo;
}),
map(([entity, entityRequestInfo]) => ({
entityRequestInfo,
entity
}))
))
first(),
switchMap(() => cleanEntityInfo$)
);
}

private getCleanEntityInfoObs(entityMonitor: EntityMonitor<T>) {
return combineLatest(
entityMonitor.entity$,
entityMonitor.entityRequest$
).pipe(
filter((entityRequestInfo) => {
return !!entityRequestInfo;
}),
map(([entity, entityRequestInfo]) => ({
entityRequestInfo,
entity
}))
);
}

Expand Down