var EventEmitter = require('events').EventEmitter

var helpers = require('broccoli-kitchen-sink-helpers')


module.exports = Watcher
function Watcher(builder) {
  this.builder = builder
  this.check()
}

Watcher.prototype = Object.create(EventEmitter.prototype)
Watcher.prototype.constructor = Watcher

Watcher.prototype.check = function() {
  try {
    var newStatsHash = this.builder.treesRead.map(function (tree) {
      return typeof tree === 'string' ? helpers.hashTree(tree) : ''
    }).join('\x00')
    if (newStatsHash !== this.statsHash) {
      this.statsHash = newStatsHash
      this.current = this.builder.build()
      this.current.then(function(directory) {
        this.emit('change', directory)
        return directory
      }.bind(this), function(error) {
        this.emit('error', error)
        throw error
      }.bind(this)).finally(this.check.bind(this))
    } else {
      setTimeout(this.check.bind(this), 100)
    }
  } catch (err) {
    console.error('Uncaught error in Broccoli file watcher:')
    console.error(err.stack)
    console.error('Watcher quitting') // do not schedule check with setTimeout
  }
}

Watcher.prototype.then = function(success, fail) {
  return this.current.then(success, fail)
}

@@ -150,16 +150,25 @@
 (builder
+, options
 ) %7B%0A  th
@@ -188,16 +188,47 @@
 builder%0A
+  this.options = options %7C%7C %7B%7D%0A
   this.c
@@ -383,16 +383,64 @@
   try %7B%0A
+    var interval = this.options.interval %7C%7C 100%0A
     var 
@@ -1014,19 +1014,24 @@
 (this), 
-100
+interval
 )%0A    %7D%0A

Allow Watcher interval to be configurable.