sbt/util/io/SourceModificationWatch.scala

45 lines
1.3 KiB
Scala
Raw Normal View History

/* sbt -- Simple Build Tool
* Copyright 2009, 2010 Mikko Peltonen, Stuart Roebuck, Mark Harrah
*/
package sbt
2010-09-05 17:19:19 +02:00
import annotation.tailrec
object SourceModificationWatch
{
2010-09-05 17:19:19 +02:00
@tailrec def watch(sourcesFinder: PathFinder, pollDelaySec: Int, state: WatchState)(terminationCondition: => Boolean): (Boolean, WatchState) =
{
2010-09-05 17:19:19 +02:00
import state._
def sourceFiles: Iterable[java.io.File] = sourcesFinder.getFiles
2010-09-05 17:19:19 +02:00
val (lastModifiedTime, fileCount) =
( (0L, 0) /: sourceFiles) {(acc, file) => (math.max(acc._1, file.lastModified), acc._2 + 1)}
2010-09-05 17:19:19 +02:00
val sourcesModified =
lastModifiedTime > lastCallbackCallTime ||
previousFileCount != fileCount
2010-09-05 17:19:19 +02:00
val (triggered, newCallbackCallTime) =
if (sourcesModified)
2010-09-05 17:19:19 +02:00
(false, System.currentTimeMillis)
else
(awaitingQuietPeriod, lastCallbackCallTime)
2010-09-05 17:19:19 +02:00
val newState = new WatchState(newCallbackCallTime, fileCount, sourcesModified, if(triggered) count + 1 else count)
if(triggered)
(true, newState)
else
{
Thread.sleep(pollDelaySec * 1000)
2010-09-05 17:19:19 +02:00
if(terminationCondition)
(false, newState)
else
watch(sourcesFinder, pollDelaySec, newState)(terminationCondition)
}
}
2010-09-05 17:19:19 +02:00
}
final class WatchState(val lastCallbackCallTime: Long, val previousFileCount: Int, val awaitingQuietPeriod:Boolean, val count: Int)
object WatchState
{
def empty = new WatchState(0L, 0, false, 0)
}