koishi-plugin-chat-patch 2.1.2 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,130 @@
1
+ import { ref, onUnmounted } from 'vue'
2
+ import { send } from '@koishijs/client'
3
+
4
+ /**
5
+ * 视频按需加载 composable
6
+ * 只在用户点击播放时才加载视频,避免内存占用
7
+ */
8
+ export function useVideoCache() {
9
+ // 当前加载的视频 URL(原始 URL -> blob URL)
10
+ const loadedVideos = ref<Record<string, string>>({})
11
+ // 正在加载的视频
12
+ const loadingVideos = ref<Set<string>>(new Set())
13
+ // 当前的 blob URL,用于清理
14
+ const currentBlobUrl = ref<string | null>(null)
15
+
16
+ /**
17
+ * 按需加载视频
18
+ * @param url 原始视频 URL
19
+ * @returns blob URL 或 null
20
+ */
21
+ async function loadVideo(url: string): Promise<string | null> {
22
+ // 如果已经加载过,直接返回
23
+ if (loadedVideos.value[url]) {
24
+ return loadedVideos.value[url]
25
+ }
26
+
27
+ // 如果正在加载,等待加载完成
28
+ if (loadingVideos.value.has(url)) {
29
+ // 等待加载完成
30
+ return new Promise((resolve) => {
31
+ const checkInterval = setInterval(() => {
32
+ if (loadedVideos.value[url]) {
33
+ clearInterval(checkInterval)
34
+ resolve(loadedVideos.value[url])
35
+ }
36
+ if (!loadingVideos.value.has(url)) {
37
+ clearInterval(checkInterval)
38
+ resolve(null)
39
+ }
40
+ }, 100)
41
+ })
42
+ }
43
+
44
+ // 开始加载
45
+ loadingVideos.value.add(url)
46
+
47
+ try {
48
+ const result = await (send as any)('fetch-video-temp', { url })
49
+
50
+ if (result.success) {
51
+ // 清理上一个 blob URL
52
+ if (currentBlobUrl.value) {
53
+ URL.revokeObjectURL(currentBlobUrl.value)
54
+ }
55
+
56
+ // 如果返回的是 dataUrl,转换为 blob
57
+ let blobUrl: string
58
+ if (result.dataUrl) {
59
+ // 从 data URL 创建 blob
60
+ const response = await fetch(result.dataUrl)
61
+ const blob = await response.blob()
62
+ blobUrl = URL.createObjectURL(blob)
63
+ } else if (result.fileUrl) {
64
+ // 如果是文件 URL,也尝试转换为 blob
65
+ try {
66
+ const response = await fetch(result.fileUrl)
67
+ const blob = await response.blob()
68
+ blobUrl = URL.createObjectURL(blob)
69
+ } catch (e) {
70
+ // 如果转换失败,直接使用文件 URL
71
+ blobUrl = result.fileUrl
72
+ }
73
+ } else {
74
+ return null
75
+ }
76
+
77
+ currentBlobUrl.value = blobUrl
78
+ loadedVideos.value[url] = blobUrl
79
+ return blobUrl
80
+ }
81
+
82
+ return null
83
+ } catch (error) {
84
+ console.error('加载视频失败:', error)
85
+ return null
86
+ } finally {
87
+ loadingVideos.value.delete(url)
88
+ }
89
+ }
90
+
91
+ /**
92
+ * 检查视频是否正在加载
93
+ */
94
+ function isVideoLoading(url: string): boolean {
95
+ return loadingVideos.value.has(url)
96
+ }
97
+
98
+ /**
99
+ * 检查视频是否已加载
100
+ */
101
+ function isVideoLoaded(url: string): boolean {
102
+ return !!loadedVideos.value[url]
103
+ }
104
+
105
+ /**
106
+ * 清空所有已加载的视频
107
+ */
108
+ function clearAllVideos() {
109
+ // 清理所有 blob URLs
110
+ Object.values(loadedVideos.value).forEach(blobUrl => {
111
+ if (blobUrl.startsWith('blob:')) {
112
+ URL.revokeObjectURL(blobUrl)
113
+ }
114
+ })
115
+ loadedVideos.value = {}
116
+ currentBlobUrl.value = null
117
+ }
118
+
119
+ // 组件卸载时清理
120
+ onUnmounted(() => {
121
+ clearAllVideos()
122
+ })
123
+
124
+ return {
125
+ loadVideo,
126
+ isVideoLoading,
127
+ isVideoLoaded,
128
+ clearAllVideos
129
+ }
130
+ }