fix: 將 startLockStatusPolling 改成特務模式(State Diffing), 解決樹狀圖瀏覽延遲。

This commit is contained in:
swpa 2026-05-28 10:51:04 +08:00
parent 77821fc620
commit e00642553f
1 changed files with 40 additions and 16 deletions

View File

@ -102,6 +102,8 @@ window.addEventListener('beforeunload', () => {
}
});
// 🌟 特務的記憶體:記錄「上一次輪詢時,處於鎖定狀態的路徑」
let activeLockState = new Set();
let lockPollingTimer = null;
function startLockStatusPolling() {
@ -114,24 +116,46 @@ function startLockStatusPolling() {
try {
const result = await apiGetLockStatus(connInfo.host);
if (result.status === 'success') {
const activeLocks = result.data;
const currentLocks = result.data;
const newLockState = new Set();
document.querySelectorAll('.edit-btn').forEach(btn => {
const path = btn.getAttribute('data-path');
if (activeLocks[path] && activeLocks[path].user_id !== SESSION_USER_ID) {
btn.style.pointerEvents = 'none';
btn.style.opacity = '0.2';
btn.title = `🔒 此區塊正由 [${activeLocks[path].username}] 編輯中`;
btn.innerText = "🔒";
} else {
if (btn.innerText === "🔒") {
btn.style.pointerEvents = 'auto';
btn.style.opacity = '0.3';
btn.title = "鎖定並編輯此項目";
btn.innerText = "✏️";
}
// 🎯 任務 1處理「新增鎖定」或「持續鎖定」的節點
for (const [path, lockInfo] of Object.entries(currentLocks)) {
if (lockInfo.user_id !== SESSION_USER_ID) {
newLockState.add(path);
const safePath = path.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
const targetBtns = document.querySelectorAll(`.edit-btn[data-path="${safePath}"]`);
targetBtns.forEach(btn => {
if (btn.innerText !== "🔒") {
btn.style.pointerEvents = 'none';
btn.style.opacity = '0.2';
btn.title = `🔒 此區塊正由 [${lockInfo.username}] 編輯中`;
btn.innerText = "🔒";
}
});
}
}
// 🔓 任務 2處理「解除鎖定」的節點
activeLockState.forEach(oldPath => {
if (!newLockState.has(oldPath)) {
const safePath = oldPath.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
const targetBtns = document.querySelectorAll(`.edit-btn[data-path="${safePath}"]`);
targetBtns.forEach(btn => {
if (btn.innerText === "🔒") {
btn.style.pointerEvents = 'auto';
btn.style.opacity = '0.3';
btn.title = "鎖定並編輯此項目";
btn.innerText = "✏️";
}
});
}
});
// 💾 任務 3更新特務的記憶
activeLockState = newLockState;
}
} catch (error) {
console.warn("獲取鎖定狀態失敗:", error);