Skip to content

Commit 46bbba9

Browse files
authored
Merge pull request #250 from JamesBrill/lodash
Replace custom `debounce` function with `lodash.debounce`
2 parents 294ccd0 + 5075729 commit 46bbba9

4 files changed

Lines changed: 16 additions & 23 deletions

File tree

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
"prepack": "bunchee",
3636
"test": "vitest"
3737
},
38+
"dependencies": {
39+
"lodash.debounce": "^4.0.8"
40+
},
3841
"devDependencies": {
3942
"@biomejs/biome": "1.9.4",
4043
"@testing-library/react-hooks": "3.7.0",

src/RecognitionManager.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import debounce from "lodash.debounce";
12
import { isNative } from "./NativeSpeechRecognition";
23
import isAndroid from "./isAndroid";
3-
import { browserSupportsPolyfills, concatTranscripts, debounce } from "./utils";
4+
import { browserSupportsPolyfills, concatTranscripts } from "./utils";
45

56
export default class RecognitionManager {
67
constructor(SpeechRecognition) {
@@ -24,11 +25,9 @@ export default class RecognitionManager {
2425
this.setSpeechRecognition(SpeechRecognition);
2526

2627
if (isAndroid()) {
27-
this.updateFinalTranscript = debounce(
28-
this.updateFinalTranscript,
29-
250,
30-
true,
31-
);
28+
this.updateFinalTranscript = debounce(this.updateFinalTranscript, 250, {
29+
leading: true,
30+
});
3231
}
3332
}
3433

src/utils.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
const debounce = (func, wait, immediate) => {
2-
let timeout;
3-
return function () {
4-
const context = this;
5-
const args = arguments;
6-
const later = function () {
7-
timeout = null;
8-
if (!immediate) func.apply(context, args);
9-
};
10-
const callNow = immediate && !timeout;
11-
clearTimeout(timeout);
12-
timeout = setTimeout(later, wait);
13-
if (callNow) func.apply(context, args);
14-
};
15-
};
16-
171
const concatTranscripts = (...transcriptParts) => {
182
return transcriptParts
193
.map((t) => t.trim())
@@ -87,7 +71,6 @@ const browserSupportsPolyfills = () => {
8771
};
8872

8973
export {
90-
debounce,
9174
concatTranscripts,
9275
commandToRegExp,
9376
compareTwoStringsUsingDiceCoefficient,

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,6 +2128,13 @@ __metadata:
21282128
languageName: node
21292129
linkType: hard
21302130

2131+
"lodash.debounce@npm:^4.0.8":
2132+
version: 4.0.8
2133+
resolution: "lodash.debounce@npm:4.0.8"
2134+
checksum: 10c0/762998a63e095412b6099b8290903e0a8ddcb353ac6e2e0f2d7e7d03abd4275fe3c689d88960eb90b0dde4f177554d51a690f22a343932ecbc50a5d111849987
2135+
languageName: node
2136+
linkType: hard
2137+
21312138
"log-symbols@npm:^6.0.0":
21322139
version: 6.0.0
21332140
resolution: "log-symbols@npm:6.0.0"
@@ -2619,6 +2626,7 @@ __metadata:
26192626
"@vitest/coverage-v8": "npm:3.0.8"
26202627
bunchee: "npm:6.4.0"
26212628
jsdom: "npm:26.0.0"
2629+
lodash.debounce: "npm:^4.0.8"
26222630
react: "npm:16.14.0"
26232631
react-dom: "npm:16.14.0"
26242632
react-test-renderer: "npm:16.14.0"

0 commit comments

Comments
 (0)