refactor: remove history webWorker

This commit is contained in:
ShawnPhang 2024-04-18 20:58:18 +08:00
parent 7c9441c264
commit 3ad6a5a7dd
4 changed files with 73 additions and 12 deletions

View File

@ -1,15 +1,16 @@
/*
* @Author: ShawnPhang
* @Date: 2024-04-18 16:10:07
* @Description: WebWorker处理差分补丁
* @Description: 使
*
*
* left 12.6262638 input 12.63
* @LastEditors: ShawnPhang <https://m.palxp.cn>
* @LastEditTime: 2024-04-18 18:40:27
* @LastEditTime: 2024-04-18 20:54:41
*/
import { onMounted } from 'vue'
import WebWorker from '@/utils/plugins/webWorker'
// import WebWorker from '@/utils/plugins/webWorker'
import historyFactory from '@/utils/widgets/diffLayouts'
import { useHistoryStore, useWidgetStore } from '@/store'
const blackClass: string[] = ['operation-item', 'icon-undo', 'icon-redo']
@ -17,7 +18,9 @@ const whiteKey: string[] = ['ArrowLeft', 'ArrowDown', 'ArrowRight', 'ArrowUp', '
const historyStore = useHistoryStore()
const widgetStore = useWidgetStore()
const historyWorker = new WebWorker('history')
// const historyWorker = new WebWorker('history')
const diffLayouts = new historyFactory()
let processing = false
let historyTimer: any = null
@ -27,7 +30,11 @@ function noPutHistory(target: any) {
}
export default () => {
historyWorker.start(null, (changes: any) => {
// historyWorker.start(null, (changes: any) => {
// changes.patches.length > 0 && historyStore.changeHistory(changes)
// processing = false
// })
diffLayouts.onmessage((changes: any) => {
changes.patches.length > 0 && historyStore.changeHistory(changes)
processing = false
})
@ -36,7 +43,8 @@ export default () => {
'mousedown',
(e: any) => {
if (noPutHistory(e.target)) return
historyWorker.send(!processing ? { op: 'diff', data: JSON.stringify(widgetStore.dLayouts) } : null)
// historyWorker.send(!processing ? { op: 'diff', data: JSON.stringify(widgetStore.dLayouts) } : null)
diffLayouts.postMessage(!processing ? { op: 'diff', data: JSON.stringify(widgetStore.dLayouts) } : null)
processing = true
},
false,
@ -47,7 +55,8 @@ export default () => {
if (noPutHistory(e.target)) return
clearTimeout(historyTimer)
historyTimer = setTimeout(() => {
historyWorker.send({ op: 'done', data: JSON.stringify(widgetStore.dLayouts) })
// historyWorker.send({ op: 'done', data: JSON.stringify(widgetStore.dLayouts) })
diffLayouts.postMessage({ op: 'done', data: JSON.stringify(widgetStore.dLayouts) })
}, 150)
},
false,
@ -56,7 +65,8 @@ export default () => {
'keydown',
(e) => {
if (!whiteKey.includes(e.key)) return
historyWorker.send(!processing ? { op: 'diff', data: JSON.stringify(widgetStore.dLayouts) } : null)
// historyWorker.send(!processing ? { op: 'diff', data: JSON.stringify(widgetStore.dLayouts) } : null)
diffLayouts.postMessage(!processing ? { op: 'diff', data: JSON.stringify(widgetStore.dLayouts) } : null)
processing = true
},
false,
@ -67,7 +77,8 @@ export default () => {
if (!whiteKey.includes(e.key)) return
clearTimeout(historyTimer)
historyTimer = setTimeout(() => {
historyWorker.send({ op: 'done', data: JSON.stringify(widgetStore.dLayouts) })
// historyWorker.send({ op: 'done', data: JSON.stringify(widgetStore.dLayouts) })
diffLayouts.postMessage({ op: 'done', data: JSON.stringify(widgetStore.dLayouts) })
}, 150)
},
false,

View File

@ -3,7 +3,7 @@
* @Date: 2022-03-06 13:53:30
* @Description:
* @LastEditors: ShawnPhang <https://m.palxp.cn>
* @LastEditTime: 2024-04-17 15:58:53
* @LastEditTime: 2024-04-18 20:02:55
*/
export default class WebWorker {
private worker: Worker | undefined

View File

@ -0,0 +1,50 @@
/*
* @Author: ShawnPhang
* @Date: 2023-09-14 11:33:44
* @Description: 使WebWorker
* @LastEditors: ShawnPhang <https://m.palxp.cn>
* @LastEditTime: 2024-04-18 20:52:17
*/
import diff from 'microdiff'
import { produce, applyPatches, enablePatches } from 'immer'
enablePatches()
const ops: any = {
CHANGE: 'replace',
CREATE: 'add',
REMOVE: 'remove',
}
let cloneData = ''
export default class {
private notifi: any
constructor() { }
/**
* onmessage
*/
public onmessage(cb: any) {
this.notifi = cb
}
public postMessage(e: any) {
if (e.op === 'done') {
if (!cloneData) return
let fork = JSON.parse(cloneData)
let curArray = JSON.parse(e.data)
// 比较数据差异
let diffData: any = diff(fork, curArray)
// 生成差分补丁
fork = produce(
fork,
(draft) => {
for (const d of diffData) {
d.op = ops[d.type]
}
draft = applyPatches(draft, diffData)
},
(patches, inversePatches) => {
this.notifi({ patches, inversePatches })
},
)
cloneData = ''
} else cloneData = e.data
}
}

View File

@ -1,9 +1,9 @@
/*
* @Author: ShawnPhang
* @Date: 2023-09-14 11:33:44
* @Description:
* @Description:
* @LastEditors: ShawnPhang <https://m.palxp.cn>
* @LastEditTime: 2024-04-18 13:59:47
* @LastEditTime: 2024-04-18 20:06:51
*/
import diff from 'microdiff'
import { produce, applyPatches, enablePatches } from 'immer'