Skip to content

Commit

Permalink
fix timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
esamattis committed Jul 22, 2011
1 parent 7b21078 commit dd3ec6b
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions lib/watcher.coffee
Expand Up @@ -66,27 +66,33 @@ class Watcher extends EventEmitter
@status = status
@emit "status", @status

matches: (filepath) ->
for match in @settings.glob.split(" ")
if gex(match).on path.basename filepath
return true
false

delayedRun: (filepath) ->
if not @timer
console.log "Setting timer", @name
@timer = setTimeout =>
console.log "Manual run from timer", @name
@onModified filepath, true
@timer = null
, @settings.interval * 60 * 1000


hasTimeoutPassed: ->
timeSinceLastrun = ((new Date()).getTime() - @lastRunTimeStamp) / 1000 / 60
timeSinceLastrun > @settings.interval

onModified: (filepath, manual=false) ->

if not manual
for match in @settings.glob.split(" ")
if gex(match).on path.basename filepath
ok = true
break
return unless ok

timeSinceLastrun = ((new Date()).getTime() - @lastRunTimeStamp) / 1000 / 60
if timeSinceLastrun < @settings.interval
console.log "#{ @name } got change on #{ filepath }, but interval is not due yet. #{ @settings.interval - timeSinceLastrun } minutes left"

if not @timer
console.log "Setting timer", @name
@timer = setTimeout =>
console.log "Manual run from timer", @name
@onModified filepath, True
@timer = null
, @settings.interval * 60 * 1000

return unless @matches filepath
if not @hasTimeoutPassed()
console.log "Cannot run #{ @name } yet, time out has not passed"
@delayedRun filepath
return
else
console.log "#{ @name } got change on #{ filepath }"
Expand Down

0 comments on commit dd3ec6b

Please sign in to comment.