Skip to content

Commit

Permalink
0.2.1: interrupting a Pause shouldn't terminate the entire Sayable
Browse files Browse the repository at this point in the history
  • Loading branch information
nafg committed Feb 26, 2018
1 parent 91d1917 commit 4738f87
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
25 changes: 20 additions & 5 deletions core/src/main/scala/simpleivr/IvrCommandInterpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@ import cats.effect.IO

class IvrCommandInterpreter(ivrApi: IvrApi, speakGenerator: SpeakGenerator = Text2waveSpeakGenerator)
extends IvrCommand.Folder[IO] {

private def curTime = IO(System.currentTimeMillis())

protected def runPause(ms: Int, interrupt: String): IO[Option[Char]] =
if (ms <= 0)
IO.pure(None)
else if (interrupt.isEmpty)
IO(Thread.sleep(ms)).map(_ => None)
else
for {
startTime <- curTime
digit <- waitForDigit(ms)
res <-
if (digit.exists(interrupt.contains(_)))
IO.pure(digit)
else
curTime.map(_ - startTime).flatMap(elapsed => runPause(ms - elapsed.toInt, interrupt))
} yield res

/**
* `None` if no DTMF was received, otherwise `Some(d)` where `d` is the
* digit that was pressed.
Expand All @@ -15,11 +34,7 @@ class IvrCommandInterpreter(ivrApi: IvrApi, speakGenerator: SpeakGenerator = Tex
case SayNothing =>
IO.pure(None)

case Pause(ms) =>
if (interrupt.nonEmpty)
waitForDigit(ms)
else
IO(Thread.sleep(ms)).map(_ => None)
case Pause(ms) => runPause(ms, interrupt)

case play: Play =>
streamFile(play.path.pathAndName, interrupt).map {
Expand Down
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ThisBuild / version := "0.2.0"
ThisBuild / version := "0.2.1"

0 comments on commit 4738f87

Please sign in to comment.