2018-08-29 15:49:44 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
|
|
|
import { OpenSlidesComponent } from 'app/openslides.component';
|
|
|
|
import { WebsocketService } from './websocket.service';
|
|
|
|
import { Observable, of, Subject } from 'rxjs';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* constants have a key associated with the data.
|
|
|
|
*/
|
|
|
|
interface Constants {
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get constants from the server.
|
|
|
|
*
|
|
|
|
* @example
|
2018-08-31 12:37:47 +02:00
|
|
|
* ```ts
|
|
|
|
* this.constantsService.get('OpenSlidesSettings').subscribe(constant => {
|
|
|
|
* console.log(constant);
|
|
|
|
* });
|
|
|
|
* ```
|
2018-08-29 15:49:44 +02:00
|
|
|
*/
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class ConstantsService extends OpenSlidesComponent {
|
|
|
|
/**
|
|
|
|
* The constants
|
|
|
|
*/
|
|
|
|
private constants: Constants;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag, if the websocket connection is open.
|
|
|
|
*/
|
|
|
|
private websocketOpen = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag, if constants are requested, but the server hasn't send them yet.
|
|
|
|
*/
|
|
|
|
private pending = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pending requests will be notified by these subjects, one per key.
|
|
|
|
*/
|
|
|
|
private pendingSubject: { [key: string]: Subject<any> } = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param websocketService
|
|
|
|
*/
|
|
|
|
public constructor(private websocketService: WebsocketService) {
|
|
|
|
super();
|
|
|
|
|
|
|
|
// The hook for recieving constants.
|
2018-08-31 12:37:47 +02:00
|
|
|
websocketService.getOberservable<Constants>('constants').subscribe(constants => {
|
2018-08-29 15:49:44 +02:00
|
|
|
this.constants = constants;
|
|
|
|
if (this.pending) {
|
|
|
|
// send constants to subscribers that await constants.
|
|
|
|
this.pending = false;
|
|
|
|
Object.keys(this.pendingSubject).forEach(key => {
|
|
|
|
this.pendingSubject[key].next(this.constants[key]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// We can request constants, if the websocket connection opens.
|
|
|
|
websocketService.connectEvent.subscribe(() => {
|
|
|
|
if (!this.websocketOpen && this.pending) {
|
2018-08-31 12:37:47 +02:00
|
|
|
this.websocketService.send('constants', {});
|
2018-08-29 15:49:44 +02:00
|
|
|
}
|
|
|
|
this.websocketOpen = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the constant named by key.
|
|
|
|
* @param key The constant to get.
|
|
|
|
*/
|
|
|
|
public get(key: string): Observable<any> {
|
|
|
|
if (this.constants) {
|
|
|
|
return of(this.constants[key]);
|
|
|
|
} else {
|
|
|
|
// we have to request constants.
|
|
|
|
if (!this.pending) {
|
|
|
|
this.pending = true;
|
|
|
|
// if the connection is open, we directly can send the request.
|
|
|
|
if (this.websocketOpen) {
|
2018-08-31 12:37:47 +02:00
|
|
|
this.websocketService.send('constants', {});
|
2018-08-29 15:49:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!this.pendingSubject[key]) {
|
|
|
|
this.pendingSubject[key] = new Subject<any>();
|
|
|
|
}
|
|
|
|
return this.pendingSubject[key].asObservable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|