Skip to content

Commit

Permalink
fix(frontend/sync): update watcher lost efficacy after reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
ForkKILLET committed Dec 11, 2023
1 parent d5fb1ba commit d59a691
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nyadict-frontend",
"private": true,
"version": "0.23.2",
"version": "0.23.3",
"type": "module",
"scripts": {
"dev": "VITE_BUILD_ENV=\"$(uname -sr)\" VITE_LAST_COMMIT=\"$(git log -1 HEAD --oneline --no-color --no-decorate)\" vite --port 1635",
Expand Down
14 changes: 10 additions & 4 deletions frontend/src/stores/archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,20 @@ export const useArchive = defineStore('archives', () => {
const load = (data: IArchiveData) => (newId: string) => {
const oldItem = archiveData[name]
if (oldItem) oldItem[kDispose]()

const key = `${newId}:${name}`
data[name] = getItem(key)
const item = data[name] = getItem(key)
const origDisposer = item[kDispose]
const updateWatchDiposer = watch(archiveData[name], () => {
mitt.emit('data:archive:update', { dataName: name })
}, { deep: true })
item[kDispose] = () => {
origDisposer()
updateWatchDiposer()
}
}
archiveItemHooks.push({ load })
watch(currentId, load(archiveData), { immediate: true })
watch(archiveData[name], () => {
mitt.emit('data:archive:update', { dataName: name })
}, { deep: true })
}

const reloadArchive = () => {
Expand Down
10 changes: 5 additions & 5 deletions frontend/src/utils/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const storeArray = <T extends object, U>(key: string, options: {
return updateLength()
},
reload: () => {
watchStopHandles.forEach(stop => stop())
watchDisposers.forEach(stop => stop())
reactiveArr.splice(0)
load()
}
Expand Down Expand Up @@ -153,13 +153,13 @@ export const storeArray = <T extends object, U>(key: string, options: {
return delete reactiveArr[+ index]
}

const watchStopHandles: WatchStopHandle[] = []
const watchDisposers: WatchStopHandle[] = []
const unwatchIndex = (index: number) => {
watchStopHandles[index]?.()
watchDisposers[index]?.()
}
const watchIndex = (index: number) => {
unwatchIndex(index)
watchStopHandles[index] = watch(reactiveArr[index], (value) => {
watchDisposers[index] = watch(reactiveArr[index], (value) => {
update(index, value)
})
}
Expand All @@ -170,7 +170,7 @@ export const storeArray = <T extends object, U>(key: string, options: {
if (isInit) options.onInit?.(reactiveArr)
return Object.assign(ref(reactiveArr) as Ref<ArrayStore<T>>, {
[kDispose]: () => {
watchStopHandles.forEach(stop => stop())
watchDisposers.forEach(stop => stop())
}
})
}
Expand Down

0 comments on commit d59a691

Please sign in to comment.