@yh-ui/hooks 0.1.15 → 0.1.17

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/dist/index.cjs CHANGED
@@ -1259,9 +1259,10 @@ function useAiVoice(options = {}) {
1259
1259
  if (e.data.size > 0) chunks.push(e.data);
1260
1260
  };
1261
1261
  recorder.onstop = () => {
1262
- audioBlob.value = new Blob(chunks, { type: "audio/webm" });
1263
- if (isRecording.value === false && chunks.length > 0) {
1264
- options.onStop?.(transcript.value, audioBlob.value);
1262
+ const blob = chunks.length > 0 ? new Blob(chunks, { type: "audio/webm" }) : null;
1263
+ audioBlob.value = blob;
1264
+ if (!isRecording.value) {
1265
+ options.onStop?.(transcript.value, blob);
1265
1266
  }
1266
1267
  };
1267
1268
  mediaRecorder.value = recorder;
@@ -1342,7 +1343,7 @@ function useAiVoice(options = {}) {
1342
1343
  }
1343
1344
  };
1344
1345
  const start = async () => {
1345
- if (isRecording.value) return;
1346
+ if (stream.value) return;
1346
1347
  try {
1347
1348
  transcript.value = "";
1348
1349
  interimTranscript.value = "";
@@ -1367,33 +1368,29 @@ function useAiVoice(options = {}) {
1367
1368
  }
1368
1369
  };
1369
1370
  const stop = () => {
1370
- if (!isRecording.value) return;
1371
+ if (!stream.value) return;
1371
1372
  isRecording.value = false;
1372
- if (stream.value) {
1373
- stream.value.getTracks().forEach((track) => track.stop());
1374
- stream.value = null;
1375
- }
1376
- if (recognition.value) {
1373
+ if (mediaRecorder.value && mediaRecorder.value.state !== "inactive") {
1377
1374
  try {
1378
- recognition.value.stop();
1375
+ mediaRecorder.value.stop();
1379
1376
  } catch {
1380
1377
  }
1381
1378
  }
1382
- if (mediaRecorder.value && mediaRecorder.value.state !== "inactive") {
1379
+ if (recognition.value) {
1383
1380
  try {
1384
- mediaRecorder.value.stop();
1381
+ recognition.value.stop();
1385
1382
  } catch {
1386
1383
  }
1387
1384
  }
1388
- cleanup();
1389
- };
1390
- const cancel = () => {
1391
- if (!isRecording.value) return;
1392
- isRecording.value = false;
1393
1385
  if (stream.value) {
1394
1386
  stream.value.getTracks().forEach((track) => track.stop());
1395
1387
  stream.value = null;
1396
1388
  }
1389
+ cleanup();
1390
+ };
1391
+ const cancel = () => {
1392
+ if (!isRecording.value && !stream.value) return;
1393
+ isRecording.value = false;
1397
1394
  if (recognition.value) {
1398
1395
  try {
1399
1396
  recognition.value.abort();
@@ -1406,6 +1403,10 @@ function useAiVoice(options = {}) {
1406
1403
  } catch {
1407
1404
  }
1408
1405
  }
1406
+ if (stream.value) {
1407
+ stream.value.getTracks().forEach((track) => track.stop());
1408
+ stream.value = null;
1409
+ }
1409
1410
  cleanup();
1410
1411
  };
1411
1412
  const cleanup = () => {
package/dist/index.mjs CHANGED
@@ -1243,9 +1243,10 @@ function useAiVoice(options = {}) {
1243
1243
  if (e.data.size > 0) chunks.push(e.data);
1244
1244
  };
1245
1245
  recorder.onstop = () => {
1246
- audioBlob.value = new Blob(chunks, { type: "audio/webm" });
1247
- if (isRecording.value === false && chunks.length > 0) {
1248
- options.onStop?.(transcript.value, audioBlob.value);
1246
+ const blob = chunks.length > 0 ? new Blob(chunks, { type: "audio/webm" }) : null;
1247
+ audioBlob.value = blob;
1248
+ if (!isRecording.value) {
1249
+ options.onStop?.(transcript.value, blob);
1249
1250
  }
1250
1251
  };
1251
1252
  mediaRecorder.value = recorder;
@@ -1326,7 +1327,7 @@ function useAiVoice(options = {}) {
1326
1327
  }
1327
1328
  };
1328
1329
  const start = async () => {
1329
- if (isRecording.value) return;
1330
+ if (stream.value) return;
1330
1331
  try {
1331
1332
  transcript.value = "";
1332
1333
  interimTranscript.value = "";
@@ -1351,33 +1352,29 @@ function useAiVoice(options = {}) {
1351
1352
  }
1352
1353
  };
1353
1354
  const stop = () => {
1354
- if (!isRecording.value) return;
1355
+ if (!stream.value) return;
1355
1356
  isRecording.value = false;
1356
- if (stream.value) {
1357
- stream.value.getTracks().forEach((track) => track.stop());
1358
- stream.value = null;
1359
- }
1360
- if (recognition.value) {
1357
+ if (mediaRecorder.value && mediaRecorder.value.state !== "inactive") {
1361
1358
  try {
1362
- recognition.value.stop();
1359
+ mediaRecorder.value.stop();
1363
1360
  } catch {
1364
1361
  }
1365
1362
  }
1366
- if (mediaRecorder.value && mediaRecorder.value.state !== "inactive") {
1363
+ if (recognition.value) {
1367
1364
  try {
1368
- mediaRecorder.value.stop();
1365
+ recognition.value.stop();
1369
1366
  } catch {
1370
1367
  }
1371
1368
  }
1372
- cleanup();
1373
- };
1374
- const cancel = () => {
1375
- if (!isRecording.value) return;
1376
- isRecording.value = false;
1377
1369
  if (stream.value) {
1378
1370
  stream.value.getTracks().forEach((track) => track.stop());
1379
1371
  stream.value = null;
1380
1372
  }
1373
+ cleanup();
1374
+ };
1375
+ const cancel = () => {
1376
+ if (!isRecording.value && !stream.value) return;
1377
+ isRecording.value = false;
1381
1378
  if (recognition.value) {
1382
1379
  try {
1383
1380
  recognition.value.abort();
@@ -1390,6 +1387,10 @@ function useAiVoice(options = {}) {
1390
1387
  } catch {
1391
1388
  }
1392
1389
  }
1390
+ if (stream.value) {
1391
+ stream.value.getTracks().forEach((track) => track.stop());
1392
+ stream.value = null;
1393
+ }
1393
1394
  cleanup();
1394
1395
  };
1395
1396
  const cleanup = () => {
@@ -40,11 +40,12 @@ function useAiVoice(options = {}) {
40
40
  if (e.data.size > 0) chunks.push(e.data);
41
41
  };
42
42
  recorder.onstop = () => {
43
- audioBlob.value = new Blob(chunks, {
43
+ const blob = chunks.length > 0 ? new Blob(chunks, {
44
44
  type: "audio/webm"
45
- });
46
- if (isRecording.value === false && chunks.length > 0) {
47
- options.onStop?.(transcript.value, audioBlob.value);
45
+ }) : null;
46
+ audioBlob.value = blob;
47
+ if (!isRecording.value) {
48
+ options.onStop?.(transcript.value, blob);
48
49
  }
49
50
  };
50
51
  mediaRecorder.value = recorder;
@@ -124,7 +125,7 @@ function useAiVoice(options = {}) {
124
125
  }
125
126
  };
126
127
  const start = async () => {
127
- if (isRecording.value) return;
128
+ if (stream.value) return;
128
129
  try {
129
130
  transcript.value = "";
130
131
  interimTranscript.value = "";
@@ -151,31 +152,27 @@ function useAiVoice(options = {}) {
151
152
  }
152
153
  };
153
154
  const stop = () => {
154
- if (!isRecording.value) return;
155
+ if (!stream.value) return;
155
156
  isRecording.value = false;
156
- if (stream.value) {
157
- stream.value.getTracks().forEach(track => track.stop());
158
- stream.value = null;
157
+ if (mediaRecorder.value && mediaRecorder.value.state !== "inactive") {
158
+ try {
159
+ mediaRecorder.value.stop();
160
+ } catch {}
159
161
  }
160
162
  if (recognition.value) {
161
163
  try {
162
164
  recognition.value.stop();
163
165
  } catch {}
164
166
  }
165
- if (mediaRecorder.value && mediaRecorder.value.state !== "inactive") {
166
- try {
167
- mediaRecorder.value.stop();
168
- } catch {}
167
+ if (stream.value) {
168
+ stream.value.getTracks().forEach(track => track.stop());
169
+ stream.value = null;
169
170
  }
170
171
  cleanup();
171
172
  };
172
173
  const cancel = () => {
173
- if (!isRecording.value) return;
174
+ if (!isRecording.value && !stream.value) return;
174
175
  isRecording.value = false;
175
- if (stream.value) {
176
- stream.value.getTracks().forEach(track => track.stop());
177
- stream.value = null;
178
- }
179
176
  if (recognition.value) {
180
177
  try {
181
178
  recognition.value.abort();
@@ -186,6 +183,10 @@ function useAiVoice(options = {}) {
186
183
  mediaRecorder.value.stop();
187
184
  } catch {}
188
185
  }
186
+ if (stream.value) {
187
+ stream.value.getTracks().forEach(track => track.stop());
188
+ stream.value = null;
189
+ }
189
190
  cleanup();
190
191
  };
191
192
  const cleanup = () => {
@@ -34,9 +34,10 @@ export function useAiVoice(options = {}) {
34
34
  if (e.data.size > 0) chunks.push(e.data);
35
35
  };
36
36
  recorder.onstop = () => {
37
- audioBlob.value = new Blob(chunks, { type: "audio/webm" });
38
- if (isRecording.value === false && chunks.length > 0) {
39
- options.onStop?.(transcript.value, audioBlob.value);
37
+ const blob = chunks.length > 0 ? new Blob(chunks, { type: "audio/webm" }) : null;
38
+ audioBlob.value = blob;
39
+ if (!isRecording.value) {
40
+ options.onStop?.(transcript.value, blob);
40
41
  }
41
42
  };
42
43
  mediaRecorder.value = recorder;
@@ -117,7 +118,7 @@ export function useAiVoice(options = {}) {
117
118
  }
118
119
  };
119
120
  const start = async () => {
120
- if (isRecording.value) return;
121
+ if (stream.value) return;
121
122
  try {
122
123
  transcript.value = "";
123
124
  interimTranscript.value = "";
@@ -142,33 +143,29 @@ export function useAiVoice(options = {}) {
142
143
  }
143
144
  };
144
145
  const stop = () => {
145
- if (!isRecording.value) return;
146
+ if (!stream.value) return;
146
147
  isRecording.value = false;
147
- if (stream.value) {
148
- stream.value.getTracks().forEach((track) => track.stop());
149
- stream.value = null;
150
- }
151
- if (recognition.value) {
148
+ if (mediaRecorder.value && mediaRecorder.value.state !== "inactive") {
152
149
  try {
153
- recognition.value.stop();
150
+ mediaRecorder.value.stop();
154
151
  } catch {
155
152
  }
156
153
  }
157
- if (mediaRecorder.value && mediaRecorder.value.state !== "inactive") {
154
+ if (recognition.value) {
158
155
  try {
159
- mediaRecorder.value.stop();
156
+ recognition.value.stop();
160
157
  } catch {
161
158
  }
162
159
  }
163
- cleanup();
164
- };
165
- const cancel = () => {
166
- if (!isRecording.value) return;
167
- isRecording.value = false;
168
160
  if (stream.value) {
169
161
  stream.value.getTracks().forEach((track) => track.stop());
170
162
  stream.value = null;
171
163
  }
164
+ cleanup();
165
+ };
166
+ const cancel = () => {
167
+ if (!isRecording.value && !stream.value) return;
168
+ isRecording.value = false;
172
169
  if (recognition.value) {
173
170
  try {
174
171
  recognition.value.abort();
@@ -181,6 +178,10 @@ export function useAiVoice(options = {}) {
181
178
  } catch {
182
179
  }
183
180
  }
181
+ if (stream.value) {
182
+ stream.value.getTracks().forEach((track) => track.stop());
183
+ stream.value = null;
184
+ }
184
185
  cleanup();
185
186
  };
186
187
  const cleanup = () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yh-ui/hooks",
3
- "version": "0.1.15",
3
+ "version": "0.1.17",
4
4
  "description": "YH-UI composition hooks",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -24,8 +24,8 @@
24
24
  ],
25
25
  "dependencies": {
26
26
  "dayjs": "^1.11.19",
27
- "@yh-ui/locale": "0.1.15",
28
- "@yh-ui/utils": "0.1.15"
27
+ "@yh-ui/locale": "0.1.17",
28
+ "@yh-ui/utils": "0.1.17"
29
29
  },
30
30
  "devDependencies": {
31
31
  "vue": "^3.5.27",