Skip to content

Commit

Permalink
fix: refactor _cachePaths and _cachePathsForDirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Mar 18, 2021
1 parent 8829c78 commit 5f85f1c
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions lib/paths-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,12 @@ export default class PathsCache extends EventEmitter {
this.emit("rebuild-cache")

await this._cacheProjectPathsAndRepositories()

/** @type {Promise<Array<Array<string>>} */
let result
try {
result = await this._cachePathsWithGlob()
} catch (e) {
console.error(e)
result = await this._cachePathsWithAtom()
}
const results = await this._cachePaths()

this._addWatchers()

this.emit("rebuild-cache-done")
return result
return results
}

/**
Expand Down Expand Up @@ -182,17 +174,14 @@ export default class PathsCache extends EventEmitter {
* Invoked when the content of the given `directory` has changed
* @param {Directory} projectDirectory
* @param {Directory} directory
* @returns {Promise<void>}
* @private
*/
_onDirectoryChanged(projectDirectory, directory) {
async _onDirectoryChanged(projectDirectory, directory) {
this.emit("rebuild-cache")
this._removeFilePathsForDirectory(projectDirectory, directory)
this._cleanWatchersForDirectory(directory)
this._cachePathsForDirectoryWithGlob(directory.path).catch((e) => {
// fallback to Atom
console.error(e)
this._cachePathsForDirectoryWithAtom(projectDirectory, directory)
})
await this._cachePathsForDirectory(projectDirectory, directory)
this.emit("rebuild-cache-done")
}

Expand Down Expand Up @@ -294,6 +283,37 @@ export default class PathsCache extends EventEmitter {
})
}

/**
* Caches file paths with Glob or Atom
* @return {Promise<Array<Array<string>>}
* @private
*/
async _cachePaths() {
try {
return this._cachePathsWithGlob()
} catch (e) {
console.error(e)
return this._cachePathsWithAtom()
}
}

/**
* Caches file paths for the given directory with Glob or Atom
* @param {Directory} projectDirectory
* @param {Directory} directory
* @return {Promise}
* @private
*/
async _cachePathsForDirectory(projectDirectory, directory) {
try {
return this._cachePathsForDirectoryWithGlob(directory.path)
} catch (e) {
// fallback to Atom
console.error(e)
return this._cachePathsForDirectoryWithAtom(projectDirectory, directory)
}
}

/*
██████ ██ ██████ ██████
██ ██ ██ ██ ██ ██
Expand Down

0 comments on commit 5f85f1c

Please sign in to comment.