2018-07-12 14:11:31 +02:00
|
|
|
import { Injector } from '@angular/core';
|
|
|
|
import { Observable, of } from 'rxjs';
|
|
|
|
|
2018-08-22 16:03:49 +02:00
|
|
|
import { DataStoreService } from './core/services/data-store.service';
|
2018-08-24 10:46:48 +02:00
|
|
|
import { CacheService } from './core/services/cache.service';
|
2018-07-12 14:11:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* injects the {@link DataStoreService} to all its children and provides a generic function to catch errors
|
|
|
|
* should be abstract and a mere parent to all {@link DataStoreService} accessors
|
|
|
|
*/
|
|
|
|
export abstract class OpenSlidesComponent {
|
|
|
|
/**
|
|
|
|
* The dataStore Service
|
|
|
|
*/
|
2018-08-24 10:46:48 +02:00
|
|
|
private static _DS: DataStoreService;
|
2018-07-12 14:11:31 +02:00
|
|
|
|
2018-08-28 11:07:10 +02:00
|
|
|
public static injector: Injector;
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* Empty constructor
|
|
|
|
*
|
|
|
|
* Static injection of {@link DataStoreService} in all child instances of OpenSlidesComponent
|
|
|
|
* Throws a warning even tho it is the new syntax. Ignored for now.
|
|
|
|
*/
|
2018-08-29 13:21:25 +02:00
|
|
|
public constructor() {}
|
2018-07-12 14:11:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* getter to access the {@link DataStoreService}
|
|
|
|
* @example this.DS.get(User)
|
|
|
|
* @return access to dataStoreService
|
|
|
|
*/
|
2018-08-29 13:21:25 +02:00
|
|
|
public get DS(): DataStoreService {
|
2018-08-28 11:07:10 +02:00
|
|
|
if (!OpenSlidesComponent.injector) {
|
|
|
|
throw new Error('OpenSlides is not bootstrapping right. This component should have the Injector.');
|
|
|
|
}
|
2018-08-24 10:46:48 +02:00
|
|
|
if (OpenSlidesComponent._DS == null) {
|
2018-08-28 09:33:50 +02:00
|
|
|
const injector = Injector.create({
|
|
|
|
providers: [
|
2018-08-24 10:46:48 +02:00
|
|
|
{
|
|
|
|
provide: DataStoreService,
|
|
|
|
useClass: DataStoreService,
|
|
|
|
deps: [CacheService]
|
|
|
|
}
|
|
|
|
],
|
2018-08-28 11:07:10 +02:00
|
|
|
parent: OpenSlidesComponent.injector
|
2018-08-28 09:33:50 +02:00
|
|
|
});
|
2018-08-24 10:46:48 +02:00
|
|
|
OpenSlidesComponent._DS = injector.get(DataStoreService);
|
2018-07-12 14:11:31 +02:00
|
|
|
}
|
2018-08-24 10:46:48 +02:00
|
|
|
return OpenSlidesComponent._DS;
|
2018-07-12 14:11:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic error handling for everything that makes HTTP Calls
|
|
|
|
* TODO: could have more features
|
|
|
|
* @return an observable error
|
|
|
|
*/
|
2018-08-29 13:21:25 +02:00
|
|
|
public handleError<T>() {
|
2018-07-12 14:11:31 +02:00
|
|
|
return (error: any): Observable<T> => {
|
|
|
|
console.error(error);
|
|
|
|
return of(error);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|