Skip to content

Commit d7d84b7

Browse files
committed
optional timeout for 'delayFunctionUntilIdle'
1 parent 298d4ff commit d7d84b7

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

www/scripts/sepiaFW.ui.actions.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ function sepiaFW_build_ui_actions(){
55
//Simple delay queue that waits for next idle state - NOTE: don't mistake this for 'command queue' (client)
66
//Executes all functions in next idle state so MAKE SURE! they don't interfere with each other!
77
var delayQueue = {};
8+
var delayQueueTimers = {};
9+
var delayQueueTimeout = 6000;
810
var delayId = 0;
9-
Actions.delayFunctionUntilIdle = function(fun, idleState){
11+
Actions.delayFunctionUntilIdle = function(fun, idleState, timeoutDelay, timeoutMessage){
1012
delayId++;
1113
if (delayId > 64000) delayId = 0;
1214
if (!idleState) idleState = "any"; //could be: unknown, ttsFinished, dialogFinished, asrFinished, anyButAsr
@@ -15,6 +17,12 @@ function sepiaFW_build_ui_actions(){
1517
idleState: idleState,
1618
id: delayId
1719
};
20+
if (timeoutDelay){
21+
delayQueueTimers[delayId] = setTimeout(function(){
22+
SepiaFW.ui.showInfo(timeoutMessage || ("Delayed function lost due to timeout - Id: " + delayId));
23+
delete delayQueue[delayId];
24+
}, timeoutDelay);
25+
}
1826
//NOTE: At the time of this function call the state can actually BE IDLE (short transient)
1927
//Should we add a timeout fallback?
2028
//console.error("delayFunction", "state", SepiaFW.animate.assistant.getState()); //DEBUG
@@ -26,13 +34,16 @@ function sepiaFW_build_ui_actions(){
2634
if (!stateFilter
2735
|| queueData.idleState == "any"
2836
|| (queueData.idleState == stateFilter)
29-
|| (queueData.idleState == "anyButAsr" && stateFilter != "asrFinished")){
37+
|| (queueData.idleState == "anyButAsr" && stateFilter != "asrFinished")
38+
){
39+
clearTimeout(delayQueueTimers[queueData.id]);
3040
queueData.fun();
3141
cleanUpIds.push(queueData.id);
3242
}
3343
});
3444
for (var i=0; i<cleanUpIds.length; i++){
3545
delete delayQueue[cleanUpIds[i]];
46+
delete delayQueueTimers[cleanUpIds[i]];
3647
}
3748
}
3849
Actions.getDelayQueueSize = function(){
@@ -41,6 +52,10 @@ function sepiaFW_build_ui_actions(){
4152
}
4253
Actions.clearDelayQueue = function(){
4354
delayQueue = {};
55+
Object.keys(delayQueueTimers).forEach(function(dId){
56+
clearTimeout(delayQueueTimers[dId]);
57+
});
58+
delayQueueTimers = {};
4459
}
4560

4661
//custom action event dispatcher
@@ -203,7 +218,8 @@ function sepiaFW_build_ui_actions(){
203218
if (delayUntilIdle || action.delayUntilIdle){
204219
Actions.delayFunctionUntilIdle(function(){
205220
SepiaFW.client.controls.handle(action.fun, action.controlData);
206-
}, "any"); //idleState req.
221+
}, "any", //idleState req.
222+
8000, "Failed to start audio (timeout).");
207223
}else{
208224
SepiaFW.client.controls.handle(action.fun, action.controlData);
209225
}
@@ -390,7 +406,8 @@ function sepiaFW_build_ui_actions(){
390406
var idleState = "anyButAsr";
391407
Actions.delayFunctionUntilIdle(function(){
392408
playAction(action);
393-
}, idleState);
409+
}, idleState,
410+
14000, "Failed to start audio (timeout).");
394411
}else{
395412
playAction(action);
396413
}

www/scripts/sepiaFW.ui.cards.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2148,7 +2148,8 @@ function sepiaFW_build_ui_cards(){
21482148
if (SepiaFW.animate.assistant.getState() != "idle"){
21492149
SepiaFW.ui.actions.delayFunctionUntilIdle(function(){
21502150
request();
2151-
}, "any"); //idleState req.
2151+
}, "any", //idleState req.
2152+
8000, "Failed to start media player (timeout).");
21522153
}else{
21532154
request();
21542155
}

0 commit comments

Comments
 (0)