Fix enums in DynamicService.ts
This commit is contained in:
1 parent
a10a49cf4f
commit
c198965a49
1 file changed
+16
-10
@@ -18,6 +18,11 @@ interface IService {
|
||||
qualifier : string
|
||||
}
|
||||
|
||||
function decode_buffer(decoder : any, buffer : Uint8Array) {
|
||||
const temp = decoder.decode(buffer);
|
||||
return fillDefaults(decoder.name, decoder.toObject(temp));
|
||||
}
|
||||
|
||||
function gen_method(service:IService, method_id : number, request_type : string, output_type : string) {
|
||||
const request_proto = root.lookupType(request_type)
|
||||
const result_proto = root.lookupType(output_type)
|
||||
@@ -30,7 +35,8 @@ function gen_method(service:IService, method_id : number, request_type : string,
|
||||
// There is a built in library function do do this but it doesn't work recursively
|
||||
// https://github.com/dcodeIO/protobuf.js/issues/1041
|
||||
// result_proto.toObject(temp, { enums: String, default: true })
|
||||
return fillDefaults(result_proto.name, result_proto.toObject(temp))
|
||||
//return fillDefaults(result_proto.name, result_proto.toObject(temp))
|
||||
return decode_buffer(result_proto, result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +52,7 @@ function gen_subscribe(service:IService, method_id : number, methodName : string
|
||||
const key = value.key;
|
||||
switch(value.updateType) {
|
||||
case RawUpdateType.CREATE: {
|
||||
var obj = resultProto.decode(value.buffer)
|
||||
var obj = decode_buffer(resultProto, value.buffer) // resultProto.decode(value.buffer)
|
||||
state[key] = obj
|
||||
result.added[key] = obj
|
||||
}
|
||||
@@ -59,7 +65,7 @@ function gen_subscribe(service:IService, method_id : number, methodName : string
|
||||
break;
|
||||
case RawUpdateType.REPLACE: {
|
||||
var oldItem = state[key]
|
||||
var newItem = resultProto.decode(value.buffer);
|
||||
var newItem = decode_buffer(resultProto, value.buffer)//resultProto.decode(value.buffer);
|
||||
state[key] = newItem;
|
||||
result.changed[key] = { new : newItem, old : oldItem };
|
||||
}
|
||||
@@ -73,7 +79,7 @@ function gen_subscribe(service:IService, method_id : number, methodName : string
|
||||
result.all = { ...state };
|
||||
callback(result);
|
||||
});
|
||||
return {
|
||||
return {
|
||||
Dispose : () => {
|
||||
service.context.unsubscribe(id, service.name, service.qualifier);
|
||||
}
|
||||
@@ -83,8 +89,8 @@ function gen_subscribe(service:IService, method_id : number, methodName : string
|
||||
|
||||
export class DynamicService {
|
||||
constructor(
|
||||
context : ServiceContext,
|
||||
name : string, qualifier:string,
|
||||
context : ServiceContext,
|
||||
name : string, qualifier:string,
|
||||
methods : Array<[string,number,string,string]>,
|
||||
streams : Array<[string,number,string]>) {
|
||||
|
||||
@@ -100,7 +106,7 @@ export class DynamicService {
|
||||
}
|
||||
|
||||
export function fillDefaults(ptype:string, value:any) {
|
||||
// This function could be replaced with toObject(value,
|
||||
// This function could be replaced with toObject(value,
|
||||
switch(ptype) {
|
||||
case "double":
|
||||
case "float":
|
||||
@@ -116,7 +122,7 @@ export function fillDefaults(ptype:string, value:any) {
|
||||
case "string": return value||""
|
||||
default:
|
||||
const type = root.lookupTypeOrEnum(ptype)
|
||||
if (isEnum(type)) {
|
||||
if (isEnum(type)) {
|
||||
const intval = value||0;
|
||||
return type.valuesById[intval]
|
||||
}
|
||||
@@ -136,8 +142,8 @@ export function fillDefaults(ptype:string, value:any) {
|
||||
}
|
||||
}
|
||||
|
||||
function isEnum(mtype:any) { return mtype && ('valuesById' in mtype); }
|
||||
function isEnum(mtype:any) { return mtype && ('valuesById' in mtype); }
|
||||
|
||||
export function merge<T>(a:T, b:object) : T {
|
||||
return { ... (a as any), ... b }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user