Skip to content

Commit

Permalink
Stop handling individual content changes
Browse files Browse the repository at this point in the history
Just reload the entire content tree each time
a file changes.

The small speed boost this provided was causing
more problems than it was worth. Namely that
content plugins that where referenced by other
plugins would get stuck in cache until both
files where updated.
  • Loading branch information
jnordberg committed Feb 24, 2016
1 parent e9dbb02 commit 1f905cc
Showing 1 changed file with 5 additions and 58 deletions.
63 changes: 5 additions & 58 deletions src/core/server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ setup = (env) ->
templatesLoad: false
viewsLoad: false
localsLoad: false
files: {}

isReady = ->
### Returns true if we have no running tasks ###
Expand All @@ -80,9 +79,10 @@ setup = (env) ->
logop = (error) ->
env.logger.error(error.message, error) if error?

changeHandler = (error) ->
changeHandler = (error, path) ->
### Emits a change event if called without error ###
env.emit 'change' unless error?
unless error?
env.emit 'change', path
logop error

loadContents = (callback=logop) ->
Expand Down Expand Up @@ -128,61 +128,8 @@ setup = (env) ->
return false
ignoreInitial: true

contentWatcher.on 'change', (path) ->

# ignore if we dont have the tree loaded or it's still loading
return if not contents? or block.contentsLoad

# also ignore if we are already working on this file
# (windows sometimes sends multiple change events for the same file)
return if block.files[path] is true

block.files[path] = true

content = null
for item in ContentTree.flatten(contents)
if item.__filename is path
content = item
break
if not content
throw new Error "Got a change event for item not previously in tree: #{ path }"

filepath =
relative: env.relativeContentsPath path
full: path

tree = content.parent
key = keyForValue tree, content
group = tree._[content.__plugin.group]

if not key?
throw new Error "Content #{ content.filename } not found in its parent tree!"

loadContent env, filepath, (error, newContent) ->
if error?
contents = null
lookup = {}
return

# replace old contents
newContent.parent = tree
tree[key] = newContent

# also in the trees plugin group
if not replaceInArray(group, content, newContent)
throw new Error "Content #{ content.filename } not found in its plugin group!"

# keep the lookup map fresh
delete lookup[normalizeUrl(content.url)]
lookup[normalizeUrl(newContent.url)] = newContent

delete block.files[path]
env.emit 'change', content.filename

# reload entire tree if a file is removed or added
# patches to modify the already loaded tree instead are welcome :-)
contentWatcher.on 'add', -> loadContents(changeHandler) if not block.contentsLoad
contentWatcher.on 'unlink', -> loadContents(changeHandler) if not block.contentsLoad
# reload content tree on changes
contentWatcher.on 'all', (path) -> loadContents(changeHandler, path) if not block.contentsLoad

templateWatcher = chokidar.watch env.templatesPath,
ignoreInitial: true
Expand Down

0 comments on commit 1f905cc

Please sign in to comment.