mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-03-01 16:20:25 +00:00
Cache object properties to avoid extra RPC
Serialize non-function properties of server-side objects as cachedProps so simple property reads don't require additional RPCs. Added cachedProps to SerializedValue, have RpcServer.storeObjectRef serialize and attach cachedProps (skipping functions), updated serializer to deserialize cachedProps and pass them to proxyCreator, and updated client proxy creation to accept cachedProps and return cached top-level properties directly. Tests updated to expect direct property access for serialized/simple objects and arrays.
This commit is contained in:
@@ -155,15 +155,36 @@ export class RpcServer {
|
||||
|
||||
/**
|
||||
* 存储对象引用并返回序列化的引用
|
||||
* 同时序列化可序列化的属性值,避免属性访问需要额外 RPC
|
||||
*/
|
||||
private storeObjectRef (value: unknown): SerializedValue {
|
||||
const refId = generateRefId();
|
||||
this.objectRefs.set(refId, value);
|
||||
const className = value?.constructor?.name;
|
||||
|
||||
// 序列化非函数属性
|
||||
const cachedProps: Record<string, SerializedValue> = {};
|
||||
if (value && typeof value === 'object') {
|
||||
for (const key of Object.keys(value)) {
|
||||
const propValue = (value as Record<string, unknown>)[key];
|
||||
// 跳过函数(方法需要远程调用)
|
||||
if (typeof propValue === 'function') {
|
||||
continue;
|
||||
}
|
||||
// 序列化属性值
|
||||
try {
|
||||
cachedProps[key] = serialize(propValue, { callbackRegistry: this.localCallbacks });
|
||||
} catch {
|
||||
// 序列化失败的属性跳过,让客户端通过 RPC 获取
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
type: SerializedValueType.OBJECT_REF,
|
||||
refId,
|
||||
className: className !== 'Object' ? className : undefined,
|
||||
cachedProps: Object.keys(cachedProps).length > 0 ? cachedProps : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user