diff --git a/client/src/app/core/rootInjector.ts b/client/src/app/core/rootInjector.ts new file mode 100644 index 000000000..56b1af684 --- /dev/null +++ b/client/src/app/core/rootInjector.ts @@ -0,0 +1,6 @@ +import { Injector } from '@angular/core'; + +export class RootInjector { + constructor() {} + public static injector: Injector; +} diff --git a/client/src/app/core/services/cache.service.spec.ts b/client/src/app/core/services/cache.service.spec.ts new file mode 100644 index 000000000..2c03bdba4 --- /dev/null +++ b/client/src/app/core/services/cache.service.spec.ts @@ -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(); + })); +}); diff --git a/client/src/app/core/services/cache.service.ts b/client/src/app/core/services/cache.service.ts new file mode 100644 index 000000000..8da4020c9 --- /dev/null +++ b/client/src/app/core/services/cache.service.ts @@ -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'); + } +} diff --git a/client/src/app/core/services/data-store.service.ts b/client/src/app/core/services/data-store.service.ts index fb98490c8..5eac23918 100644 --- a/client/src/app/core/services/data-store.service.ts +++ b/client/src/app/core/services/data-store.service.ts @@ -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 diff --git a/client/src/app/openslides.component.ts b/client/src/app/openslides.component.ts index c78939e0e..cb726d1f8 100644 --- a/client/src/app/openslides.component.ts +++ b/client/src/app/openslides.component.ts @@ -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; } /** diff --git a/client/src/main.ts b/client/src/main.ts index 673719c2d..d53cc2d87 100644 --- a/client/src/main.ts +++ b/client/src/main.ts @@ -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) => { + RootInjector.injector = moduleRef.injector; + }) .catch(err => console.log(err));