UNPKG

ivr-tester

Version:

An automated testing framework for IVR call flows

76 lines (75 loc) 2.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PostSilencePrompt = void 0; class PostSilencePrompt { constructor(definition, call, matchedCallback, timeoutCallback, timeoutSet, timeoutClear) { this.definition = definition; this.call = call; this.matchedCallback = matchedCallback; this.timeoutCallback = timeoutCallback; this.timeoutSet = timeoutSet; this.timeoutClear = timeoutClear; this.promptTimedOut = false; this.isFirstInvocation = true; this.lastKnownTranscript = ""; this.skipPrompt = false; } setNext(prompt) { this.nextPrompt = prompt; return prompt; } transcriptUpdated(transcriptEvent) { if (this.promptTimedOut) { return; } if (this.skipPrompt && this.nextPrompt) { this.nextPrompt.transcriptUpdated(transcriptEvent); return; } this.lastKnownTranscript = transcriptEvent.merge(); if (this.isFirstInvocation) { this.startTimeoutTimer(); this.isFirstInvocation = false; } this.processUpdatedTranscript(transcriptEvent); } processUpdatedTranscript(transcriptEvent) { this.clearSilenceAfterPromptTimer(); const transcript = transcriptEvent.merge(); if (this.definition.whenPrompt(transcript)) { this.clearTimeoutTimer(); // The timeout interval that is set cannot be relied upon to execute after that // exact number of milliseconds. This is because other executing code that blocks // or holds onto the event loop will push the execution of the timeout back. The only // guarantee is that the timeout will not execute sooner than the declared // timeout interval. // -- https://nodejs.org/en/docs/guides/timers-in-node/ this.silenceAfterPromptTimer = this.timeoutSet(() => { this.skipPrompt = true; this.clearSilenceAfterPromptTimer(); transcriptEvent.clear(); this.matchedCallback(this, transcript); this.definition.then.do(this.call); }, this.definition.silenceAfterPrompt); } } clearSilenceAfterPromptTimer() { if (this.silenceAfterPromptTimer) { this.timeoutClear(this.silenceAfterPromptTimer); this.silenceAfterPromptTimer = undefined; } } startTimeoutTimer() { this.timeoutTimer = this.timeoutSet(() => { this.promptTimedOut = true; this.timeoutCallback(this, this.lastKnownTranscript); }, this.definition.timeout); } clearTimeoutTimer() { if (this.timeoutTimer) { this.timeoutClear(this.timeoutTimer); this.timeoutTimer = undefined; } } } exports.PostSilencePrompt = PostSilencePrompt;