Files
xFrameworkPushServiceHolder/projects/x-framework-push-service/src/lib/base/x-base-web-rtc.service.ts
T

135 lines
3.2 KiB
TypeScript

import { Subject } from 'rxjs';
import {
X_FRAMEWORK_PUSH_SERVICE_CONFIG,
X_FRAMEWORK_PUSH_SERVICE_TOKEN_FETCHER,
} from '../tokens/x-injectable-tokens';
import { isString } from 'x-framework-core';
import { XBasePushService } from './x-base-push.service';
import { Inject, Injectable, Optional } from '@angular/core';
import { XWebRTCAction } from '../typings/x-push-notification.typings';
import { IXPushServiceTokenFetcher } from '../policies/x-push-service-token-fetcher';
import { XFrameworkPushServiceConfig } from '../config/x-framework-push-service.config';
@Injectable()
export abstract class XBaseWebRTCService extends XBasePushService {
//
//#region Props ...
public onOfferRecieved$ = new Subject<any>();
public onAnswerRecieved$ = new Subject<{
answer: any;
requesterId: string;
}>();
public onCandidateRecieved$ = new Subject<{
candidate: any;
senderId: string;
}>();
//#endregion
//
//#region Constructor ...
constructor(
//
@Inject(X_FRAMEWORK_PUSH_SERVICE_CONFIG)
public config: XFrameworkPushServiceConfig,
//
@Optional()
@Inject(X_FRAMEWORK_PUSH_SERVICE_TOKEN_FETCHER)
public tokenFetcher?: IXPushServiceTokenFetcher
) {
super(config, tokenFetcher);
}
//#endregion
//
//#region Actions ...
public async offer(offer: any, to: string) {
//
if (!isString(offer)) {
offer = JSON.stringify(offer);
}
//
await this.invoke(XWebRTCAction.Offer, offer, to);
}
public async answer(answer: any) {
//
if (!isString(answer)) {
answer = JSON.stringify(answer);
}
//
await this.invoke(XWebRTCAction.Answer, answer);
}
public async candidate(candidate: any, to: string) {
//
if (!isString(candidate)) {
candidate = JSON.stringify(candidate);
}
//
await this.invoke(XWebRTCAction.Candidate, candidate, to);
}
//#endregion
/**
* Register All Available
*/
registerSubjects(): void {
super.registerSubjects();
//
this.onOfferRecieved((offer: any) => {
this.onOfferRecieved$.next(offer);
});
//
this.onAnswerRecieved((answer: any, requesterId: string) => {
this.onAnswerRecieved$.next({ answer, requesterId });
});
//
this.onCandidateRecieved((candidate: any, senderId: string) => {
this.onCandidateRecieved$.next({ candidate, senderId });
});
}
//
//#region Event Handler Attaches ...
public onOfferRecieved(callback: (offer: any) => any) {
this.on(XWebRTCAction.Offer, callback);
}
public onAnswerRecieved(callback: (answer: any, requesterId: string) => any) {
this.on(XWebRTCAction.Answer, callback);
}
public onCandidateRecieved(
callback: (candidate: any, senderId: string) => any
) {
this.on(XWebRTCAction.Candidate, callback);
}
//#endregion
//
//#region Event Handler Detachers ...
public offOfferRecieved(callback?: (offer: any) => any) {
this.off(XWebRTCAction.Offer, callback);
}
public offAnswerRecieved(
callback?: (answer: any, requesterId: string) => any
) {
this.off(XWebRTCAction.Answer, callback);
}
public offCandidateRecieved(
callback?: (candidate: any, senderId: string) => any
) {
this.off(XWebRTCAction.Candidate, callback);
}
//#endregion
}