Skip to content

Commit

Permalink
debouncing
Browse files Browse the repository at this point in the history
  • Loading branch information
wasnoplus committed Sep 28, 2020
1 parent 3922e8d commit 4dd2f78
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions script/resultHandling.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// todo script/VoskJS.js inladen

// const langModel = "model-nl.tar.gz";
// const allWords = ['volgende', 'vorige'];
const callbacks = [];
const allWords = []
var eventWordMap;
Expand All @@ -28,20 +24,40 @@ window.iridium.onExternalNavigate = (callback) => {
};


// source: https://davidwalsh.name/javascript-debounce-function
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};


function handleResult(res, handleIt, reset=null) {
if (typeof reset == "function") {
reset();
}
var wordHandled = false;
words = res.result;
words.forEach((x) => {
if (allWords.includes(x.word)) {
wordHandled = true;
handleIt(x.word, x.conf);
}
});
if (typeof res.result != undefined) {
// words = res.result;
res.result.forEach((x) => {
if (allWords.includes(x.word)) {
wordHandled = true;
handleIt(x.word, x.conf);
}
});
}
}


function sendCommand(w, c) {
if (c >= .5) {
const action = eventWordMap[w];
Expand All @@ -54,8 +70,9 @@ function sendCommand(w, c) {

thisRec = new VoskJS.Recognizer(langModel);
thisRec.onresult = result => {
if (result.result) {
handleResult(result, sendCommand);
}
debounce(function() { handleResult(result, sendCommand) }, 200);
// if (result.result) {
// handleResult(result, sendCommand);
// }
}
thisRec.getActive().then(active => thisRec.setActive(!active));

0 comments on commit 4dd2f78

Please sign in to comment.