Skip to content

Commit

Permalink
refactor(fileList): small refactoring (#3243)
Browse files Browse the repository at this point in the history
  • Loading branch information
lusarz authored and johnjbarton committed Dec 31, 2018
1 parent 51007b1 commit b05a54e
Showing 1 changed file with 57 additions and 65 deletions.
122 changes: 57 additions & 65 deletions lib/file-list.js
Expand Up @@ -13,13 +13,6 @@ const helper = require('./helper')
const log = require('./logger').create('watcher')
const createPatternObject = require('./config').createPatternObject

function byPath (a, b) {
if (a.path > b.path) return 1
if (a.path < b.path) return -1

return 0
}

class FileList {
constructor (patterns, excludes, emitter, preprocess, autoWatchBatchDelay) {
this._patterns = patterns || []
Expand Down Expand Up @@ -67,52 +60,49 @@ class FileList {
const matchedFiles = new Set()

let lastCompletedRefresh = this._refreshing
lastCompletedRefresh = Promise.map(this._patterns, (patternObject) => {
const pattern = patternObject.pattern
const type = patternObject.type

if (helper.isUrlAbsolute(pattern)) {
this.buckets.set(pattern, [new Url(pattern, type)])
return Promise.resolve()
}

const mg = new Glob(pathLib.normalize(pattern), { cwd: '/', follow: true, nodir: true, sync: true })
const files = mg.found
if (_.isEmpty(files)) {
this.buckets.set(pattern, [])
log.warn(`Pattern "${pattern}" does not match any file.`)
return
}

return Promise.map(files, (path) => {
if (this._findExcluded(path)) {
log.debug(`Excluded file "${path}"`)
return Promise.resolve()
lastCompletedRefresh = Promise
.map(this._patterns, ({ pattern, type, nocache }) => {
if (helper.isUrlAbsolute(pattern)) {
this.buckets.set(pattern, [new Url(pattern, type)])
return
}

if (matchedFiles.has(path)) {
return Promise.resolve()
const mg = new Glob(pathLib.normalize(pattern), { cwd: '/', follow: true, nodir: true, sync: true })
if (_.isEmpty(mg.found)) {
this.buckets.set(pattern, [])
log.warn(`Pattern "${pattern}" does not match any file.`)
return
}

matchedFiles.add(path)

const file = new File(path, mg.statCache[path].mtime, patternObject.nocache, type)
if (file.doNotCache) {
log.debug(`Not preprocessing "${pattern}" due to nocache`)
return Promise.resolve(file)
}

return this._preprocess(file).then(() => file)
return Promise
.filter(mg.found, (path) => {
if (this._findExcluded(path)) {
log.debug(`Excluded file "${path}"`)
return false
} else if (matchedFiles.has(path)) {
return false
} else {
matchedFiles.add(path)
return true
}
})
.map((path) => {
const file = new File(path, mg.statCache[path].mtime, nocache, type)
if (nocache) {
log.debug(`Not preprocessing "${pattern}" due to nocache`)
return file
} else {
return this._preprocess(file).then(() => file)
}
})
.then((files) => {
this.buckets.set(pattern, files)

if (_.isEmpty(files)) {
log.warn(`All files matched by "${pattern}" were excluded or matched by prior matchers.`)
}
})
})
.then((files) => {
files = _.compact(files)
this.buckets.set(pattern, files)

if (_.isEmpty(files)) {
log.warn(`All files matched by "${pattern}" were excluded or matched by prior matchers.`)
}
})
})
.then(() => {
// When we return from this function the file processing chain will be
// complete. In the case of two fast refresh() calls, the second call
Expand Down Expand Up @@ -141,14 +131,19 @@ class FileList {
}

const files = this._getFilesByPattern(p.pattern)
files.sort(byPath)
files.sort((a, b) => {
if (a.path > b.path) return 1
if (a.path < b.path) return -1

return 0
})

if (p.served) {
served.push.apply(served, files) // TODO: replace with served.push(...files) after remove Node 4 support
served.push(...files)
}

files.forEach((file) => {
const other = lookup[file.path]
if (other && other.compare(p) < 0) return
if (lookup[file.path] && lookup[file.path].compare(p) < 0) return

lookup[file.path] = p
if (p.included) {
Expand Down Expand Up @@ -198,11 +193,9 @@ class FileList {
const file = new File(path)
this._getFilesByPattern(pattern.pattern).push(file)

return Promise.all([
fs.statAsync(path),
this._refreshing
])
.spread((stat) => {
return Promise
.all([fs.statAsync(path), this._refreshing])
.then(([stat]) => {
file.mtime = stat.mtime
return this._preprocess(file)
})
Expand All @@ -222,15 +215,14 @@ class FileList {
return Promise.resolve(this.files)
}

return Promise.all([
fs.statAsync(path),
this._refreshing
]).spread((stat) => {
if (!force && stat.mtime <= file.mtime) throw new Promise.CancellationError()
return Promise
.all([fs.statAsync(path), this._refreshing])
.then(([stat]) => {
if (!force && stat.mtime <= file.mtime) throw new Promise.CancellationError()

file.mtime = stat.mtime
return this._preprocess(file)
})
file.mtime = stat.mtime
return this._preprocess(file)
})
.then(() => {
log.info(`Changed file "${path}".`)
this._emitModified(force)
Expand Down

0 comments on commit b05a54e

Please sign in to comment.