clear repositories and extra DS clear on logout

This commit is contained in:
FinnStutzenstein 2019-02-28 11:04:19 +01:00
parent 9e2d42f9f4
commit 3432c66d4d
3 changed files with 32 additions and 5 deletions

View File

@ -6,6 +6,7 @@ import { environment } from 'environments/environment';
import { User } from '../../shared/models/users/user';
import { OpenSlidesService } from './openslides.service';
import { HttpService } from './http.service';
import { DataStoreService } from './data-store.service';
/**
* The data returned by a post request to the login route.
@ -34,7 +35,8 @@ export class AuthService {
private http: HttpService,
private operator: OperatorService,
private OpenSlides: OpenSlidesService,
private router: Router
private router: Router,
private DS: DataStoreService
) {}
/**
@ -71,6 +73,8 @@ export class AuthService {
} catch (e) {
// We do nothing on failures. Reboot OpenSlides anyway.
}
// Clear the DataStore
this.DS.clear();
this.router.navigate(['/']);
this.OpenSlides.reboot();
}

View File

@ -1,4 +1,4 @@
import { Injectable } from '@angular/core';
import { Injectable, EventEmitter } from '@angular/core';
import { Observable, Subject } from 'rxjs';
@ -123,6 +123,20 @@ export class DataStoreService {
return this.changedOrDeletedSubject.asObservable();
}
/**
* Observable subject for changed or deleted models in the datastore.
*/
private readonly clearEvent: EventEmitter<void> = new EventEmitter<void>();
/**
* Observe the datastore for changes and deletions.
*
* @return an observable for changed and deleted objects.
*/
public get clearObservable(): Observable<void> {
return this.clearEvent.asObservable();
}
/**
* The maximal change id from this DataStore.
*/
@ -171,9 +185,7 @@ export class DataStoreService {
});
});
} else {
this.jsonStore = {};
this.modelStore = {};
this._maxChangeId = 0;
await this.clear();
}
return this.maxChangeId;
}
@ -207,6 +219,7 @@ export class DataStoreService {
this._maxChangeId = 0;
await this.storageService.remove(DataStoreService.cachePrefix + 'DS');
await this.storageService.remove(DataStoreService.cachePrefix + 'maxChangeId');
this.clearEvent.next();
}
/**

View File

@ -68,6 +68,8 @@ export abstract class BaseRepository<V extends BaseViewModel, M extends BaseMode
}
public onAfterAppsLoaded(): void {
this.DS.clearObservable.subscribe(() => this.clear());
// Populate the local viewModelStore with ViewModel Objects.
this.DS.getAll(this.baseModelCtor).forEach((model: M) => {
this.viewModelStore[model.id] = this.createViewModel(model);
@ -152,6 +154,14 @@ export abstract class BaseRepository<V extends BaseViewModel, M extends BaseMode
*/
protected abstract createViewModel(model: M): V;
/**
* Clears the repository.
*/
protected clear(): void {
this.viewModelStore = {};
this.updateViewModelListObservable();
}
/**
* helper function to return one viewModel
*/