优化
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { computed, onScopeDispose, ref, watch, type ComputedRef, type Ref } from 'vue'
|
||||
import { computed, getCurrentInstance, onActivated, onScopeDispose, ref, watch, type ComputedRef, type Ref } from 'vue'
|
||||
import { useUnsavedWorkStore } from '@/core/unsaved-work/unsavedWork'
|
||||
|
||||
interface UnsavedWorkRegistrationOptions<T extends object> {
|
||||
@@ -13,12 +13,16 @@ export function useUnsavedWorkRegistration<T extends object>(
|
||||
options: UnsavedWorkRegistrationOptions<T>,
|
||||
) {
|
||||
const store = useUnsavedWorkStore()
|
||||
const routeName = getCurrentInstance()?.proxy?.$route?.name
|
||||
const workspaceTabId = routeName === undefined || routeName === null ? null : String(routeName)
|
||||
const owner = Symbol(options.key)
|
||||
const snapshot = computed(() => JSON.stringify(options.model))
|
||||
const baseline = ref(snapshot.value)
|
||||
store.claim(options.key, owner)
|
||||
|
||||
function sync() {
|
||||
if (!options.isOpen.value) {
|
||||
store.remove(options.key)
|
||||
store.remove(options.key, owner)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -27,7 +31,8 @@ export function useUnsavedWorkRegistration<T extends object>(
|
||||
label: options.label.value,
|
||||
dirty: snapshot.value !== baseline.value,
|
||||
requiredPermission: options.requiredPermission.value,
|
||||
})
|
||||
workspaceTabId,
|
||||
}, owner)
|
||||
}
|
||||
|
||||
function captureBaseline() {
|
||||
@@ -41,7 +46,8 @@ export function useUnsavedWorkRegistration<T extends object>(
|
||||
{ flush: 'sync' },
|
||||
)
|
||||
sync()
|
||||
onScopeDispose(() => store.remove(options.key))
|
||||
onActivated(sync)
|
||||
onScopeDispose(() => store.release(options.key, owner))
|
||||
|
||||
return { captureBaseline }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user