Skip to content

Commit 347709a

Browse files
committed
fix: minimap 导致的切换任务延迟
1 parent 7eaccae commit 347709a

2 files changed

Lines changed: 48 additions & 5 deletions

File tree

src/views/process/components/NodeTimelineList.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { DynamicScroller, DynamicScrollerItem } from 'vue-virtual-scroller'
55
import type { NodeInfo } from '../../../types'
66
import NodeCard from '../../../components/NodeCard.vue'
77
import NodeTimelineMinimap from './NodeTimelineMinimap.vue'
8+
import { MINIMAP_CONFIG } from '../utils/minimapColors'
89
910
type NodeTimelineItem = NodeInfo & {
1011
_uniqueKey: string
@@ -109,7 +110,7 @@ const setLocalScrollerRef = (value: Element | object | null) => {
109110
</template>
110111
</DynamicScroller>
111112
<node-timeline-minimap
112-
v-if="safeScrollToItem"
113+
v-if="safeScrollToItem && nodes.length >= MINIMAP_CONFIG.minNodesToShow"
113114
:nodes="nodes"
114115
:scroller-ref="localScrollerRef"
115116
:selected-node-id="selectedNodeId ?? null"

src/views/process/components/NodeTimelineMinimap.vue

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { ref, watch, onMounted, onBeforeUnmount } from 'vue'
2+
import { shallowRef, ref, watch, onMounted, onBeforeUnmount } from 'vue'
33
import type { DynamicScroller } from 'vue-virtual-scroller'
44
import type { NodeInfo } from '../../../types'
55
import { MINIMAP_CONFIG } from '../utils/minimapColors'
@@ -17,7 +17,7 @@ const props = defineProps<{
1717
const canvasRef = ref<HTMLCanvasElement | null>(null)
1818
const containerRef = ref<HTMLDivElement | null>(null)
1919
const scrollerRefLocal = ref<InstanceType<typeof DynamicScroller> | null>(null)
20-
const nodesRef = ref<NodeTimelineItem[]>([])
20+
const nodesRef = shallowRef<NodeTimelineItem[]>([])
2121
const selectedNodeIdRef = ref<number | null>(null)
2222
2323
watch(() => props.scrollerRef, (v) => { scrollerRefLocal.value = v })
@@ -42,6 +42,8 @@ let resizeObserver: ResizeObserver | null = null
4242
let scrollerEl: HTMLElement | null = null
4343
let scrollRafId: number | null = null
4444
let redrawRafId: number | null = null
45+
let visibleTimer: ReturnType<typeof setTimeout> | null = null
46+
let idleRedrawId: number | null = null
4547
4648
const scheduleRedraw = () => {
4749
if (redrawRafId != null) return
@@ -51,6 +53,20 @@ const scheduleRedraw = () => {
5153
})
5254
}
5355
56+
const scheduleIdleRedraw = () => {
57+
if (idleRedrawId != null) return
58+
const run = () => {
59+
idleRedrawId = null
60+
scheduleRedraw()
61+
}
62+
const requestIdle = window.requestIdleCallback
63+
if (requestIdle) {
64+
idleRedrawId = requestIdle(run, { timeout: 300 }) as unknown as number
65+
return
66+
}
67+
idleRedrawId = window.setTimeout(run, 80)
68+
}
69+
5470
const onScrollerScroll = () => {
5571
if (scrollRafId != null) return
5672
scrollRafId = requestAnimationFrame(() => {
@@ -87,11 +103,25 @@ const detachScroller = () => {
87103
const visible = ref(false)
88104
89105
watch(() => props.nodes.length, () => {
90-
visible.value = props.nodes.length >= MINIMAP_CONFIG.minNodesToShow
106+
if (visibleTimer) {
107+
clearTimeout(visibleTimer)
108+
visibleTimer = null
109+
}
110+
111+
if (props.nodes.length < MINIMAP_CONFIG.minNodesToShow) {
112+
visible.value = false
113+
return
114+
}
115+
116+
visibleTimer = setTimeout(() => {
117+
visibleTimer = null
118+
visible.value = true
119+
scheduleIdleRedraw()
120+
}, 120)
91121
}, { immediate: true })
92122
93123
watch(() => props.nodes, () => {
94-
scheduleRedraw()
124+
scheduleIdleRedraw()
95125
}, { flush: 'post' })
96126
97127
watch(() => props.selectedNodeId, () => {
@@ -124,6 +154,18 @@ onBeforeUnmount(() => {
124154
cancelAnimationFrame(redrawRafId)
125155
redrawRafId = null
126156
}
157+
if (visibleTimer) {
158+
clearTimeout(visibleTimer)
159+
visibleTimer = null
160+
}
161+
if (idleRedrawId != null) {
162+
if (window.cancelIdleCallback) {
163+
window.cancelIdleCallback(idleRedrawId)
164+
} else {
165+
clearTimeout(idleRedrawId)
166+
}
167+
idleRedrawId = null
168+
}
127169
})
128170
</script>
129171

0 commit comments

Comments
 (0)