2019-09-25 18:02:15 +09:00

131 lines
3.0 KiB
TypeScript

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map, takeWhile, timeout, take } from 'rxjs/operators';
import { ProtocolService } from '@ucap-webmessenger/protocol';
import {
InfoRequest,
InfoResponse,
encodeInfo,
decodeInfo,
decodeInfoData,
InfoData
} from '../models/info';
import {
SVC_TYPE_EVENT,
SSVC_TYPE_EVENT_INFO_REQ,
SSVC_TYPE_EVENT_INFO_RES,
SSVC_TYPE_EVENT_INFO_DATA,
SSVC_TYPE_EVENT_SEND_REQ,
SSVC_TYPE_EVENT_PUSH_CL_REQ,
SSVC_TYPE_EVENT_READ_REQ,
SSVC_TYPE_EVENT_DEL_REQ,
SSVC_TYPE_EVENT_CANCEL_REQ
} from '../types/service';
import {
SendRequest,
SendResponse,
decodeSend,
encodeSend
} from '../models/send';
import { PushRequest, encodePush } from '../models/push';
import {
ReadResponse,
ReadRequest,
encodeRead,
decodeRead
} from '../models/read';
import {
DelRequest,
DelResponse,
encodeDel,
decodeDel
} from '@ucap-webmessenger/protocol-buddy';
import {
CancelRequest,
CancelResponse,
encodeCancel,
decodeCancel
} from '../models/cancel';
@Injectable({
providedIn: 'root'
})
export class EventProtocolService {
constructor(private protocolService: ProtocolService) {}
public info(req: InfoRequest): Observable<InfoResponse | InfoData> {
return this.protocolService
.call(SVC_TYPE_EVENT, SSVC_TYPE_EVENT_INFO_REQ, ...encodeInfo(req))
.pipe(
takeWhile(res => SSVC_TYPE_EVENT_INFO_RES !== res.subServiceType, true),
map(res => {
if (SSVC_TYPE_EVENT_INFO_DATA === res.subServiceType) {
return {
...decodeInfoData(res),
Type: SSVC_TYPE_EVENT_INFO_DATA
};
}
return {
...decodeInfo(res),
Type: SSVC_TYPE_EVENT_INFO_RES
};
})
);
}
public send(req: SendRequest): Observable<SendResponse> {
return this.protocolService
.call(SVC_TYPE_EVENT, SSVC_TYPE_EVENT_SEND_REQ, ...encodeSend(req))
.pipe(
take(1),
map(res => {
return decodeSend(res);
})
);
}
public push(req: PushRequest): void {
return this.protocolService.send(
SVC_TYPE_EVENT,
SSVC_TYPE_EVENT_PUSH_CL_REQ,
...encodePush(req)
);
}
public read(req: ReadRequest): Observable<ReadResponse> {
return this.protocolService
.call(SVC_TYPE_EVENT, SSVC_TYPE_EVENT_READ_REQ, ...encodeRead(req))
.pipe(
take(1),
map(res => {
return decodeRead(res);
})
);
}
public del(req: DelRequest): Observable<DelResponse> {
return this.protocolService
.call(SVC_TYPE_EVENT, SSVC_TYPE_EVENT_DEL_REQ, ...encodeDel(req))
.pipe(
take(1),
map(res => {
return decodeDel(res);
})
);
}
public cancel(req: CancelRequest): Observable<CancelResponse> {
return this.protocolService
.call(SVC_TYPE_EVENT, SSVC_TYPE_EVENT_CANCEL_REQ, ...encodeCancel(req))
.pipe(
take(1),
map(res => {
return decodeCancel(res);
})
);
}
}