###1Watch a file for changes23Watch for changes to the given file. Returns obj, which4is an event emitter with events:56- 'change', ctime - when file changes or is created7- 'delete' - when file is deleted89and a method .close().1011The ctime might be undefined, in case it can't be determined.1213If debounce is given, only fires after the file14definitely has not had its ctime changed15for at least debounce ms. Does NOT fire when16the file first has ctime changed.17###1819fs = require('fs')20{EventEmitter} = require('events')2122class exports.Watcher extends EventEmitter23constructor: (@path, @interval, @debounce) ->24fs.watchFile(@path, {interval: @interval, persistent:false}, @_listen)2526close: () =>27@removeAllListeners()28fs.unwatchFile(@path, @listener)2930_listen: (curr, prev) =>31if curr.dev == 032@emit('delete')33else34if @debounce35@_emit_when_stable(true)36else37fs.stat @path, (err, stats) =>38if err39@emit('change')40else41@emit('change', stats.ctime)4243_emit_when_stable: (first) =>44###45@_emit_when_stable gets called46periodically until the last ctime of the file47is at least @debounce ms in the past, or there48is an error.49###50if first and @_waiting_for_stable51return52@_waiting_for_stable = true53fs.stat @path, (err, stats) =>54if err55# maybe file deleted; give up.56delete @_waiting_for_stable57return58elapsed = new Date() - stats.ctime59if elapsed < @debounce60# File keeps changing - try again soon61setTimeout((=>@_emit_when_stable(false)), Math.max(500, @debounce - elapsed + 100))62else63delete @_waiting_for_stable64@emit('change', stats.ctime)65666768