11<script setup lang="ts">
2- import { ref , watch , onMounted , onBeforeUnmount } from ' vue'
2+ import { shallowRef , ref , watch , onMounted , onBeforeUnmount } from ' vue'
33import type { DynamicScroller } from ' vue-virtual-scroller'
44import type { NodeInfo } from ' ../../../types'
55import { MINIMAP_CONFIG } from ' ../utils/minimapColors'
@@ -17,7 +17,7 @@ const props = defineProps<{
1717const canvasRef = ref <HTMLCanvasElement | null >(null )
1818const containerRef = ref <HTMLDivElement | null >(null )
1919const scrollerRefLocal = ref <InstanceType <typeof DynamicScroller > | null >(null )
20- const nodesRef = ref <NodeTimelineItem []>([])
20+ const nodesRef = shallowRef <NodeTimelineItem []>([])
2121const selectedNodeIdRef = ref <number | null >(null )
2222
2323watch (() => props .scrollerRef , (v ) => { scrollerRefLocal .value = v })
@@ -42,6 +42,8 @@ let resizeObserver: ResizeObserver | null = null
4242let scrollerEl: HTMLElement | null = null
4343let scrollRafId: number | null = null
4444let redrawRafId: number | null = null
45+ let visibleTimer: ReturnType <typeof setTimeout > | null = null
46+ let idleRedrawId: number | null = null
4547
4648const 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+
5470const onScrollerScroll = () => {
5571 if (scrollRafId != null ) return
5672 scrollRafId = requestAnimationFrame (() => {
@@ -87,11 +103,25 @@ const detachScroller = () => {
87103const visible = ref (false )
88104
89105watch (() => 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
93123watch (() => props .nodes , () => {
94- scheduleRedraw ()
124+ scheduleIdleRedraw ()
95125}, { flush: ' post' })
96126
97127watch (() => 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