Browse Source

保存草稿

master
修宁 7 months ago
parent
commit
575fbfa664
  1. 63
      src/core/manager/StateManager.ts

63
src/core/manager/StateManager.ts

@ -94,6 +94,9 @@ export default class StateManager {
// 标记是否有未保存的更改 // 标记是否有未保存的更改
private pendingChanges = false private pendingChanges = false
private isAutoSavingPaused = false
private autoSaveTimer: number | null = null
private isUpdating = false
// 变化追踪器 // 变化追踪器
private readonly changeTracker: DataDiff = { private readonly changeTracker: DataDiff = {
@ -120,6 +123,9 @@ export default class StateManager {
// this.historySteps = Array.from({ length: this.maxHistorySteps }, () => null) // this.historySteps = Array.from({ length: this.maxHistorySteps }, () => null)
this.historySteps = [] this.historySteps = []
this.pendingChanges = false
this.isAutoSavingPaused = false
} }
/** /**
@ -130,6 +136,7 @@ export default class StateManager {
this.changeTracker.added.length = 0 this.changeTracker.added.length = 0
this.changeTracker.removed.length = 0 this.changeTracker.removed.length = 0
this.changeTracker.updated.length = 0 this.changeTracker.updated.length = 0
this.isUpdating = true
} }
/** /**
@ -141,6 +148,9 @@ export default class StateManager {
this.syncDataState(this.changeTracker) this.syncDataState(this.changeTracker)
this.isChanged.value = true this.isChanged.value = true
this.pendingChanges = true // 标记有需要保存的更改 this.pendingChanges = true // 标记有需要保存的更改
this.isUpdating = false
this.startAutoSave() // 触发自动保存
} }
// 差异反转方法 // 差异反转方法
@ -268,6 +278,7 @@ export default class StateManager {
this.pendingChanges = true this.pendingChanges = true
system.msg('撤销完成') system.msg('撤销完成')
this.startAutoSave()
} }
/** /**
@ -287,14 +298,53 @@ export default class StateManager {
this.pendingChanges = true this.pendingChanges = true
system.msg('重做完成') system.msg('重做完成')
this.startAutoSave()
}
startAutoSave() {
this.isAutoSavingPaused = false
if (this.autoSaveTimer !== null) {
window.clearTimeout(this.autoSaveTimer)
this.autoSaveTimer = null
}
// 设置新的定时器,在 5 秒后尝试保存
this.queueAutoSave()
} }
stopAutoSave() { stopAutoSave() {
// 停止自动保存逻辑 if (this.autoSaveTimer !== null) {
window.clearTimeout(this.autoSaveTimer)
this.autoSaveTimer = null
}
this.isAutoSavingPaused = true
} }
startAutoSave() { private queueAutoSave() {
// 实现自动保存逻辑, 只要调用过 endStateUpdate(), 固定 5秒后触发 saveToLocalstore 方法, 调用过程不用等回调完成 if (this.autoSaveTimer !== null || this.isAutoSavingPaused) return
if (!this.pendingChanges) return
this.autoSaveTimer = window.setTimeout(async () => {
this.autoSaveTimer = null
if (this.isUpdating) {
const checkInterval = setInterval(() => {
if (!this.isUpdating) {
clearInterval(checkInterval)
if (!this.isAutoSavingPaused) {
this.queueAutoSave()
}
} else {
console.log('wait for entityManager to finish updating...')
}
}, 200)
return
}
await this.saveToLocalstore()
}, 5000)
} }
/** /**
@ -328,13 +378,10 @@ export default class StateManager {
this.pendingChanges = false this.pendingChanges = false
// 强制保存一次初始状态 // 强制保存一次初始状态
await this.saveToLocalstore()
console.log('[StateManager] 加载完成,共', data.items.length, '个对象') console.log('[StateManager] 加载完成,共', data.items.length, '个对象')
} finally { } finally {
this.isLoading.value = false this.isLoading.value = false
// 重新启动自动保存
this.startAutoSave()
} }
} }
@ -392,6 +439,7 @@ export default class StateManager {
*/ */
async saveToLocalstore() { async saveToLocalstore() {
await localforage.setItem(this.storeKey, this.vdata) await localforage.setItem(this.storeKey, this.vdata)
console.log('[StateManager] 数据已保存草稿:' + this.storeKey + ', 共', this.vdata.items.length, '个对象')
} }
/** /**
@ -406,9 +454,10 @@ export default class StateManager {
...saved ...saved
} }
this.isChanged.value = saved.isChanged || false this.isChanged.value = saved.isChanged || false
this.pendingChanges = true
this.fullSync() // 同步到视口 this.fullSync() // 同步到视口
console.log('[StateManager] 从本地存储恢复', this.vdata.items.length, '个对象') console.log('[StateManager] 从本地存储恢复', this.vdata.items.length, '个对象')
this.pendingChanges = false
} }
} catch (error) { } catch (error) {

Loading…
Cancel
Save