Singleton DS, inject things into it.
This commit is contained in:
parent
36cfb41ff7
commit
95564f9a74
6
client/src/app/core/rootInjector.ts
Normal file
6
client/src/app/core/rootInjector.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import { Injector } from '@angular/core';
|
||||
|
||||
export class RootInjector {
|
||||
constructor() {}
|
||||
public static injector: Injector;
|
||||
}
|
15
client/src/app/core/services/cache.service.spec.ts
Normal file
15
client/src/app/core/services/cache.service.spec.ts
Normal 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();
|
||||
}));
|
||||
});
|
21
client/src/app/core/services/cache.service.ts
Normal file
21
client/src/app/core/services/cache.service.ts
Normal 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');
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -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]
|
||||
}
|
||||
return this.dataStore;
|
||||
],
|
||||
RootInjector.injector
|
||||
);
|
||||
OpenSlidesComponent._DS = injector.get(DataStoreService);
|
||||
}
|
||||
return OpenSlidesComponent._DS;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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));
|
||||
|
Loading…
Reference in New Issue
Block a user