fix(vitepress): 构建阶段避免限流演示定时器常驻

原因:RateLimitingDemo 在脚本顶层直接启动 setInterval,SSR 构建时也会执行,导致 Node 事件循环一直有活跃句柄,构建进程无法退出。

解决:将定时器移动到 onMounted,仅在浏览器端启动,并在 onUnmounted 中清理,避免构建阶段创建常驻任务。

结果:构建流程可正常结束,不再卡住。
This commit is contained in:
sanbuphy
2026-02-06 14:45:39 +08:00
parent fe76383a29
commit f388bb6969
@@ -221,7 +221,7 @@
</template> </template>
<script setup> <script setup>
import { ref, computed, reactive } from 'vue' import { ref, computed, reactive, onMounted, onUnmounted } from 'vue'
const currentAlgo = ref('token') const currentAlgo = ref('token')
const isMatching = ref(false) const isMatching = ref(false)
@@ -447,19 +447,36 @@ const getStatusEmoji = (status) => {
} }
} }
// 定时补充令牌 let tokenInterval = null
setInterval(() => { let leakyInterval = null
onMounted(() => {
tokenInterval = setInterval(() => {
if (bucketState.tokens < bucketState.capacity) { if (bucketState.tokens < bucketState.capacity) {
bucketState.tokens = Math.min(bucketState.tokens + bucketState.rate, bucketState.capacity) bucketState.tokens = Math.min(
bucketState.tokens + bucketState.rate,
bucketState.capacity
)
} }
}, 1000) }, 1000)
// 定时漏桶流出 leakyInterval = setInterval(() => {
setInterval(() => {
if (leakyState.current > 0) { if (leakyState.current > 0) {
leakyState.current = Math.max(0, leakyState.current - leakyState.rate) leakyState.current = Math.max(0, leakyState.current - leakyState.rate)
} }
}, 1000) }, 1000)
})
onUnmounted(() => {
if (tokenInterval) {
clearInterval(tokenInterval)
tokenInterval = null
}
if (leakyInterval) {
clearInterval(leakyInterval)
leakyInterval = null
}
})
</script> </script>
<style scoped> <style scoped>