@lzpenguin/server 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +12 -7
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -178,14 +178,16 @@ export class RiffleServer {
|
|
|
178
178
|
}
|
|
179
179
|
|
|
180
180
|
// 合并服务器数据与当前缓存,避免丢失乐观更新的数据
|
|
181
|
-
//
|
|
181
|
+
// 策略:对于 world 数据,优先保留乐观更新(当前缓存),然后合并服务器数据
|
|
182
|
+
// 这样可以确保刚画的笔画不会因为服务器推送旧数据而丢失
|
|
182
183
|
if (this.currentData) {
|
|
183
|
-
// 对于 self 数据:优先使用服务器数据(因为服务器会合并所有更新)
|
|
184
|
-
// 但对于 world 数据:需要合并,因为可能有其他玩家的更新
|
|
185
184
|
const mergedData = {
|
|
186
|
-
// world
|
|
187
|
-
|
|
188
|
-
//
|
|
185
|
+
// world 数据:先使用当前缓存(包含乐观更新),再合并服务器数据
|
|
186
|
+
// _deepMerge 会先保留 target(当前缓存)的所有数据,然后添加 source(服务器数据)中不存在的项
|
|
187
|
+
// 对于数组(如 strokes),会合并去重,确保不会丢失任何笔画
|
|
188
|
+
world: this._deepMerge(this.currentData.world || {}, message.world || {}),
|
|
189
|
+
// self 数据:服务器数据优先(因为服务器会合并所有玩家的更新)
|
|
190
|
+
// 但也要合并当前缓存,确保不丢失字段
|
|
189
191
|
self: {
|
|
190
192
|
public: this._deepMerge(
|
|
191
193
|
message.self?.public || {},
|
|
@@ -271,7 +273,7 @@ export class RiffleServer {
|
|
|
271
273
|
/**
|
|
272
274
|
* 深度合并对象
|
|
273
275
|
* @private
|
|
274
|
-
* @param {Object} target -
|
|
276
|
+
* @param {Object} target - 目标对象(基础数据)
|
|
275
277
|
* @param {Object} source - 源对象(新数据,优先使用)
|
|
276
278
|
* @returns {Object} 合并后的对象
|
|
277
279
|
*/
|
|
@@ -280,6 +282,8 @@ export class RiffleServer {
|
|
|
280
282
|
if (!source) return target;
|
|
281
283
|
|
|
282
284
|
// 处理数组:如果都是数组,合并去重(基于 JSON 字符串比较)
|
|
285
|
+
// 先保留 target 的所有项,然后添加 source 中不存在的项
|
|
286
|
+
// 这样可以确保不会丢失任何数据
|
|
283
287
|
if (Array.isArray(target) && Array.isArray(source)) {
|
|
284
288
|
const merged = [...target];
|
|
285
289
|
const targetStrings = new Set(target.map(item => JSON.stringify(item)));
|
|
@@ -299,6 +303,7 @@ export class RiffleServer {
|
|
|
299
303
|
}
|
|
300
304
|
|
|
301
305
|
// 处理对象:递归合并
|
|
306
|
+
// 先复制 target,然后用 source 的值覆盖(但递归合并子对象)
|
|
302
307
|
const result = { ...target };
|
|
303
308
|
for (const key in source) {
|
|
304
309
|
if (source.hasOwnProperty(key)) {
|