Use reactive fassade for wp cache service

pull/4769/head
Roman Roelofsen 8 years ago
parent 7728c3cc88
commit a7e5858631
  1. 26
      frontend/app/components/work-packages/work-package-cache.service.ts
  2. 3
      frontend/app/components/wp-edit/wp-edit-form.directive.ts
  3. 4
      frontend/app/helpers/reactive-fassade.ts
  4. 6
      frontend/app/states.ts

@ -31,20 +31,16 @@ import {opWorkPackagesModule} from "../../angular-modules";
import {WorkPackageResource} from "../api/api-v3/hal-resources/work-package-resource.service"; import {WorkPackageResource} from "../api/api-v3/hal-resources/work-package-resource.service";
import {ApiWorkPackagesService} from "../api/api-work-packages/api-work-packages.service"; import {ApiWorkPackagesService} from "../api/api-work-packages/api-work-packages.service";
import {states} from "../../states"; import {states} from "../../states";
import IScope = angular.IScope;
import {State} from "../../helpers/reactive-fassade"; import {State} from "../../helpers/reactive-fassade";
import IScope = angular.IScope;
export class WorkPackageCacheService { export class WorkPackageCacheService {
// private workPackageCache: {[id: number]: WorkPackageResource} = {};
// private workPackagesSubject = new Rx.ReplaySubject<{[id: number]: WorkPackageResource}>(1);
private newWorkPackageCreatedSubject = new Rx.Subject<WorkPackageResource>(); private newWorkPackageCreatedSubject = new Rx.Subject<WorkPackageResource>();
/*@ngInject*/ /*@ngInject*/
constructor(private $rootScope: IScope, private apiWorkPackages: ApiWorkPackagesService) { constructor(private apiWorkPackages: ApiWorkPackagesService) {
} }
newWorkPackageCreated(wp: WorkPackageResource) { newWorkPackageCreated(wp: WorkPackageResource) {
@ -52,7 +48,6 @@ export class WorkPackageCacheService {
} }
updateWorkPackage(wp: WorkPackageResource) { updateWorkPackage(wp: WorkPackageResource) {
// this.updateWorkPackageList([wp]);
states.workPackages.put(wp.id.toString(), wp); states.workPackages.put(wp.id.toString(), wp);
} }
@ -65,37 +60,22 @@ export class WorkPackageCacheService {
// } else { // } else {
// this.workPackageCache[wp.id] = wp; // this.workPackageCache[wp.id] = wp;
// } // }
// TODO Roman: clarify with Jens/Oliver. Is this check still ok? // TODO Roman: clarify with Jens/Oliver. Is this check still ok?
if (!wp.dirty) { if (!wp.dirty) {
states.workPackages.put(wp.id.toString(), wp); states.workPackages.put(wp.id.toString(), wp);
} }
} }
// this.workPackagesSubject.onNext(this.workPackageCache);
} }
loadWorkPackage(workPackageId: number, forceUpdate = false): State<WorkPackageResource> { loadWorkPackage(workPackageId: number, forceUpdate = false): State<WorkPackageResource> {
// if (forceUpdate || this.workPackageCache[workPackageId] === undefined) {
// this.apiWorkPackages.loadWorkPackageById(workPackageId, forceUpdate).then(wp => {
// this.updateWorkPackage(wp);
// });
// }
//
// return this.workPackagesSubject
// .map(cache => cache[workPackageId])
// .filter(wp => wp !== undefined);
const state = states.workPackages.get(workPackageId.toString()); const state = states.workPackages.get(workPackageId.toString());
if (forceUpdate) { if (forceUpdate) {
state.clear(); state.clear();
// this.apiWorkPackages.loadWorkPackageById(workPackageId, forceUpdate).then(wp => {
// state.put(wp);
// });
} }
state.putFromPromiseIfPristine( state.putFromPromiseIfPristine(
this.apiWorkPackages.loadWorkPackageById(workPackageId, forceUpdate)); () => this.apiWorkPackages.loadWorkPackageById(workPackageId, forceUpdate));
return state; return state;
} }

@ -32,6 +32,7 @@ import {WorkPackageEditFieldController} from './wp-edit-field/wp-edit-field.dire
import {WorkPackageCacheService} from '../work-packages/work-package-cache.service'; import {WorkPackageCacheService} from '../work-packages/work-package-cache.service';
import {scopedObservable} from '../../helpers/angular-rx-utils'; import {scopedObservable} from '../../helpers/angular-rx-utils';
import {WorkPackageResource} from '../api/api-v3/hal-resources/work-package-resource.service'; import {WorkPackageResource} from '../api/api-v3/hal-resources/work-package-resource.service';
import {states} from "../../states";
export class WorkPackageEditFormController { export class WorkPackageEditFormController {
public workPackage; public workPackage;
@ -56,7 +57,7 @@ export class WorkPackageEditFormController {
wpEditModeState.register(this); wpEditModeState.register(this);
} }
scopedObservable($scope, wpCacheService.loadWorkPackage(this.workPackage.id)) states.workPackages.get(this.workPackage.id.toString()).observe($scope)
.subscribe((wp: WorkPackageResource) => { .subscribe((wp: WorkPackageResource) => {
this.workPackage = wp; this.workPackage = wp;
}); });

@ -82,9 +82,9 @@ export class State<T> extends StoreElement {
return this; return this;
} }
public putFromPromiseIfPristine(promise: PromiseLike<T>): this { public putFromPromiseIfPristine(calledIfPristine: () => PromiseLike<T>): this {
if (this.isPristine()) { if (this.isPristine()) {
this.putFromPromise(promise); this.putFromPromise(calledIfPristine());
} }
return this; return this;
} }

@ -2,11 +2,13 @@ import {MultiState, initStates} from "./helpers/reactive-fassade";
import {WorkPackageResource} from "./components/api/api-v3/hal-resources/work-package-resource.service"; import {WorkPackageResource} from "./components/api/api-v3/hal-resources/work-package-resource.service";
export const states = { export const states = {
workPackages: new MultiState<WorkPackageResource>() workPackages: new MultiState<WorkPackageResource>()
}; };
initStates(states, (msg: any) => console.trace(msg)); initStates(states, function (msg: any) {
// RR: stupid hack to avoid compiler error
(console.trace as any)(msg);
});

Loading…
Cancel
Save