nex_animation/frontend/src/recordingGuard.ts

29 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 「有人在录音」的全局计数。
*
* 录音面板(RecordAudio)是挂在素材卡片里的小组件,麦克风流、已录波形、试听 URL
* 全是它的局部状态。卡片一旦在轮询刷新中被换掉,组件卸载,`onBeforeUnmount` 会
* 把麦克风还给系统、这段录音当场蒸发 —— 用户举着麦克风说了半分钟,回来发现面板
* 没了。所以轮询必须知道「现在正有人在录」,把这一轮让过去。
*
* 放模块级而不是组件级:面板可以同时开着好几个(平台声线卡 + 生成抽屉各一个),
* 任何一个开着都得让轮询停下来。
*/
import { computed, ref } from 'vue'
const openSheets = ref(0)
/** 页面用它把自动刷新暂停掉:`paused="modalOpen || recordingBusy"` */
export const recordingBusy = computed(() => openSheets.value > 0)
/** 面板打开时登记,返回释放函数(关闭 / 卸载时必须调用,否则轮询永远醒不过来) */
export function beginRecordingSheet(): () => void {
openSheets.value += 1
let released = false
return () => {
if (released) return
released = true
openSheets.value = Math.max(0, openSheets.value - 1)
}
}