UNPKG

commonui-lib-test

Version:

"#common ui lib test"

183 lines (182 loc) 7.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_es_1 = require("lodash-es"); const protocolHandler_1 = require("./protocolHandler"); const operators_1 = require("rxjs/operators"); const rxjs_1 = require("rxjs"); class BasicMsgHandler { constructor() { this.protoRoot = {}; this.enumActionKey = ""; this.serverToClientKey = ""; this.clientToServerKey = ""; this.socketHandler = null; this.responseAction = {}; this.resultAction = {}; this.updateAction = {}; this.notifyAction = {}; } encodeData(protocolName, data = null) { let protocolNo = this.protoRoot.lookup(this.enumActionKey).values[protocolName]; let Request = this.protoRoot.lookup(this.clientToServerKey); let stringfyData = { jsonCommand: JSON.stringify(data) }; let sendObj = Request.encode(stringfyData).finish(); let PB = new protocolHandler_1.ProtocolBuilder(); let msg = new protocolHandler_1.MemoryStream([]); PB.Encode_FromByte(msg, 1); PB.Encode_FromShort(msg, protocolNo); PB.Encode_FromBinary(msg, sendObj); let msgData = msg.getData(); PB = msg = null; return msgData; } decodeData(data) { let PB = new protocolHandler_1.ProtocolBuilder(); let msg = new protocolHandler_1.MemoryStream(data); let header = PB.Decode_ToByte(msg); let protocolNo = PB.Decode_ToShort(msg); let msgData = msg.getData().slice(msg.getPosition(), msg.getLength()); PB = msg = null; let obj = { header, protocolNo, data: msgData }; return obj; } createSubject(subject) { let target = {}; target.subject = subject || new rxjs_1.Subject(); target.next = evt => target.subject.next(evt); target.subscribe = (onNext) => { return target.subject.subscribe(evt => this.onMessageNext(evt, onNext), this.onMessageError); }; return target; } createResettable(factory) { const resetter = new rxjs_1.Subject(); const source = new rxjs_1.Subject(); let destination = factory(); let subscription = source.subscribe(destination); let observable = resetter.asObservable().pipe(operators_1.startWith(null), operators_1.switchMap(() => destination)); let reset = () => { subscription.unsubscribe(); destination = factory(); subscription = source.subscribe(destination); resetter.next(); }; let subject = source; return { observable, reset, subject }; } initResponseAction(enumActionKey, filterKey = "") { let enumAction = this.protoRoot.lookup(enumActionKey).values; lodash_es_1.keys(enumAction) .filter((key) => key != filterKey) .forEach((key) => { this.responseAction[key] = this.createSubject(new rxjs_1.Subject()); this.responseAction[key].protocolNo = enumAction[key]; this.responseAction[key].protocolName = key; this.responseAction[key].send = (data) => { this.socketHandler.send(this.encodeData(key, data)); }; }); } initResultAction(resultKey, filterKey = "") { let resultInfo = this.protoRoot.lookup(resultKey).fields; lodash_es_1.keys(resultInfo) .filter((key) => key != filterKey) .forEach((key) => { this.resultAction[key] = this.createSubject(new rxjs_1.BehaviorSubject(null)); this.resultAction[key].typeName = key; this.resultAction[key].type = resultInfo[key].type; this.resultAction[key].id = resultInfo[key].id; }); } initUpdateAction(updateKey, filterKey = "") { let updateInfo = this.protoRoot.lookup(updateKey).fields; lodash_es_1.keys(updateInfo) .filter((key) => key != filterKey) .forEach((key) => { this.updateAction[key] = this.createSubject(new rxjs_1.BehaviorSubject(null)); this.updateAction[key].typeName = key; this.updateAction[key].type = updateInfo[key].type; this.updateAction[key].id = updateInfo[key].id; }); } initNotifyAction(enumNotifyKey) { let enumNotify = this.protoRoot.lookup(enumNotifyKey).values; lodash_es_1.keys(enumNotify) .forEach(key => { this.notifyAction[key] = this.createSubject(new rxjs_1.Subject()); this.notifyAction[key].notifyName = key; this.notifyAction[key].notifyType = enumNotify[key]; }); } onMessageNext(evt, onMessageCB = null) { if (evt === undefined || evt === null) return; if (onMessageCB) onMessageCB(evt); } onMessageError(evt) { console.error("OGOC - GS onMessageError : ", evt); } reciveServerToClient(evt) { let protocolNo = evt.protocolNo; let enumAction = this.protoRoot.lookup(this.enumActionKey).values; let protocolName = lodash_es_1.keys(enumAction).find(key => enumAction[key] === protocolNo); let response = this.protoRoot.lookup(this.serverToClientKey).decode(evt.data); response.ctime = lodash_es_1.now(); // console.group("reciveServerToClient"); // console.log("protocolNo : ", protocolNo); // console.log("protocolName : ", protocolName); // console.log("response : ", response); // console.groupEnd(); this.responseAction[protocolName].next(response); if (lodash_es_1.has(response, "resultInfo")) this.reciveResultInfo(response); if (lodash_es_1.has(response, "updateList")) this.reciveUpdateList(response); if (lodash_es_1.has(response, "notifyList")) this.reciveNotifyList(response); } reciveResultInfo(evt) { let { resultInfo } = evt; if (!resultInfo) return; let infoKeys = lodash_es_1.keys(resultInfo); for (let i = 0; i < infoKeys.length; i++) { let key = infoKeys[i]; let data = resultInfo[key]; data.ctime = lodash_es_1.now(); let action = lodash_es_1.find(this.resultAction, action => action.typeName === key); if (action) action.next(data); } } reciveUpdateList(evt) { let { updateList } = evt; if (!updateList || updateList.length === 0) return; for (let i = 0; i < updateList.length; i++) { let updateInfo = updateList[i]; let { updateOneof } = updateInfo; let data = updateInfo[updateOneof]; data.ctime = lodash_es_1.now(); let action = lodash_es_1.find(this.updateAction, action => action.typeName === updateOneof); if (action) action.next(data); } } reciveNotifyList(evt) { let { notifyList } = evt; if (!notifyList || notifyList.length == 0) return; for (let i = 0; i < notifyList.length; i++) { let notifyInfo = notifyList[i]; let { notifyType } = notifyInfo; let action = lodash_es_1.find(this.notifyAction, action => action.notifyType === notifyType); if (action) action.next(evt); } } initHandler(protoRoot, socketHandler) { } } exports.default = BasicMsgHandler;