a2bei4-utils 1.0.0 → 1.0.2
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.
- package/LICENSE +21 -21
- package/README.md +2 -2
- package/dist/a2bei4.utils.cjs.js +1051 -250
- package/dist/a2bei4.utils.cjs.js.map +1 -1
- package/dist/a2bei4.utils.cjs.min.js +1 -1
- package/dist/a2bei4.utils.cjs.min.js.map +1 -1
- package/dist/a2bei4.utils.esm.js +1047 -251
- package/dist/a2bei4.utils.esm.js.map +1 -1
- package/dist/a2bei4.utils.esm.min.js +1 -1
- package/dist/a2bei4.utils.esm.min.js.map +1 -1
- package/dist/a2bei4.utils.umd.js +1051 -250
- package/dist/a2bei4.utils.umd.js.map +1 -1
- package/dist/a2bei4.utils.umd.min.js +1 -1
- package/dist/a2bei4.utils.umd.min.js.map +1 -1
- package/dist/arr.cjs +27 -27
- package/dist/arr.cjs.map +1 -1
- package/dist/arr.js +27 -27
- package/dist/arr.js.map +1 -1
- package/dist/audio.cjs +281 -0
- package/dist/audio.cjs.map +1 -0
- package/dist/audio.js +278 -0
- package/dist/audio.js.map +1 -0
- package/dist/common.cjs +6 -6
- package/dist/common.cjs.map +1 -1
- package/dist/common.js +6 -6
- package/dist/common.js.map +1 -1
- package/dist/download.cjs +43 -0
- package/dist/download.cjs.map +1 -1
- package/dist/download.js +43 -1
- package/dist/download.js.map +1 -1
- package/dist/evt.cjs +148 -148
- package/dist/evt.cjs.map +1 -1
- package/dist/evt.js +148 -148
- package/dist/evt.js.map +1 -1
- package/dist/id.cjs +68 -68
- package/dist/id.cjs.map +1 -1
- package/dist/id.js +68 -68
- package/dist/id.js.map +1 -1
- package/dist/timer.cjs +0 -1
- package/dist/timer.cjs.map +1 -1
- package/dist/timer.js +0 -1
- package/dist/timer.js.map +1 -1
- package/dist/tree.cjs +75 -0
- package/dist/tree.cjs.map +1 -1
- package/dist/tree.js +75 -1
- package/dist/tree.js.map +1 -1
- package/dist/webSocket.cjs +409 -0
- package/dist/webSocket.cjs.map +1 -0
- package/dist/webSocket.js +407 -0
- package/dist/webSocket.js.map +1 -0
- package/package.json +12 -1
- package/readme.txt +8 -5
- package/types/audio.d.ts +57 -0
- package/types/download.d.ts +12 -1
- package/types/index.d.ts +208 -2
- package/types/tree.d.ts +17 -1
- package/types/webSocket.d.ts +124 -0
package/dist/a2bei4.utils.cjs.js
CHANGED
|
@@ -1,32 +1,308 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* 使用 Fisher-Yates 算法对数组 **原地** 随机乱序。
|
|
5
|
-
* @template T
|
|
6
|
-
* @param {T[]} arr - 要乱序的数组
|
|
7
|
-
* @returns {T[]} 返回传入的同一数组实例(已乱序)
|
|
8
|
-
*/
|
|
9
|
-
function shuffle(arr) {
|
|
10
|
-
// 方式一:
|
|
11
|
-
// arr.sort(() => Math.random() - 0.5);
|
|
12
|
-
// 方式二:
|
|
13
|
-
for (let i = arr.length - 1; i > 0; i--) {
|
|
14
|
-
const j = Math.floor(Math.random() * (i + 1));
|
|
15
|
-
[arr[i], arr[j]] = [arr[j], arr[i]];
|
|
16
|
-
}
|
|
17
|
-
return arr;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 将数组中的元素从 `fromIndex` 移动到 `toIndex`,**原地** 修改并返回该数组。
|
|
22
|
-
* @template T
|
|
23
|
-
* @param {T[]} arr - 要操作的数组
|
|
24
|
-
* @param {number} fromIndex - 原始下标
|
|
25
|
-
* @param {number} toIndex - 目标下标
|
|
26
|
-
* @returns {T[]} 返回传入的同一数组实例
|
|
27
|
-
*/
|
|
28
|
-
function moveItem(arr, fromIndex, toIndex) {
|
|
29
|
-
arr.splice(toIndex, 0, arr.splice(fromIndex, 1)[0]);
|
|
3
|
+
/**
|
|
4
|
+
* 使用 Fisher-Yates 算法对数组 **原地** 随机乱序。
|
|
5
|
+
* @template T
|
|
6
|
+
* @param {T[]} arr - 要乱序的数组
|
|
7
|
+
* @returns {T[]} 返回传入的同一数组实例(已乱序)
|
|
8
|
+
*/
|
|
9
|
+
function shuffle(arr) {
|
|
10
|
+
// 方式一:
|
|
11
|
+
// arr.sort(() => Math.random() - 0.5);
|
|
12
|
+
// 方式二:
|
|
13
|
+
for (let i = arr.length - 1; i > 0; i--) {
|
|
14
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
15
|
+
[arr[i], arr[j]] = [arr[j], arr[i]];
|
|
16
|
+
}
|
|
17
|
+
return arr;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 将数组中的元素从 `fromIndex` 移动到 `toIndex`,**原地** 修改并返回该数组。
|
|
22
|
+
* @template T
|
|
23
|
+
* @param {T[]} arr - 要操作的数组
|
|
24
|
+
* @param {number} fromIndex - 原始下标
|
|
25
|
+
* @param {number} toIndex - 目标下标
|
|
26
|
+
* @returns {T[]} 返回传入的同一数组实例
|
|
27
|
+
*/
|
|
28
|
+
function moveItem(arr, fromIndex, toIndex) {
|
|
29
|
+
arr.splice(toIndex, 0, arr.splice(fromIndex, 1)[0]);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const AudioStreamResamplerProcessorCode = `
|
|
33
|
+
class AudioStreamResamplerProcessor extends AudioWorkletProcessor {
|
|
34
|
+
constructor(options) {
|
|
35
|
+
super();
|
|
36
|
+
const config = options.processorOptions || {};
|
|
37
|
+
this.targetSampleRate = config.targetSampleRate || 16000;
|
|
38
|
+
this.sourceSampleRate = sampleRate;
|
|
39
|
+
this.downsampleRatio = this.sourceSampleRate / this.targetSampleRate;
|
|
40
|
+
|
|
41
|
+
// 16000Hz 用 60ms chunk (960 samples),其他用 1024
|
|
42
|
+
this.chunkSize = this.targetSampleRate === 16000 ? 960 : 1024;
|
|
43
|
+
|
|
44
|
+
const sourceBufferSize = config.sourceBufferSize || 16384; // ~340ms @48kHz
|
|
45
|
+
const pcmBufferSize = config.pcmBufferSize || (this.chunkSize * 10); // 更大缓冲,减少溢出概率
|
|
46
|
+
|
|
47
|
+
this.sourceBuffer = new Float32Array(sourceBufferSize);
|
|
48
|
+
this.sourceBufferLength = 0;
|
|
49
|
+
|
|
50
|
+
this.pcmBuffer = new Int16Array(pcmBufferSize);
|
|
51
|
+
this.pcmBufferIndex = 0;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
process(inputs, outputs, parameters) {
|
|
55
|
+
const input = inputs[0];
|
|
56
|
+
if (!input || input.length === 0 || input[0].length === 0) return true;
|
|
57
|
+
const inputChannel = input[0];
|
|
58
|
+
|
|
59
|
+
// 1. 写入源缓冲区(溢出时覆盖最旧数据)
|
|
60
|
+
const newLength = this.sourceBufferLength + inputChannel.length;
|
|
61
|
+
if (newLength > this.sourceBuffer.length) {
|
|
62
|
+
const overflow = newLength - this.sourceBuffer.length;
|
|
63
|
+
this.sourceBuffer.copyWithin(0, overflow);
|
|
64
|
+
this.sourceBufferLength = this.sourceBuffer.length - inputChannel.length;
|
|
65
|
+
}
|
|
66
|
+
this.sourceBuffer.set(inputChannel, this.sourceBufferLength);
|
|
67
|
+
this.sourceBufferLength += inputChannel.length;
|
|
68
|
+
|
|
69
|
+
// 2. 计算可降采样样本数
|
|
70
|
+
const availableOutputSamples = Math.floor(this.sourceBufferLength / this.downsampleRatio);
|
|
71
|
+
if (availableOutputSamples === 0) return true;
|
|
72
|
+
|
|
73
|
+
// 3. 线性插值降采样
|
|
74
|
+
const downsampled = new Float32Array(availableOutputSamples);
|
|
75
|
+
for (let i = 0; i < availableOutputSamples; i++) {
|
|
76
|
+
const srcIndex = i * this.downsampleRatio;
|
|
77
|
+
const srcIndexInt = Math.floor(srcIndex);
|
|
78
|
+
const fraction = srcIndex - srcIndexInt;
|
|
79
|
+
const val0 = this.sourceBuffer[srcIndexInt];
|
|
80
|
+
const val1 = srcIndexInt + 1 < this.sourceBufferLength ? this.sourceBuffer[srcIndexInt + 1] : val0;
|
|
81
|
+
downsampled[i] = val0 + (val1 - val0) * fraction;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 4. Float32 → Int16 PCM
|
|
85
|
+
const pcmData = this.floatTo16BitPCM(downsampled);
|
|
86
|
+
|
|
87
|
+
// 5. 写入 PCM 缓冲区(空间不足时直接覆盖最旧,缓冲区足够大基本不会触发)
|
|
88
|
+
if (this.pcmBufferIndex + pcmData.length > this.pcmBuffer.length) {
|
|
89
|
+
// 简单策略:从头覆盖(丢弃最旧数据)
|
|
90
|
+
this.pcmBufferIndex = 0;
|
|
91
|
+
}
|
|
92
|
+
this.pcmBuffer.set(pcmData, this.pcmBufferIndex);
|
|
93
|
+
this.pcmBufferIndex += pcmData.length;
|
|
94
|
+
|
|
95
|
+
// 6. 发送所有完整的 chunk(关键:复制到新数组再转移)
|
|
96
|
+
while (this.pcmBufferIndex >= this.chunkSize) {
|
|
97
|
+
// 方法1:推荐,使用 slice(隐式复制到新 ArrayBuffer)
|
|
98
|
+
const chunk = this.pcmBuffer.slice(0, this.chunkSize);
|
|
99
|
+
|
|
100
|
+
// 方法2:等价写法
|
|
101
|
+
// const chunk = new Int16Array(this.pcmBuffer.subarray(0, this.chunkSize));
|
|
102
|
+
|
|
103
|
+
this.port.postMessage(chunk, [chunk.buffer]); // 转移新缓冲区,安全!
|
|
104
|
+
|
|
105
|
+
// 移动剩余数据到开头
|
|
106
|
+
this.pcmBuffer.copyWithin(0, this.chunkSize, this.pcmBufferIndex);
|
|
107
|
+
this.pcmBufferIndex -= this.chunkSize;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// 7. 清理已消费的源数据
|
|
111
|
+
const consumedSrc = Math.floor(availableOutputSamples * this.downsampleRatio + 0.5); // 四舍五入更准
|
|
112
|
+
if (consumedSrc > 0) {
|
|
113
|
+
this.sourceBuffer.copyWithin(0, consumedSrc, this.sourceBufferLength);
|
|
114
|
+
this.sourceBufferLength -= consumedSrc;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
floatTo16BitPCM(input) {
|
|
121
|
+
const output = new Int16Array(input.length);
|
|
122
|
+
for (let i = 0; i < input.length; i++) {
|
|
123
|
+
const s = Math.max(-1, Math.min(1, input[i]));
|
|
124
|
+
output[i] = s < 0 ? s * 0x8000 : s * 0x7FFF;
|
|
125
|
+
}
|
|
126
|
+
return output;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
registerProcessor('audio-stream-resampler-processor', AudioStreamResamplerProcessor);
|
|
131
|
+
`;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 浏览器端实时音频流重采样器。
|
|
135
|
+
* 基于 AudioWorklet 将麦克风/媒体流转换为 16 kHz、16-bit、单声道 PCM,
|
|
136
|
+
* 并通过回调逐块输出,可选保存完整 PCM 用于后续合并。
|
|
137
|
+
*/
|
|
138
|
+
class AudioStreamResampler {
|
|
139
|
+
/**
|
|
140
|
+
* @param {object} config
|
|
141
|
+
* @param {function(Int16Array)} config.onData - 收到一个 chunk PCM 数据的回调
|
|
142
|
+
* @param {function(string, string)} [config.onStateChange] - 状态变化回调
|
|
143
|
+
* @param {object} [config.processorOptions] - 传递给 AudioWorklet 的选项
|
|
144
|
+
* @param {boolean} [config.saveFullPcm=false] - 是否在内部保存所有 PCM 用于 stop 时合并(长时间录音建议关闭)
|
|
145
|
+
*/
|
|
146
|
+
constructor(config) {
|
|
147
|
+
this.onData = config.onData || (() => {});
|
|
148
|
+
this.onStateChange = config.onStateChange || (() => {});
|
|
149
|
+
this.processorOptions = config.processorOptions || {};
|
|
150
|
+
this.saveFullPcm = config.saveFullPcm ?? false;
|
|
151
|
+
|
|
152
|
+
this.audioContext = null;
|
|
153
|
+
this.workletNode = null;
|
|
154
|
+
this.source = null;
|
|
155
|
+
this.workletUrl = null;
|
|
156
|
+
this.fullPcmData = this.saveFullPcm ? [] : null;
|
|
157
|
+
|
|
158
|
+
this.isInitialized = false;
|
|
159
|
+
this.isProcessing = false;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* 初始化 AudioContext 并加载 AudioWorklet。
|
|
164
|
+
* 完成后状态变为 `"ready"`。
|
|
165
|
+
*/
|
|
166
|
+
async init() {
|
|
167
|
+
this.onStateChange("initializing", "正在初始化音频环境...");
|
|
168
|
+
try {
|
|
169
|
+
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
|
170
|
+
|
|
171
|
+
const blob = new Blob([AudioStreamResamplerProcessorCode], { type: "application/javascript" });
|
|
172
|
+
this.workletUrl = URL.createObjectURL(blob);
|
|
173
|
+
await this.audioContext.audioWorklet.addModule(this.workletUrl);
|
|
174
|
+
|
|
175
|
+
this.workletNode = new AudioWorkletNode(this.audioContext, "audio-stream-resampler-processor", {
|
|
176
|
+
processorOptions: this.processorOptions
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
this.workletNode.port.onmessage = (event) => {
|
|
180
|
+
const chunk = event.data; // Int16Array
|
|
181
|
+
this.onData(chunk);
|
|
182
|
+
if (this.saveFullPcm) {
|
|
183
|
+
this.fullPcmData.push(chunk);
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
this.isInitialized = true;
|
|
188
|
+
this.onStateChange("ready", "音频环境已就绪");
|
|
189
|
+
} catch (err) {
|
|
190
|
+
console.error("AudioStreamResampler init error:", err);
|
|
191
|
+
this.onStateChange("error", `初始化失败: ${err.message}`);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* 绑定媒体流,开始实时处理。
|
|
197
|
+
* @param {MediaStream} stream - 通过 getUserMedia 或其他方式获得的流
|
|
198
|
+
*/
|
|
199
|
+
setMediaStream(stream) {
|
|
200
|
+
if (!this.isInitialized) {
|
|
201
|
+
console.error("请先调用 init()");
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (this.source) {
|
|
206
|
+
this.source.disconnect();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
this.source = this.audioContext.createMediaStreamSource(stream);
|
|
210
|
+
this.source.connect(this.workletNode);
|
|
211
|
+
// workletNode 默认连接到 destination,可选断开以静音
|
|
212
|
+
// this.workletNode.connect(this.audioContext.destination);
|
|
213
|
+
|
|
214
|
+
this.isProcessing = true;
|
|
215
|
+
this.onStateChange("processing", "正在处理音频流...");
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* 停止处理并释放资源。
|
|
220
|
+
* @param {(fullPcm: Int16Array) => void} [callback] - 若构造时 `saveFullPcm=true`,会把合并后的完整 PCM 通过此回调传出
|
|
221
|
+
*/
|
|
222
|
+
stop(callback) {
|
|
223
|
+
if (!this.isProcessing) return;
|
|
224
|
+
|
|
225
|
+
this.onStateChange("stopping", "正在停止...");
|
|
226
|
+
|
|
227
|
+
if (this.source) {
|
|
228
|
+
this.source.disconnect();
|
|
229
|
+
this.source = null;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
this._cleanup();
|
|
233
|
+
|
|
234
|
+
this.onStateChange("stopped", "已停止");
|
|
235
|
+
|
|
236
|
+
if (this.saveFullPcm && callback && typeof callback === "function") {
|
|
237
|
+
const totalLength = this.fullPcmData.reduce((sum, chunk) => sum + chunk.length, 0);
|
|
238
|
+
const combined = new Int16Array(totalLength);
|
|
239
|
+
let offset = 0;
|
|
240
|
+
for (const chunk of this.fullPcmData) {
|
|
241
|
+
combined.set(chunk, offset);
|
|
242
|
+
offset += chunk.length;
|
|
243
|
+
}
|
|
244
|
+
callback(combined);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
_cleanup() {
|
|
249
|
+
if (this.workletNode) {
|
|
250
|
+
this.workletNode.disconnect();
|
|
251
|
+
this.workletNode.port.close();
|
|
252
|
+
this.workletNode = null;
|
|
253
|
+
}
|
|
254
|
+
if (this.audioContext && this.audioContext.state !== "closed") {
|
|
255
|
+
this.audioContext.close();
|
|
256
|
+
this.audioContext = null;
|
|
257
|
+
}
|
|
258
|
+
if (this.workletUrl) {
|
|
259
|
+
URL.revokeObjectURL(this.workletUrl);
|
|
260
|
+
this.workletUrl = null;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
this.isProcessing = false;
|
|
264
|
+
this.isInitialized = false;
|
|
265
|
+
if (this.fullPcmData) {
|
|
266
|
+
this.fullPcmData.length = 0;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* 将 16-bit 单声道 PCM 数据封装成标准 WAV Blob。
|
|
273
|
+
*
|
|
274
|
+
* @param {Int16Array} pcmData - PCM 采样数据
|
|
275
|
+
* @param {number} [sampleRate=16000] - 采样率,默认 16 kHz
|
|
276
|
+
* @returns {Blob} audio/wav Blob
|
|
277
|
+
*/
|
|
278
|
+
function pcmToWavBlob(pcmData, sampleRate = 16000) {
|
|
279
|
+
const length = pcmData.length;
|
|
280
|
+
const buffer = new ArrayBuffer(44 + length * 2);
|
|
281
|
+
const view = new DataView(buffer);
|
|
282
|
+
const writeString = (offset, string) => {
|
|
283
|
+
for (let i = 0; i < string.length; i++) {
|
|
284
|
+
view.setUint8(offset + i, string.charCodeAt(i));
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
writeString(0, "RIFF");
|
|
288
|
+
view.setUint32(4, 36 + length * 2, true);
|
|
289
|
+
writeString(8, "WAVE");
|
|
290
|
+
writeString(12, "fmt ");
|
|
291
|
+
view.setUint32(16, 16, true);
|
|
292
|
+
view.setUint16(20, 1, true);
|
|
293
|
+
view.setUint16(22, 1, true);
|
|
294
|
+
view.setUint32(24, sampleRate, true);
|
|
295
|
+
view.setUint32(28, sampleRate * 2, true);
|
|
296
|
+
view.setUint16(32, 2, true);
|
|
297
|
+
view.setUint16(34, 16, true);
|
|
298
|
+
writeString(36, "data");
|
|
299
|
+
view.setUint32(40, length * 2, true);
|
|
300
|
+
let offset = 44;
|
|
301
|
+
for (let i = 0; i < length; i++) {
|
|
302
|
+
view.setInt16(offset, pcmData[i], true);
|
|
303
|
+
offset += 2;
|
|
304
|
+
}
|
|
305
|
+
return new Blob([buffer], { type: "audio/wav" });
|
|
30
306
|
}
|
|
31
307
|
|
|
32
308
|
/**
|
|
@@ -87,7 +363,7 @@ function getSearchParam(key) {
|
|
|
87
363
|
|
|
88
364
|
/**
|
|
89
365
|
* 返回任意值的运行时类型字符串(小写形式)。
|
|
90
|
-
*
|
|
366
|
+
*
|
|
91
367
|
* @param {*} obj 待检测的值
|
|
92
368
|
* @returns {keyof globalThis|"blob"|"file"|"formdata"|string} 小写类型名
|
|
93
369
|
*/
|
|
@@ -100,7 +376,7 @@ function getDataType(obj) {
|
|
|
100
376
|
|
|
101
377
|
/**
|
|
102
378
|
* 判断值是否为原生 Blob(含 File)。
|
|
103
|
-
*
|
|
379
|
+
*
|
|
104
380
|
* @param {*} obj - 待检测的值
|
|
105
381
|
* @returns {obj is Blob}
|
|
106
382
|
*/
|
|
@@ -110,7 +386,7 @@ function isBlob(obj) {
|
|
|
110
386
|
|
|
111
387
|
/**
|
|
112
388
|
* 判断值是否为**纯粹**的 Object(即 `{}` 或 `new Object()`,不含数组、null、自定义类等)。
|
|
113
|
-
*
|
|
389
|
+
*
|
|
114
390
|
* @param {*} obj - 待检测的值
|
|
115
391
|
* @returns {obj is Record<PropertyKey, any>}
|
|
116
392
|
*/
|
|
@@ -120,7 +396,7 @@ function isPlainObject(obj) {
|
|
|
120
396
|
|
|
121
397
|
/**
|
|
122
398
|
* 判断值是否为 Promise(含 Promise 子类)。
|
|
123
|
-
*
|
|
399
|
+
*
|
|
124
400
|
* @param {*} obj - 待检测的值
|
|
125
401
|
* @returns {obj is Promise<any>}
|
|
126
402
|
*/
|
|
@@ -140,7 +416,7 @@ function isDate(t) {
|
|
|
140
416
|
|
|
141
417
|
/**
|
|
142
418
|
* 判断值是否为函数(含异步函数、生成器函数、类)。
|
|
143
|
-
*
|
|
419
|
+
*
|
|
144
420
|
* @param {*} obj - 待检测的值
|
|
145
421
|
* @returns {obj is Function}
|
|
146
422
|
*/
|
|
@@ -150,7 +426,7 @@ function isFunction(obj) {
|
|
|
150
426
|
|
|
151
427
|
/**
|
|
152
428
|
* 判断值是否为**非空**字符串。
|
|
153
|
-
*
|
|
429
|
+
*
|
|
154
430
|
* @param {*} obj - 待检测的值
|
|
155
431
|
* @returns {obj is string}
|
|
156
432
|
*/
|
|
@@ -642,6 +918,7 @@ function formatDurationMaxHour(totalSeconds, options = {}) {
|
|
|
642
918
|
|
|
643
919
|
/**
|
|
644
920
|
* 通过动态创建 `<a>` 标签触发浏览器下载。
|
|
921
|
+
* 注意:此方法可能无法强制下载浏览器原生支持的文件(如图片、PDF),浏览器可能会选择直接打开。
|
|
645
922
|
*
|
|
646
923
|
* @param {string} url - 任意可下载地址(同源或允许跨域)
|
|
647
924
|
* @param {string} [fileName] - 保存到本地的文件名;不传时使用时间戳
|
|
@@ -702,224 +979,265 @@ function downloadJSON(data, fileName) {
|
|
|
702
979
|
downloadByData(data, fileName, "application/json");
|
|
703
980
|
}
|
|
704
981
|
|
|
705
|
-
/**
|
|
706
|
-
*
|
|
707
|
-
*
|
|
708
|
-
*
|
|
709
|
-
*
|
|
710
|
-
* -
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
}
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
982
|
+
/**
|
|
983
|
+
* 通过 `fetch` 获取资源并强制下载,避免浏览器直接打开文件。
|
|
984
|
+
* 此方法适用于下载图片、PDF 等浏览器默认会打开的文件类型。
|
|
985
|
+
* 如果 `fetch` 失败(如 CORS 策略阻止),会回退到 `downloadByUrl` 方法作为备选方案。
|
|
986
|
+
*
|
|
987
|
+
* @param {string} url - 文件的 URL 地址
|
|
988
|
+
* @param {string} [fileName] - 保存到本地的文件名。如果不提供,会尝试从 URL 中自动提取
|
|
989
|
+
* @returns {Promise<void>} 返回一个 Promise,在下载开始或失败后 resolve
|
|
990
|
+
*/
|
|
991
|
+
async function fetchOrDownloadByUrl(url, fileName) {
|
|
992
|
+
// 如果未提供文件名,尝试从 URL 路径中提取
|
|
993
|
+
if (!fileName) {
|
|
994
|
+
try {
|
|
995
|
+
const urlPathname = new URL(url).pathname;
|
|
996
|
+
// 获取路径的最后一部分作为文件名,并移除可能的查询参数
|
|
997
|
+
fileName = urlPathname.substring(urlPathname.lastIndexOf("/") + 1).split("?")[0];
|
|
998
|
+
} catch (e) {}
|
|
999
|
+
// 如果提取后文件名为空(例如 URL 以 '/' 结尾),也使用时间戳
|
|
1000
|
+
if (!fileName) {
|
|
1001
|
+
fileName = Date.now().toString();
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
try {
|
|
1006
|
+
const response = await fetch(url, {
|
|
1007
|
+
method: "GET",
|
|
1008
|
+
mode: "cors",
|
|
1009
|
+
cache: "no-cache"
|
|
1010
|
+
});
|
|
1011
|
+
|
|
1012
|
+
if (!response.ok) {
|
|
1013
|
+
throw new Error(`HTTP error! ${response.status}: ${response.statusText}`);
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
const blob = await response.blob();
|
|
1017
|
+
downloadByBlob(blob, fileName);
|
|
1018
|
+
} catch (error) {
|
|
1019
|
+
downloadByUrl(url, fileName);
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
/**
|
|
1024
|
+
* 简单、高性能的通用事件总线。
|
|
1025
|
+
* - 支持命名空间事件
|
|
1026
|
+
* - 支持一次性监听器
|
|
1027
|
+
* - 返回唯一 flag,用于精确卸载
|
|
1028
|
+
* - emit 时可选自定义 this 指向
|
|
1029
|
+
*/
|
|
1030
|
+
class MyEvent {
|
|
1031
|
+
constructor() {
|
|
1032
|
+
this.evtPool = new Map();
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
/**
|
|
1036
|
+
* 注册事件监听器。
|
|
1037
|
+
* @param {string} name - 事件名
|
|
1038
|
+
* @param {Function} fn - 回调函数
|
|
1039
|
+
* @returns {string} flag - 唯一标识,用于 off
|
|
1040
|
+
*/
|
|
1041
|
+
on(name, fn) {
|
|
1042
|
+
let flag = Date.now() + "_" + parseInt(Math.random() * 1e8);
|
|
1043
|
+
const evtItem = {
|
|
1044
|
+
flag,
|
|
1045
|
+
fn
|
|
1046
|
+
};
|
|
1047
|
+
if (this.evtPool.has(name)) {
|
|
1048
|
+
this.evtPool.get(name).push(evtItem);
|
|
1049
|
+
} else {
|
|
1050
|
+
this.evtPool.set(name, [evtItem]);
|
|
1051
|
+
}
|
|
1052
|
+
return flag;
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
/**
|
|
1056
|
+
* 注册一次性监听器,触发后自动移除。
|
|
1057
|
+
* @param {string} name - 事件名
|
|
1058
|
+
* @param {Function} fn - 回调函数
|
|
1059
|
+
* @returns {string} flag - 唯一标识
|
|
1060
|
+
*/
|
|
1061
|
+
once(name, fn) {
|
|
1062
|
+
const _this = this;
|
|
1063
|
+
let wrapper;
|
|
1064
|
+
wrapper = function (data) {
|
|
1065
|
+
_this.off(name, wrapper);
|
|
1066
|
+
fn.call(this, data);
|
|
1067
|
+
};
|
|
1068
|
+
return this.on(name, wrapper);
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
/**
|
|
1072
|
+
* 移除指定事件监听器。
|
|
1073
|
+
* @param {string} name - 事件名
|
|
1074
|
+
* @param {Function|string} fnOrFlag - 回调函数或 flag
|
|
1075
|
+
*/
|
|
1076
|
+
off(name, fnOrFlag) {
|
|
1077
|
+
if (!this.evtPool.has(name)) return;
|
|
1078
|
+
const evtItems = this.evtPool.get(name);
|
|
1079
|
+
const filtered = evtItems.filter((item) => item.fn !== fnOrFlag && item.flag !== fnOrFlag);
|
|
1080
|
+
if (filtered.length === 0) {
|
|
1081
|
+
this.evtPool.delete(name);
|
|
1082
|
+
} else {
|
|
1083
|
+
this.evtPool.set(name, filtered);
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
/**
|
|
1088
|
+
* 触发事件(同步执行)。
|
|
1089
|
+
* @param {string} name - 事件名
|
|
1090
|
+
* @param {*} [data] - 任意载荷
|
|
1091
|
+
* @param {*} [fnThis] - 回调内部 this 指向,默认 undefined
|
|
1092
|
+
*/
|
|
1093
|
+
emit(name, data, fnThis) {
|
|
1094
|
+
if (!this.evtPool.has(name)) return;
|
|
1095
|
+
const evtItems = this.evtPool.get(name);
|
|
1096
|
+
evtItems.forEach((item) => {
|
|
1097
|
+
try {
|
|
1098
|
+
item.fn.call(fnThis, data);
|
|
1099
|
+
} catch (err) {
|
|
1100
|
+
console.error(`Error in event listener for "${name}":`, err);
|
|
1101
|
+
}
|
|
1102
|
+
});
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
/**
|
|
1107
|
+
* 跨页通信插件:通过 localStorage + storage 事件将当前实例的 emit 广播到其他同源页面。
|
|
1108
|
+
* 支持节流、命名空间隔离。
|
|
1109
|
+
*/
|
|
1110
|
+
const MyEvent_CrossPagePlugin = (() => {
|
|
1111
|
+
const INSTALLED = new WeakSet(); // 防止重复安装
|
|
1112
|
+
|
|
1113
|
+
return {
|
|
1114
|
+
/**
|
|
1115
|
+
* 为指定 MyEvent 实例安装跨页插件。
|
|
1116
|
+
* @param {MyEvent} bus - 事件总线实例
|
|
1117
|
+
* @param {Options} [opts] - 配置项
|
|
1118
|
+
*/
|
|
1119
|
+
install(bus, opts = {}) {
|
|
1120
|
+
if (INSTALLED.has(bus)) return;
|
|
1121
|
+
INSTALLED.add(bus);
|
|
1122
|
+
|
|
1123
|
+
const ns = `___my-event-cross-page-${opts.namespace || "default"}___`;
|
|
1124
|
+
const delay = opts.throttle || 16;
|
|
1125
|
+
let last = 0;
|
|
1126
|
+
|
|
1127
|
+
// 1、重写 emit
|
|
1128
|
+
const rawEmit = bus.emit;
|
|
1129
|
+
bus.emit = function (name, data, fnThis) {
|
|
1130
|
+
rawEmit.call(bus, name, data, fnThis); // 本地先执行
|
|
1131
|
+
const now = Date.now();
|
|
1132
|
+
if (now - last < delay) return;
|
|
1133
|
+
last = now;
|
|
1134
|
+
const key = ns + name;
|
|
1135
|
+
try {
|
|
1136
|
+
localStorage.setItem(key, JSON.stringify({ name, data, ts: now }));
|
|
1137
|
+
localStorage.removeItem(key); // 触发 storage 事件
|
|
1138
|
+
} catch (e) {}
|
|
1139
|
+
};
|
|
1140
|
+
|
|
1141
|
+
// 2、监听其他页广播
|
|
1142
|
+
function onStorageHandler(e) {
|
|
1143
|
+
if (!e.key || !e.key.startsWith(ns)) return;
|
|
1144
|
+
let payload;
|
|
1145
|
+
try {
|
|
1146
|
+
payload = JSON.parse(e.newValue || "{}");
|
|
1147
|
+
} catch {
|
|
1148
|
+
return;
|
|
1149
|
+
}
|
|
1150
|
+
if (!payload.ts || payload.ts <= last) return;
|
|
1151
|
+
rawEmit.call(bus, e.key.slice(ns.length), payload.data); // 仅本地
|
|
1152
|
+
}
|
|
1153
|
+
addEventListener("storage", onStorageHandler);
|
|
1154
|
+
|
|
1155
|
+
// 3、保存卸载器
|
|
1156
|
+
bus._uninstallCrossPage = () => {
|
|
1157
|
+
removeEventListener("storage", onStorageHandler);
|
|
1158
|
+
bus.emit = rawEmit;
|
|
1159
|
+
INSTALLED.delete(bus);
|
|
1160
|
+
};
|
|
1161
|
+
},
|
|
1162
|
+
|
|
1163
|
+
/**
|
|
1164
|
+
* 卸载插件,恢复原始 emit 并停止监听。
|
|
1165
|
+
* @param {MyEvent} bus - 事件总线实例
|
|
1166
|
+
*/
|
|
1167
|
+
uninstall(bus) {
|
|
1168
|
+
if (typeof bus._uninstallCrossPage === "function") bus._uninstallCrossPage();
|
|
1169
|
+
}
|
|
1170
|
+
};
|
|
853
1171
|
})();
|
|
854
1172
|
|
|
855
|
-
/**
|
|
856
|
-
* 生成 RFC4122 版本 4 的 GUID/UUID。
|
|
857
|
-
* 收集来源:《基于mvc的javascript web富应用开发》 书中介绍是Robert Kieffer写的,还留了网址 http://goo.gl/0b0hu ,但实际访问不了。
|
|
858
|
-
* 格式:`xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`
|
|
859
|
-
*
|
|
860
|
-
* @returns {string} 36 位大写 GUID
|
|
861
|
-
*
|
|
862
|
-
* @example
|
|
863
|
-
* // A2E0F340-6C3B-4D7F-B8C1-1E4F6A8B9C0D
|
|
864
|
-
* console.log(getGUID())
|
|
865
|
-
*/
|
|
866
|
-
function getGUID() {
|
|
867
|
-
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
868
|
-
let r = (Math.random() * 16) | 0,
|
|
869
|
-
v = c == "x" ? r : (r & 0x3) | 0x8;
|
|
870
|
-
return v.toString(16).toUpperCase();
|
|
871
|
-
});
|
|
872
|
-
}
|
|
873
|
-
|
|
874
|
-
/**
|
|
875
|
-
* 分布式短 ID 生成器。
|
|
876
|
-
* 格式:`${timestamp}${flag}${serial}`,其中:
|
|
877
|
-
* - timestamp:毫秒级时间戳
|
|
878
|
-
* - flag:客户端标识串(自定义)
|
|
879
|
-
* - serial:同一毫秒内的序号,左补零到固定长度
|
|
880
|
-
*/
|
|
881
|
-
class MyId {
|
|
882
|
-
#ts = Date.now(); // 时间戳
|
|
883
|
-
#sn = 0; // 序号(保证同一客户端之间的唯一项)
|
|
884
|
-
#flag = ""; // 客户端标识(保证不同客户端之间的唯一项)
|
|
885
|
-
#len = 5; // 序号位长度(我的电脑测试,同一时间戳内可以for循环执行了1000次左右,没有一次超过3k,所以5位应该够用了)
|
|
886
|
-
// 测试代码
|
|
887
|
-
// let obj = {[Date.now()]:[]}; try { for (let i = 0; i < 100000; i++) { obj[Date.now()].push(i); } } catch { console.log(obj[Object.getOwnPropertyNames(obj)[0]].length); }
|
|
888
|
-
|
|
889
|
-
/**
|
|
890
|
-
* @param {object} [option]
|
|
891
|
-
* @param {string} [option.flag] - 客户端标识,默认空串
|
|
892
|
-
* @param {number} [option.len=5] - 序号位长度(位数),安全范围 ≥0
|
|
893
|
-
*/
|
|
894
|
-
constructor(option = {}) {
|
|
895
|
-
if (option) {
|
|
896
|
-
if (typeof option.flag === "string") {
|
|
897
|
-
this.#flag = option.flag;
|
|
898
|
-
}
|
|
899
|
-
if (Number.isSafeInteger(option.len) && len >= 0) {
|
|
900
|
-
this.#len = option.len;
|
|
901
|
-
}
|
|
902
|
-
}
|
|
903
|
-
}
|
|
904
|
-
|
|
905
|
-
/**
|
|
906
|
-
* 生成下一个全局唯一字符串 ID。
|
|
907
|
-
* 同一毫秒序号自动递增;序号溢出时会在控制台警告。
|
|
908
|
-
* @returns {string}
|
|
909
|
-
*/
|
|
910
|
-
nextId() {
|
|
911
|
-
let ts = Date.now();
|
|
912
|
-
if (ts === this.#ts) {
|
|
913
|
-
this.#sn++;
|
|
914
|
-
if (this.#sn >= 10 ** this.#len) {
|
|
915
|
-
console.log("长度不够用了!!!");
|
|
916
|
-
}
|
|
917
|
-
} else {
|
|
918
|
-
this.#sn = 0;
|
|
919
|
-
this.#ts = ts;
|
|
920
|
-
}
|
|
921
|
-
return ts.toString() + this.#flag + this.#sn.toString().padStart(this.#len, "0");
|
|
922
|
-
}
|
|
1173
|
+
/**
|
|
1174
|
+
* 生成 RFC4122 版本 4 的 GUID/UUID。
|
|
1175
|
+
* 收集来源:《基于mvc的javascript web富应用开发》 书中介绍是Robert Kieffer写的,还留了网址 http://goo.gl/0b0hu ,但实际访问不了。
|
|
1176
|
+
* 格式:`xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`
|
|
1177
|
+
*
|
|
1178
|
+
* @returns {string} 36 位大写 GUID
|
|
1179
|
+
*
|
|
1180
|
+
* @example
|
|
1181
|
+
* // A2E0F340-6C3B-4D7F-B8C1-1E4F6A8B9C0D
|
|
1182
|
+
* console.log(getGUID())
|
|
1183
|
+
*/
|
|
1184
|
+
function getGUID() {
|
|
1185
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
1186
|
+
let r = (Math.random() * 16) | 0,
|
|
1187
|
+
v = c == "x" ? r : (r & 0x3) | 0x8;
|
|
1188
|
+
return v.toString(16).toUpperCase();
|
|
1189
|
+
});
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
/**
|
|
1193
|
+
* 分布式短 ID 生成器。
|
|
1194
|
+
* 格式:`${timestamp}${flag}${serial}`,其中:
|
|
1195
|
+
* - timestamp:毫秒级时间戳
|
|
1196
|
+
* - flag:客户端标识串(自定义)
|
|
1197
|
+
* - serial:同一毫秒内的序号,左补零到固定长度
|
|
1198
|
+
*/
|
|
1199
|
+
class MyId {
|
|
1200
|
+
#ts = Date.now(); // 时间戳
|
|
1201
|
+
#sn = 0; // 序号(保证同一客户端之间的唯一项)
|
|
1202
|
+
#flag = ""; // 客户端标识(保证不同客户端之间的唯一项)
|
|
1203
|
+
#len = 5; // 序号位长度(我的电脑测试,同一时间戳内可以for循环执行了1000次左右,没有一次超过3k,所以5位应该够用了)
|
|
1204
|
+
// 测试代码
|
|
1205
|
+
// let obj = {[Date.now()]:[]}; try { for (let i = 0; i < 100000; i++) { obj[Date.now()].push(i); } } catch { console.log(obj[Object.getOwnPropertyNames(obj)[0]].length); }
|
|
1206
|
+
|
|
1207
|
+
/**
|
|
1208
|
+
* @param {object} [option]
|
|
1209
|
+
* @param {string} [option.flag] - 客户端标识,默认空串
|
|
1210
|
+
* @param {number} [option.len=5] - 序号位长度(位数),安全范围 ≥0
|
|
1211
|
+
*/
|
|
1212
|
+
constructor(option = {}) {
|
|
1213
|
+
if (option) {
|
|
1214
|
+
if (typeof option.flag === "string") {
|
|
1215
|
+
this.#flag = option.flag;
|
|
1216
|
+
}
|
|
1217
|
+
if (Number.isSafeInteger(option.len) && len >= 0) {
|
|
1218
|
+
this.#len = option.len;
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
/**
|
|
1224
|
+
* 生成下一个全局唯一字符串 ID。
|
|
1225
|
+
* 同一毫秒序号自动递增;序号溢出时会在控制台警告。
|
|
1226
|
+
* @returns {string}
|
|
1227
|
+
*/
|
|
1228
|
+
nextId() {
|
|
1229
|
+
let ts = Date.now();
|
|
1230
|
+
if (ts === this.#ts) {
|
|
1231
|
+
this.#sn++;
|
|
1232
|
+
if (this.#sn >= 10 ** this.#len) {
|
|
1233
|
+
console.log("长度不够用了!!!");
|
|
1234
|
+
}
|
|
1235
|
+
} else {
|
|
1236
|
+
this.#sn = 0;
|
|
1237
|
+
this.#ts = ts;
|
|
1238
|
+
}
|
|
1239
|
+
return ts.toString() + this.#flag + this.#sn.toString().padStart(this.#len, "0");
|
|
1240
|
+
}
|
|
923
1241
|
}
|
|
924
1242
|
|
|
925
1243
|
/**
|
|
@@ -927,7 +1245,6 @@ class MyId {
|
|
|
927
1245
|
* 每次任务执行完成后才计算下一次间隔,避免任务堆积。
|
|
928
1246
|
*/
|
|
929
1247
|
class IntervalTimer {
|
|
930
|
-
|
|
931
1248
|
/**
|
|
932
1249
|
* 创建定时器实例。
|
|
933
1250
|
* @param {() => void} fn - 每次间隔要执行的业务函数
|
|
@@ -1068,10 +1385,491 @@ const findObjAttrValueById = function findObjAttrValueByIdFn(id, arr, resultKey
|
|
|
1068
1385
|
}
|
|
1069
1386
|
};
|
|
1070
1387
|
|
|
1388
|
+
/**
|
|
1389
|
+
* 从服务端返回的已选 id 数组里,提取出
|
|
1390
|
+
* 1. 叶子节点
|
|
1391
|
+
* 2. 所有子节点都被选中的父节点
|
|
1392
|
+
* 其余父节点一律丢弃(由前端 Tree 自动算半选)
|
|
1393
|
+
*
|
|
1394
|
+
* @param {Array} treeData 完整树
|
|
1395
|
+
* @param {Array} selectedKeys 后端给的选中 id 数组
|
|
1396
|
+
* @param {String} idKey 节点唯一字段
|
|
1397
|
+
* @param {String} childrenKey 子节点字段
|
|
1398
|
+
* @returns {{checked: string[], halfChecked: string[]}}
|
|
1399
|
+
*/
|
|
1400
|
+
function extractFullyCheckedKeys(treeData, selectedKeys, idKey = "id", childrenKey = "children") {
|
|
1401
|
+
const selectedSet = new Set(selectedKeys);
|
|
1402
|
+
const checked = new Set();
|
|
1403
|
+
const halfChecked = new Set();
|
|
1404
|
+
|
|
1405
|
+
/* 返回值含义
|
|
1406
|
+
0 - 未选中
|
|
1407
|
+
1 - 半选
|
|
1408
|
+
2 - 全选
|
|
1409
|
+
*/
|
|
1410
|
+
function dfs(node) {
|
|
1411
|
+
const nodeId = node[idKey];
|
|
1412
|
+
const children = node[childrenKey] || [];
|
|
1413
|
+
|
|
1414
|
+
// 叶子
|
|
1415
|
+
if (!children.length) {
|
|
1416
|
+
if (selectedSet.has(nodeId)) {
|
|
1417
|
+
checked.add(nodeId);
|
|
1418
|
+
return 2;
|
|
1419
|
+
}
|
|
1420
|
+
return 0;
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
// 非叶子
|
|
1424
|
+
let allChecked = true;
|
|
1425
|
+
let someChecked = false;
|
|
1426
|
+
|
|
1427
|
+
children.forEach((child) => {
|
|
1428
|
+
const childState = dfs(child);
|
|
1429
|
+
if (childState !== 2) allChecked = false;
|
|
1430
|
+
if (childState >= 1) someChecked = true;
|
|
1431
|
+
});
|
|
1432
|
+
|
|
1433
|
+
// 当前节点本身在 selectedKeys 里,但子节点未全选 → 只能算半选
|
|
1434
|
+
if (selectedSet.has(nodeId)) {
|
|
1435
|
+
if (allChecked) {
|
|
1436
|
+
checked.add(nodeId);
|
|
1437
|
+
return 2;
|
|
1438
|
+
}
|
|
1439
|
+
halfChecked.add(nodeId);
|
|
1440
|
+
return 1;
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
// 当前节点不在 selectedKeys 里,看子节点
|
|
1444
|
+
if (allChecked) {
|
|
1445
|
+
checked.add(nodeId);
|
|
1446
|
+
return 2;
|
|
1447
|
+
}
|
|
1448
|
+
if (someChecked) {
|
|
1449
|
+
halfChecked.add(nodeId);
|
|
1450
|
+
return 1;
|
|
1451
|
+
}
|
|
1452
|
+
return 0;
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
treeData.forEach(dfs);
|
|
1456
|
+
return {
|
|
1457
|
+
checked: [...checked],
|
|
1458
|
+
halfChecked: [...halfChecked]
|
|
1459
|
+
};
|
|
1460
|
+
}
|
|
1461
|
+
|
|
1462
|
+
/**
|
|
1463
|
+
* @class WebSocketManager - 一个纯粹、强大的 WebSocket 连接管理引擎
|
|
1464
|
+
*
|
|
1465
|
+
* @features
|
|
1466
|
+
* - 智能断线重连 (指数退避算法)
|
|
1467
|
+
* - 网络状态监听
|
|
1468
|
+
* - 高度可定制的心跳保活机制 (通过注入函数实现)
|
|
1469
|
+
* - 页面可见性 API 集成
|
|
1470
|
+
* - 消息发送队列
|
|
1471
|
+
* - 清晰的生命周期管理
|
|
1472
|
+
* - 优雅的资源销毁
|
|
1473
|
+
* - 可配置的数据序列化/反序列化
|
|
1474
|
+
* - 纯粹的消息传递,不关心消息内容
|
|
1475
|
+
*/
|
|
1476
|
+
class WebSocketManager {
|
|
1477
|
+
/**
|
|
1478
|
+
* @param {string} url - WebSocket 服务器的地址
|
|
1479
|
+
* @param {object} [options={}] - 配置选项
|
|
1480
|
+
* @param {number} [options.heartbeatInterval=30000] - 心跳间隔 (毫秒)
|
|
1481
|
+
* @param {number} [options.heartbeatTimeout=10000] - 心跳超时时间 (毫秒)
|
|
1482
|
+
* @param {number} [options.reconnectBaseInterval=1000] - 重连基础间隔 (毫秒)
|
|
1483
|
+
* @param {number} [options.maxReconnectInterval=30000] - 最大重连间隔 (毫秒)
|
|
1484
|
+
* @param {number} [options.maxReconnectAttempts=Infinity] - 最大重连次数
|
|
1485
|
+
* @param {boolean} [options.autoConnect=true] - 是否在实例化后自动连接
|
|
1486
|
+
* @param {boolean} [options.serializeData=false] - 发送数据时是否自动序列化为JSON字符串
|
|
1487
|
+
* @param {boolean} [options.deserializeData=false] - 接收数据时是否自动反序列化为JSON对象
|
|
1488
|
+
* @param {function|null} [options.getPingMessage=null] - 返回要发送的 ping 消息内容的函数。如果为 null,则不发送心跳。
|
|
1489
|
+
* @param {function|null} [options.isPongMessage=null] - 判断接收到的消息是否为 pong 的函数。接收 MessageEvent 对象作为参数,返回布尔值。
|
|
1490
|
+
* @param {object} [options.protocols] - WebSocket 协议
|
|
1491
|
+
*/
|
|
1492
|
+
constructor(url, options = {}) {
|
|
1493
|
+
this.url = url;
|
|
1494
|
+
|
|
1495
|
+
// 默认的心跳实现,用于向后兼容
|
|
1496
|
+
const defaultGetPingMessage = () => JSON.stringify({ type: "ping" });
|
|
1497
|
+
const defaultIsPongMessage = (event) => {
|
|
1498
|
+
try {
|
|
1499
|
+
const message = JSON.parse(event.data);
|
|
1500
|
+
return message.type === "pong";
|
|
1501
|
+
} catch (e) {
|
|
1502
|
+
return false;
|
|
1503
|
+
}
|
|
1504
|
+
};
|
|
1505
|
+
|
|
1506
|
+
this.options = {
|
|
1507
|
+
heartbeatInterval: 30000,
|
|
1508
|
+
heartbeatTimeout: 10000,
|
|
1509
|
+
reconnectBaseInterval: 1000,
|
|
1510
|
+
maxReconnectInterval: 30000,
|
|
1511
|
+
maxReconnectAttempts: Infinity,
|
|
1512
|
+
autoConnect: true,
|
|
1513
|
+
serializeData: false,
|
|
1514
|
+
deserializeData: false,
|
|
1515
|
+
getPingMessage: defaultGetPingMessage, // 默认提供 ping 消息生成器
|
|
1516
|
+
isPongMessage: defaultIsPongMessage, // 默认提供 pong 消息判断器
|
|
1517
|
+
...options
|
|
1518
|
+
};
|
|
1519
|
+
|
|
1520
|
+
// WebSocket 实例
|
|
1521
|
+
this.ws = null;
|
|
1522
|
+
|
|
1523
|
+
// 状态管理
|
|
1524
|
+
this.readyState = WebSocket.CLOSED;
|
|
1525
|
+
this.reconnectAttempts = 0;
|
|
1526
|
+
this.forcedClose = false;
|
|
1527
|
+
this.isReconnecting = false;
|
|
1528
|
+
|
|
1529
|
+
// 定时器
|
|
1530
|
+
this.heartbeatTimer = null;
|
|
1531
|
+
this.heartbeatTimeoutTimer = null;
|
|
1532
|
+
this.reconnectTimer = null;
|
|
1533
|
+
|
|
1534
|
+
// 消息队列
|
|
1535
|
+
this.messageQueue = [];
|
|
1536
|
+
|
|
1537
|
+
// 事件监听器
|
|
1538
|
+
this.listeners = new Map();
|
|
1539
|
+
|
|
1540
|
+
// 绑定方法上下文
|
|
1541
|
+
this._onOpen = this._onOpen.bind(this);
|
|
1542
|
+
this._onMessage = this._onMessage.bind(this);
|
|
1543
|
+
this._onClose = this._onClose.bind(this);
|
|
1544
|
+
this._onError = this._onError.bind(this);
|
|
1545
|
+
this._handleVisibilityChange = this._handleVisibilityChange.bind(this);
|
|
1546
|
+
this._handleOnline = this._handleOnline.bind(this);
|
|
1547
|
+
this._handleOffline = this._handleOffline.bind(this);
|
|
1548
|
+
|
|
1549
|
+
this._setupEventListeners();
|
|
1550
|
+
|
|
1551
|
+
if (this.options.autoConnect) {
|
|
1552
|
+
this.connect();
|
|
1553
|
+
}
|
|
1554
|
+
}
|
|
1555
|
+
|
|
1556
|
+
// --- 公共 API ---
|
|
1557
|
+
|
|
1558
|
+
/**
|
|
1559
|
+
* 连接到 WebSocket 服务器
|
|
1560
|
+
*/
|
|
1561
|
+
connect() {
|
|
1562
|
+
if (this.ws && (this.ws.readyState === WebSocket.CONNECTING || this.ws.readyState === WebSocket.OPEN)) {
|
|
1563
|
+
return;
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
this.forcedClose = false;
|
|
1567
|
+
this._updateReadyState(WebSocket.CONNECTING);
|
|
1568
|
+
console.log(`[WS] 正在连接到 ${this.url}...`);
|
|
1569
|
+
this._emit("connecting");
|
|
1570
|
+
|
|
1571
|
+
try {
|
|
1572
|
+
this.ws = new WebSocket(this.url, this.options.protocols);
|
|
1573
|
+
this.ws.onopen = this._onOpen;
|
|
1574
|
+
this.ws.onmessage = this._onMessage;
|
|
1575
|
+
this.ws.onclose = this._onClose;
|
|
1576
|
+
this.ws.onerror = this._onError;
|
|
1577
|
+
} catch (error) {
|
|
1578
|
+
console.error("[WS] 连接失败:", error);
|
|
1579
|
+
this._onError(error);
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1582
|
+
|
|
1583
|
+
/**
|
|
1584
|
+
* 发送数据
|
|
1585
|
+
* @param {string|object|ArrayBuffer|Blob} data - 要发送的数据
|
|
1586
|
+
*/
|
|
1587
|
+
send(data) {
|
|
1588
|
+
if (this.readyState === WebSocket.OPEN) {
|
|
1589
|
+
let message;
|
|
1590
|
+
// 根据配置决定是否序列化数据
|
|
1591
|
+
if (this.options.serializeData) {
|
|
1592
|
+
message = JSON.stringify(data);
|
|
1593
|
+
} else {
|
|
1594
|
+
message = data;
|
|
1595
|
+
}
|
|
1596
|
+
this.ws.send(message);
|
|
1597
|
+
console.log("[WS] 消息已发送:", message);
|
|
1598
|
+
} else {
|
|
1599
|
+
console.warn("[WS] 连接未打开,消息已加入队列:", data);
|
|
1600
|
+
this.messageQueue.push(data);
|
|
1601
|
+
}
|
|
1602
|
+
}
|
|
1603
|
+
|
|
1604
|
+
/**
|
|
1605
|
+
* 主动关闭连接
|
|
1606
|
+
* @param {number} [code=1000] - 关闭代码
|
|
1607
|
+
* @param {string} [reason=''] - 关闭原因
|
|
1608
|
+
*/
|
|
1609
|
+
close(code = 1000, reason = "Normal closure") {
|
|
1610
|
+
this.forcedClose = true;
|
|
1611
|
+
this._stopHeartbeat();
|
|
1612
|
+
this._clearReconnectTimer();
|
|
1613
|
+
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
1614
|
+
this.ws.close(code, reason);
|
|
1615
|
+
} else {
|
|
1616
|
+
this._updateReadyState(WebSocket.CLOSED);
|
|
1617
|
+
this._emit("close", { code, reason, wasClean: true });
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
|
|
1621
|
+
/**
|
|
1622
|
+
* 彻底销毁实例,清理所有资源
|
|
1623
|
+
*/
|
|
1624
|
+
destroy() {
|
|
1625
|
+
console.log("[WS] 正在销毁实例...");
|
|
1626
|
+
this.close(1000, "Instance destroyed");
|
|
1627
|
+
this._removeEventListeners();
|
|
1628
|
+
this.messageQueue = [];
|
|
1629
|
+
this.listeners.clear();
|
|
1630
|
+
this.ws = null;
|
|
1631
|
+
}
|
|
1632
|
+
|
|
1633
|
+
/**
|
|
1634
|
+
* 添加事件监听器
|
|
1635
|
+
* @param {string} eventName - 事件名 (e.g., 'open', 'message', 'close', 'error', 'reconnect')
|
|
1636
|
+
* @param {function} callback - 回调函数
|
|
1637
|
+
*/
|
|
1638
|
+
on(eventName, callback) {
|
|
1639
|
+
if (!this.listeners.has(eventName)) {
|
|
1640
|
+
this.listeners.set(eventName, []);
|
|
1641
|
+
}
|
|
1642
|
+
this.listeners.get(eventName).push(callback);
|
|
1643
|
+
}
|
|
1644
|
+
|
|
1645
|
+
/**
|
|
1646
|
+
* 移除事件监听器
|
|
1647
|
+
* @param {string} eventName - 事件名
|
|
1648
|
+
* @param {function} callback - 回调函数
|
|
1649
|
+
*/
|
|
1650
|
+
off(eventName, callback) {
|
|
1651
|
+
if (this.listeners.has(eventName)) {
|
|
1652
|
+
const callbacks = this.listeners.get(eventName);
|
|
1653
|
+
const index = callbacks.indexOf(callback);
|
|
1654
|
+
if (index > -1) {
|
|
1655
|
+
callbacks.splice(index, 1);
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
|
|
1660
|
+
// --- 内部方法 ---
|
|
1661
|
+
|
|
1662
|
+
_onOpen(event) {
|
|
1663
|
+
console.log("[WS] 连接已建立");
|
|
1664
|
+
this._updateReadyState(WebSocket.OPEN);
|
|
1665
|
+
this.reconnectAttempts = 0;
|
|
1666
|
+
this.isReconnecting = false;
|
|
1667
|
+
|
|
1668
|
+
// 如果配置了 getPingMessage,则启动心跳
|
|
1669
|
+
if (this.options.getPingMessage) {
|
|
1670
|
+
this._startHeartbeat();
|
|
1671
|
+
}
|
|
1672
|
+
|
|
1673
|
+
this._flushMessageQueue();
|
|
1674
|
+
this._emit("open", event);
|
|
1675
|
+
}
|
|
1676
|
+
|
|
1677
|
+
/**
|
|
1678
|
+
* 纯粹的消息处理方法:处理心跳,然后将数据交给使用者
|
|
1679
|
+
* @param {MessageEvent} event
|
|
1680
|
+
*/
|
|
1681
|
+
_onMessage(event) {
|
|
1682
|
+
// --- 步骤 1: 使用注入的函数优先处理内部心跳机制 ---
|
|
1683
|
+
if (this.options.isPongMessage && this.options.isPongMessage(event)) {
|
|
1684
|
+
this._handlePong();
|
|
1685
|
+
return; // 是心跳消息,处理完毕,直接返回
|
|
1686
|
+
}
|
|
1687
|
+
|
|
1688
|
+
// --- 步骤 2: 根据用户配置处理业务消息 ---
|
|
1689
|
+
if (this.options.deserializeData) {
|
|
1690
|
+
try {
|
|
1691
|
+
const parsedMessage = JSON.parse(event.data);
|
|
1692
|
+
this._emit("message", parsedMessage, event);
|
|
1693
|
+
} catch (e) {
|
|
1694
|
+
this._emit("message", event.data, event);
|
|
1695
|
+
}
|
|
1696
|
+
} else {
|
|
1697
|
+
this._emit("message", event.data, event);
|
|
1698
|
+
}
|
|
1699
|
+
}
|
|
1700
|
+
|
|
1701
|
+
_onClose(event) {
|
|
1702
|
+
console.log("[WS] 连接已关闭", event);
|
|
1703
|
+
this._updateReadyState(WebSocket.CLOSED);
|
|
1704
|
+
this._stopHeartbeat();
|
|
1705
|
+
this._emit("close", event);
|
|
1706
|
+
|
|
1707
|
+
if (!this.forcedClose) {
|
|
1708
|
+
this._scheduleReconnect();
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
|
|
1712
|
+
_onError(event) {
|
|
1713
|
+
console.error("[WS] 连接发生错误:", event);
|
|
1714
|
+
this._emit("error", event);
|
|
1715
|
+
}
|
|
1716
|
+
|
|
1717
|
+
// --- 心跳机制 ---
|
|
1718
|
+
_startHeartbeat() {
|
|
1719
|
+
// 如果没有配置 getPingMessage,则无法启动心跳
|
|
1720
|
+
if (!this.options.getPingMessage) {
|
|
1721
|
+
return;
|
|
1722
|
+
}
|
|
1723
|
+
|
|
1724
|
+
this._stopHeartbeat();
|
|
1725
|
+
this.heartbeatTimer = setInterval(() => {
|
|
1726
|
+
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
1727
|
+
try {
|
|
1728
|
+
// 调用注入的函数获取消息内容并发送
|
|
1729
|
+
const pingMessage = this.options.getPingMessage();
|
|
1730
|
+
this.ws.send(pingMessage);
|
|
1731
|
+
console.log("[WS] 发送 Ping:", pingMessage);
|
|
1732
|
+
this._setHeartbeatTimeout();
|
|
1733
|
+
} catch (error) {
|
|
1734
|
+
console.error("[WS] 发送 Ping 消息失败:", error);
|
|
1735
|
+
}
|
|
1736
|
+
}
|
|
1737
|
+
}, this.options.heartbeatInterval);
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1740
|
+
_stopHeartbeat() {
|
|
1741
|
+
if (this.heartbeatTimer) {
|
|
1742
|
+
clearInterval(this.heartbeatTimer);
|
|
1743
|
+
this.heartbeatTimer = null;
|
|
1744
|
+
}
|
|
1745
|
+
this._clearHeartbeatTimeout();
|
|
1746
|
+
}
|
|
1747
|
+
|
|
1748
|
+
_setHeartbeatTimeout() {
|
|
1749
|
+
this._clearHeartbeatTimeout();
|
|
1750
|
+
this.heartbeatTimeoutTimer = setTimeout(() => {
|
|
1751
|
+
console.error("[WS] 心跳超时,主动断开连接");
|
|
1752
|
+
this.ws.close(1006, "Heartbeat timeout");
|
|
1753
|
+
}, this.options.heartbeatTimeout);
|
|
1754
|
+
}
|
|
1755
|
+
|
|
1756
|
+
_clearHeartbeatTimeout() {
|
|
1757
|
+
if (this.heartbeatTimeoutTimer) {
|
|
1758
|
+
clearTimeout(this.heartbeatTimeoutTimer);
|
|
1759
|
+
this.heartbeatTimeoutTimer = null;
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
|
|
1763
|
+
_handlePong() {
|
|
1764
|
+
console.log("[WS] 收到 Pong");
|
|
1765
|
+
this._clearHeartbeatTimeout();
|
|
1766
|
+
}
|
|
1767
|
+
|
|
1768
|
+
// --- 重连机制 ---
|
|
1769
|
+
_scheduleReconnect() {
|
|
1770
|
+
if (this.forcedClose || this.isReconnecting || this.reconnectAttempts >= this.options.maxReconnectAttempts) {
|
|
1771
|
+
if (this.reconnectAttempts >= this.options.maxReconnectAttempts) {
|
|
1772
|
+
console.error("[WS] 已达到最大重连次数,停止重连");
|
|
1773
|
+
this._emit("reconnect-failed");
|
|
1774
|
+
}
|
|
1775
|
+
return;
|
|
1776
|
+
}
|
|
1777
|
+
|
|
1778
|
+
this.isReconnecting = true;
|
|
1779
|
+
const interval = Math.min(this.options.reconnectBaseInterval * Math.pow(2, this.reconnectAttempts), this.options.maxReconnectInterval);
|
|
1780
|
+
|
|
1781
|
+
console.log(`[WS] ${interval / 1000}秒后将尝试第 ${this.reconnectAttempts + 1} 次重连...`);
|
|
1782
|
+
this._emit("reconnect-attempt", { attempt: this.reconnectAttempts + 1, interval });
|
|
1783
|
+
|
|
1784
|
+
this.reconnectTimer = setTimeout(() => {
|
|
1785
|
+
this.reconnectAttempts++;
|
|
1786
|
+
this.connect();
|
|
1787
|
+
}, interval);
|
|
1788
|
+
}
|
|
1789
|
+
|
|
1790
|
+
_clearReconnectTimer() {
|
|
1791
|
+
if (this.reconnectTimer) {
|
|
1792
|
+
clearTimeout(this.reconnectTimer);
|
|
1793
|
+
this.reconnectTimer = null;
|
|
1794
|
+
}
|
|
1795
|
+
}
|
|
1796
|
+
|
|
1797
|
+
// --- 消息队列 ---
|
|
1798
|
+
_flushMessageQueue() {
|
|
1799
|
+
if (this.messageQueue.length === 0) return;
|
|
1800
|
+
console.log(`[WS] 发送队列中的 ${this.messageQueue.length} 条消息`);
|
|
1801
|
+
const queue = [...this.messageQueue];
|
|
1802
|
+
this.messageQueue = [];
|
|
1803
|
+
queue.forEach((data) => this.send(data));
|
|
1804
|
+
}
|
|
1805
|
+
|
|
1806
|
+
// --- 事件系统 ---
|
|
1807
|
+
_emit(eventName, ...args) {
|
|
1808
|
+
if (this.listeners.has(eventName)) {
|
|
1809
|
+
this.listeners.get(eventName).forEach((callback) => callback(...args));
|
|
1810
|
+
}
|
|
1811
|
+
}
|
|
1812
|
+
|
|
1813
|
+
// --- 状态与监听器管理 ---
|
|
1814
|
+
_updateReadyState(newState) {
|
|
1815
|
+
this.readyState = newState;
|
|
1816
|
+
this._emit("ready-state-change", newState);
|
|
1817
|
+
}
|
|
1818
|
+
|
|
1819
|
+
_setupEventListeners() {
|
|
1820
|
+
document.addEventListener("visibilitychange", this._handleVisibilityChange);
|
|
1821
|
+
window.addEventListener("online", this._handleOnline);
|
|
1822
|
+
window.addEventListener("offline", this._handleOffline);
|
|
1823
|
+
}
|
|
1824
|
+
|
|
1825
|
+
_removeEventListeners() {
|
|
1826
|
+
document.removeEventListener("visibilitychange", this._handleVisibilityChange);
|
|
1827
|
+
window.removeEventListener("online", this._handleOnline);
|
|
1828
|
+
window.removeEventListener("offline", this._handleOffline);
|
|
1829
|
+
}
|
|
1830
|
+
|
|
1831
|
+
_handleVisibilityChange() {
|
|
1832
|
+
// 如果未启用心跳,不需要处理页面可见性变化
|
|
1833
|
+
if (!this.options.getPingMessage) {
|
|
1834
|
+
return;
|
|
1835
|
+
}
|
|
1836
|
+
|
|
1837
|
+
if (document.hidden) {
|
|
1838
|
+
console.log("[WS] 页面隐藏,停止心跳");
|
|
1839
|
+
this._stopHeartbeat();
|
|
1840
|
+
} else {
|
|
1841
|
+
console.log("[WS] 页面可见,检查连接状态");
|
|
1842
|
+
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
1843
|
+
this._startHeartbeat();
|
|
1844
|
+
} else if (!this.forcedClose && !this.isReconnecting) {
|
|
1845
|
+
this.connect();
|
|
1846
|
+
}
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1849
|
+
|
|
1850
|
+
_handleOnline() {
|
|
1851
|
+
console.log("[WS] 网络已恢复,尝试重连");
|
|
1852
|
+
if (!this.forcedClose && this.readyState !== WebSocket.OPEN) {
|
|
1853
|
+
this._clearReconnectTimer(); // 清除当前的重连计划
|
|
1854
|
+
this.connect(); // 立即尝试连接
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
|
|
1858
|
+
_handleOffline() {
|
|
1859
|
+
console.log("[WS] 网络已断开");
|
|
1860
|
+
this._clearReconnectTimer(); // 停止重连尝试
|
|
1861
|
+
// ws.onclose 会被触发,从而启动重连逻辑,但我们已经停止了
|
|
1862
|
+
// 所以这里可以手动触发一次 close 事件来通知应用层
|
|
1863
|
+
if (this.ws) this.ws.onclose({ code: 1006, reason: "Network offline" });
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
|
|
1867
|
+
exports.AudioStreamResampler = AudioStreamResampler;
|
|
1071
1868
|
exports.IntervalTimer = IntervalTimer;
|
|
1072
1869
|
exports.MyEvent = MyEvent;
|
|
1073
1870
|
exports.MyEvent_CrossPagePlugin = MyEvent_CrossPagePlugin;
|
|
1074
1871
|
exports.MyId = MyId;
|
|
1872
|
+
exports.WebSocketManager = WebSocketManager;
|
|
1075
1873
|
exports.assignExisting = assignExisting;
|
|
1076
1874
|
exports.calcTimeDifference = calcTimeDifference;
|
|
1077
1875
|
exports.debounce = debounce;
|
|
@@ -1081,6 +1879,8 @@ exports.downloadByData = downloadByData;
|
|
|
1081
1879
|
exports.downloadByUrl = downloadByUrl;
|
|
1082
1880
|
exports.downloadExcel = downloadExcel;
|
|
1083
1881
|
exports.downloadJSON = downloadJSON;
|
|
1882
|
+
exports.extractFullyCheckedKeys = extractFullyCheckedKeys;
|
|
1883
|
+
exports.fetchOrDownloadByUrl = fetchOrDownloadByUrl;
|
|
1084
1884
|
exports.findObjAttrValueById = findObjAttrValueById;
|
|
1085
1885
|
exports.flatCompleteTree2NestedTree = flatCompleteTree2NestedTree;
|
|
1086
1886
|
exports.formatDuration = formatDuration;
|
|
@@ -1100,6 +1900,7 @@ exports.isPlainObject = isPlainObject;
|
|
|
1100
1900
|
exports.isPromise = isPromise;
|
|
1101
1901
|
exports.moveItem = moveItem;
|
|
1102
1902
|
exports.nestedTree2IdMap = nestedTree2IdMap;
|
|
1903
|
+
exports.pcmToWavBlob = pcmToWavBlob;
|
|
1103
1904
|
exports.randomDateInRange = randomDateInRange;
|
|
1104
1905
|
exports.randomEnLetter = randomEnLetter;
|
|
1105
1906
|
exports.randomHan = randomHan;
|