Browse Source

refactor(student): 优化 Y.Doc update 事件的日志逻辑

调整 Y.Doc update 事件的日志处理,只在来自网络的更新时解析并打印具体内容,添加异常捕获避免日志报错,同时移除不必要的本地更新日志输出。
lsc 7 hours ago
parent
commit
a1d93ea2f9
1 changed files with 19 additions and 9 deletions
  1. 19 9
      src/views/Student/index.vue

+ 19 - 9
src/views/Student/index.vue

@@ -4457,17 +4457,27 @@ const createWebSocketConnection = async (type = 1) => {
 
     // ★ 三层日志之二:Y.Doc update 事件 — 列出所有根类型的 size,看更新到底改了哪个
     docSocket.value.on('update', (update: Uint8Array, origin: any) => {
-      const originName = origin?.constructor?.name || (origin === providerSocket.value ? 'ws-provider' : typeof origin)
-      const share = (docSocket.value as any)?.share
-      const sizes: string[] = []
-      if (share) {
-        for (const [name, type] of share.entries()) {
-          const sz = type.size ?? type.length ?? '?'
-          const latest = type.get?.('latest')
-          sizes.push(`${name}=${sz}${latest ? '(' + latest.type + ')' : ''}`)
+      // origin === providerSocket.value 表示来自网络
+      // origin !== providerSocket.value 表示本地修改
+      if (origin !== providerSocket.value) return
+
+      try {
+        const { structs } = Y.decodeUpdate(update)
+        for (const struct of structs) {
+          const arr = (struct as any)?.content?.arr
+          if (arr && Array.isArray(arr)) {
+            for (const val of arr) {
+              if (val && typeof val === 'object' && val.type && val.mId) {
+                console.log(`📦 提取: type=${val.type}, mId=${val.mId}`)
+                if (val.mId !== mId.value) getMessages(val)
+              }
+            }
+          }
         }
       }
-      console.log(`📥 Y.Doc update: ${update.length} bytes, origin: ${originName}, types: [${sizes.join(', ')}]`)
+      catch (e) {
+        console.log('📦 解析失败:', e)
+      }
     })
 
     // 清理可能残留的旧心跳定时器