Skip to content

Commit

Permalink
v2.4.1. Bugfix.
Browse files Browse the repository at this point in the history
- v2.4.1 April 10, 2013
	- Fixed bubblr events
	- Fixed swap file detection
  • Loading branch information
balupton committed Apr 10, 2013
1 parent 1941fac commit c30c9b0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 24 deletions.
4 changes: 4 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## History

- v2.4.1 April 10, 2013
- Fixed bubblr events
- Fixed swap file detection

- v2.4.0 April 5, 2013
- Dependency upgrades

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "watchr",
"version": "2.4.0",
"version": "2.4.1",
"description": "Better file system watching for Node.js",
"homepage": "https://github.com/bevry/watchr",
"keywords": [
Expand Down
70 changes: 56 additions & 14 deletions src/lib/watchr.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ Watcher = class extends EventEmitter
# Bubbler
# Setup a bubble wrapper
bubbler: (eventName) =>
return (args...) => @bubble(args...)
return (args...) => @bubble(eventName, args...)

###
Listen
Expand Down Expand Up @@ -359,47 +359,89 @@ Watcher = class extends EventEmitter
# Error?
return @emit('error',err) if err

# Prepare
deletedFiles = []
createdFiles = []
tasks = new TaskGroup().setConfig(concurrency:0).once 'complete', =>
[eventName,childFileRelativePath] = args
# Check we are from watch method
return unless typeChecker.isString(eventName)

# Check that we can be trusted (can't trust the other event types)
return unless eventName is 'change'

# Note
@log('debug', 'determined child file change:', childFileRelativePath, 'via:', fileFullPath)

# Check that we haven't already taken care of the file
if childFileRelativePath in deletedFiles or childFileRelativePath in createdFiles
@log('debug', 'that had already been taken care of:', childFileRelativePath, 'via:', fileFullPath)
return

# Check that we have a child watch
childFileWatcher = @children[childFileRelativePath]
unless childFileWatcher
@log('debug', 'but had no watchr for the child file:', childFileRelativePath, 'via:', fileFullPath)
return

# Forward the change event onto the child file watcher
@log('debug', 'forwarding child file change onto child watcher:', childFileRelativePath, 'via:', fileFullPath)
return childFileWatcher.listener('change','.')

# Check for deleted files
# by cycling through our known children
eachr @children, (childFileWatcher,childFileRelativePath) =>
eachr @children, (childFileWatcher,childFileRelativePath) => tasks.addTask (complete) =>
# Skip if this is a new file (not a deleted file)
return if childFileRelativePath in newFileRelativePaths
return complete() if childFileRelativePath in newFileRelativePaths

# Fetch full path
childFileFullPath = pathUtil.join(fileFullPath,childFileRelativePath)

# Skip if ignored file
return if @isIgnoredPath(childFileFullPath)
if @isIgnoredPath(childFileFullPath)
@log('debug','ignored delete:',childFileFullPath,'via:',fileFullPath)
return complete()

# Emit the event
# Emit the event and note the change
@log('debug','determined delete:',childFileFullPath,'via:',fileFullPath)
@closeChild(childFileRelativePath,'deleted')
deletedFiles.push(childFileRelativePath)
return complete()

# Check for new files
eachr newFileRelativePaths, (childFileRelativePath) =>
eachr newFileRelativePaths, (childFileRelativePath) => tasks.addTask (complete) =>
# Skip if we are already watching this file
return if @children[childFileRelativePath]?
return complete() if @children[childFileRelativePath]?
@children[childFileRelativePath] = false # reserve this file

# Fetch full path
childFileFullPath = pathUtil.join(fileFullPath,childFileRelativePath)

# Skip if ignored file
return if @isIgnoredPath(childFileFullPath)
if @isIgnoredPath(childFileFullPath)
@log('debug','ignored create:',childFileFullPath,'via:',fileFullPath)
return complete()

# Fetch the stat for the new file
safefs.stat childFileFullPath, (err,childFileStat) =>
# Error?
return @emit('error',err) if err
if err
@emit('error',err)
return complete()

# Emit the event
# Emit the event and note the change
@log('debug','determined create:',childFileFullPath,'via:',fileFullPath)
@emitSafe('change','create',childFileFullPath,childFileStat,null)
@watchChild({
fullPath: childFileFullPath,
relativePath: childFileRelativePath,
stat: childFileStat
})
createdFiles.push(childFileRelativePath)
return complete()

# Run the tasks
tasks.run()


# If we are a file, lets simply emit the change event
Expand All @@ -409,19 +451,19 @@ Watcher = class extends EventEmitter
@emitSafe('change','update',fileFullPath,currentStat,previousStat)

# Check if the file still exists
safefs.exists fileFullPath, (exists) ->
safefs.exists fileFullPath, (exists) =>
# Apply
fileExists = exists

# If the file still exists, then update the stat
if fileExists
safefs.stat fileFullPath, (err,stat) ->
safefs.stat fileFullPath, (err,stat) =>
# Check
return me.emit('error',err) if err
return @emit('error',err) if err

# Update
currentStat = stat
me.stat = currentStat
@stat = currentStat

# Get on with it
determineTheChange()
Expand Down
19 changes: 10 additions & 9 deletions src/test/everything.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pathUtil = require('path')
fsUtil = require('fs')
balUtil = require('bal-util')
extendr = require('extendr')
watchr = require(__dirname+'/../lib/watchr')
assert = require('assert')
joe = require('joe')
Expand Down Expand Up @@ -29,10 +30,7 @@ writetree =
# =====================================
# Tests

# -------------------------------------
# Watchr

joe.suite 'watchr', (suite,test) ->
runTests = (opts,describe,test) ->
# Prepare
watcher = null

Expand Down Expand Up @@ -73,7 +71,7 @@ joe.suite 'watchr', (suite,test) ->
done(err)

test 'start watching', (done) ->
watchr.watch(
watchr.watch(extendr.extend({
path: outPath
listener: changeHappened
ignorePaths: [pathUtil.join(outPath,'blah')]
Expand All @@ -82,7 +80,7 @@ joe.suite 'watchr', (suite,test) ->
next: (err,_watcher) ->
watcher = _watcher
wait batchDelay, -> done(err)
)
},opts))

test 'detect write', (done) ->
writeFile('a')
Expand Down Expand Up @@ -136,6 +134,9 @@ joe.suite 'watchr', (suite,test) ->
test 'stop watching', ->
watcher.close()

test 'completed', (done) ->
done()
process.exit()
# Run tests for each method
joe.describe 'watchr', (describe,test) ->
describe 'watch', (describe,test) ->
runTests({preferredMethods:['watch','watchFile']},describe,test)
describe 'watchFile', (describe,test) ->
runTests({preferredMethods:['watchFile','watch']},describe,test)

0 comments on commit c30c9b0

Please sign in to comment.