Singleton DS, inject things into it.

This commit is contained in:
FinnStutzenstein 2018-08-24 10:46:48 +02:00
parent 36cfb41ff7
commit 95564f9a74
6 changed files with 68 additions and 15 deletions

View File

@ -0,0 +1,6 @@
import { Injector } from '@angular/core';
export class RootInjector {
constructor() {}
public static injector: Injector;
}

View File

@ -0,0 +1,15 @@
import { TestBed, inject } from '@angular/core/testing';
import { CacheService } from './cache.service';
describe('WebsocketService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [CacheService]
});
});
it('should be created', inject([CacheService], (service: CacheService) => {
expect(service).toBeTruthy();
}));
});

View File

@ -0,0 +1,21 @@
import { Injectable } from '@angular/core';
/**
* Handles all incoming and outgoing notify messages via {@link WebsocketService}.
*/
@Injectable({
providedIn: 'root'
})
export class CacheService {
/**
* Constructor to create the NotifyService. Registers itself to the WebsocketService.
* @param websocketService
*/
constructor() {
console.log('Cache constructor');
}
public test() {
console.log('hi');
}
}

View File

@ -3,6 +3,7 @@ import { Observable, BehaviorSubject } from 'rxjs';
import { ImproperlyConfiguredError } from 'app/core/exceptions';
import { BaseModel, ModelId } from 'app/shared/models/base.model';
import { CacheService } from './cache.service';
/**
* represents a collection on the Django server, uses an ID to access a {@link BaseModel}.
@ -53,7 +54,9 @@ export class DataStoreService {
* Empty constructor for dataStore
* @param http use HttpClient to send models back to the server
*/
constructor() {}
constructor(private cacheService: CacheService) {
cacheService.test();
}
/**
* Read one, multiple or all ID's from dataStore

View File

@ -1,23 +1,19 @@
import { Injector } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { DataStoreService } from './core/services/data-store.service';
import { CacheService } from './core/services/cache.service';
import { RootInjector } from './core/rootInjector';
/**
* 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 {
/**
* To inject the {@link DataStoreService} into the children of OpenSlidesComponent
*/
protected injector: Injector;
/**
* The dataStore Service
*/
protected dataStore: DataStoreService;
private static _DS: DataStoreService;
/**
* Empty constructor
@ -25,9 +21,7 @@ export abstract class OpenSlidesComponent {
* Static injection of {@link DataStoreService} in all child instances of OpenSlidesComponent
* Throws a warning even tho it is the new syntax. Ignored for now.
*/
constructor() {
this.injector = Injector.create([{ provide: DataStoreService, useClass: DataStoreService, deps: [] }]);
}
constructor() {}
/**
* getter to access the {@link DataStoreService}
@ -35,10 +29,20 @@ export abstract class OpenSlidesComponent {
* @return access to dataStoreService
*/
get DS(): DataStoreService {
if (this.dataStore == null) {
this.dataStore = this.injector.get(DataStoreService);
if (OpenSlidesComponent._DS == null) {
const injector = Injector.create(
[
{
provide: DataStoreService,
useClass: DataStoreService,
deps: [CacheService]
}
],
RootInjector.injector
);
OpenSlidesComponent._DS = injector.get(DataStoreService);
}
return this.dataStore;
return OpenSlidesComponent._DS;
}
/**

View File

@ -1,8 +1,9 @@
import { enableProdMode } from '@angular/core';
import { enableProdMode, NgModuleRef } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
import { RootInjector } from 'app/core/rootInjector';
if (environment.production) {
enableProdMode();
@ -10,4 +11,7 @@ if (environment.production) {
platformBrowserDynamic()
.bootstrapModule(AppModule)
.then((moduleRef: NgModuleRef<AppModule>) => {
RootInjector.injector = moduleRef.injector;
})
.catch(err => console.log(err));