Rename LoadPathsTask to PathLoader

This commit is contained in:
Corey Johnson & Kevin Sawicki 2013-06-14 15:00:22 -07:00 committed by probablycorey
parent 84a8f11fc9
commit 8661290c6a
5 changed files with 59 additions and 68 deletions

View File

@ -5,7 +5,7 @@ $ = require 'jquery'
humanize = require 'humanize-plus'
fsUtils = require 'fs-utils'
path = require 'path'
LoadPathsTask = require './load-paths-task'
PathLoader = require './path-loader'
Point = require 'point'
module.exports =
@ -217,7 +217,7 @@ class FuzzyFinderView extends SelectList
if @reloadProjectPaths
@loadPathsTask?.terminate()
@loadPathsTask = LoadPathsTask.once (paths) =>
@loadPathsTask = PathLoader.startTask (paths) =>
@projectPaths = paths
@reloadProjectPaths = false
@populateProjectPaths(options)

View File

@ -16,8 +16,8 @@ module.exports =
@createView().toggleGitFinder()
if project.getPath()?
LoadPathsTask = require './load-paths-task'
@loadPathsTask = LoadPathsTask.once (paths) => @projectPaths = paths
PathLoader = require './path-loader'
@loadPathsTask = PathLoader.startTask (paths) => @projectPaths = paths
for editSession in project.getEditSessions()
editSession.lastOpened = state[editSession.getPath()]

View File

@ -3,64 +3,56 @@ path = require 'path'
_ = require 'underscore'
Git = require 'git'
class PathLoader
asyncCallsInProgress: 0
pathsChunkSize: 100
paths: null
repo: null
rootPath: null
ignoredNames: null
callback: null
asyncCallsInProgress = 0
pathsChunkSize = 100
paths = []
repo = null
ignoredNames = null
callback = null
constructor: (@rootPath, ignoreVcsIgnores=false, @ignoredNames=[], @callback) ->
@paths = []
@repo = Git.open(@rootPath, refreshOnWindowFocus: false) if ignoreVcsIgnores
@ignoredNames.sort()
isIgnored = (loadedPath) ->
repo?.isPathIgnored(loadedPath) or _.indexOf(ignoredNames, path.basename(loadedPath), true) isnt -1
isIgnored: (loadedPath) ->
@repo?.isPathIgnored(loadedPath) or _.indexOf(@ignoredNames, path.basename(loadedPath), true) isnt -1
asyncCallStarting = ->
asyncCallsInProgress++
asyncCallStarting: ->
@asyncCallsInProgress++
asyncCallDone = ->
if --asyncCallsInProgress is 0
repo?.destroy()
emit('load-paths:paths-found', paths)
callback()
asyncCallDone: ->
if --@asyncCallsInProgress is 0
@repo?.destroy()
emit('load-paths:paths-found', @paths)
@callback()
pathLoaded = (path) ->
paths.push(path) unless isIgnored(path)
if paths.length is pathsChunkSize
emit('load-paths:paths-found', paths)
paths = []
pathLoaded: (path) ->
@paths.push(path) unless @isIgnored(path)
if @paths.length is @pathsChunkSize
emit('load-paths:paths-found', @paths)
@paths = []
loadPath = (path) ->
asyncCallStarting()
fs.lstat path, (error, stats) ->
unless error?
if stats.isSymbolicLink()
asyncCallStarting()
fs.stat path, (error, stats) ->
unless error?
pathLoaded(path) if stats.isFile()
asyncCallDone()
else if stats.isDirectory()
loadFolder(path) unless isIgnored(path)
else if stats.isFile()
pathLoaded(path)
asyncCallDone()
loadPath: (path) ->
@asyncCallStarting()
fs.lstat path, (error, stats) =>
unless error?
if stats.isSymbolicLink()
@asyncCallStarting()
fs.stat path, (error, stats) =>
unless error?
@pathLoaded(path) if stats.isFile()
@asyncCallDone()
else if stats.isDirectory()
@loadFolder(path) unless @isIgnored(path)
else if stats.isFile()
@pathLoaded(path)
@asyncCallDone()
loadFolder = (folderPath) ->
asyncCallStarting()
fs.readdir folderPath, (error, children=[]) ->
loadPath(path.join(folderPath, childName)) for childName in children
asyncCallDone()
loadFolder: (folderPath) ->
@asyncCallStarting()
fs.readdir folderPath, (error, children=[]) =>
@loadPath(path.join(folderPath, childName)) for childName in children
@asyncCallDone()
load: ->
@loadFolder(@rootPath)
module.exports = (rootPath, ignoreVcsIgnores, ignoredNames) ->
module.exports = (rootPath, ignoreVcsIgnores, ignore) ->
ignoredNames = ignore
callback = @async()
pathLoader = new PathLoader(rootPath, ignoreVcsIgnores, ignoredNames, callback)
pathLoader.load()
repo = Git.open(rootPath, refreshOnWindowFocus: false) if ignoreVcsIgnores
ignoredNames.sort()
loadFolder(rootPath)

View File

@ -1,8 +1,7 @@
Task = require 'task'
module.exports =
class LoadPathsTask
@once: (callback) ->
startTask: (callback) ->
projectPaths = []
taskPath = require.resolve('./load-paths-handler')
ignoredNames = config.get('fuzzyFinder.ignoredNames') ? []

View File

@ -1,6 +1,6 @@
RootView = require 'root-view'
FuzzyFinder = require 'fuzzy-finder/lib/fuzzy-finder-view'
LoadPathsTask = require 'fuzzy-finder/lib/load-paths-task'
PathLoader = require 'fuzzy-finder/lib/path-loader'
_ = require 'underscore'
$ = require 'jquery'
{$$} = require 'space-pen'
@ -302,15 +302,15 @@ describe 'FuzzyFinder', ->
describe "cached file paths", ->
it "caches file paths after first time", ->
spyOn(LoadPathsTask, "once").andCallThrough()
spyOn(PathLoader, "startTask").andCallThrough()
rootView.trigger 'fuzzy-finder:toggle-file-finder'
waitsFor ->
finderView.list.children('li').length > 0
runs ->
expect(LoadPathsTask.once).toHaveBeenCalled()
LoadPathsTask.once.reset()
expect(PathLoader.startTask).toHaveBeenCalled()
PathLoader.startTask.reset()
rootView.trigger 'fuzzy-finder:toggle-file-finder'
rootView.trigger 'fuzzy-finder:toggle-file-finder'
@ -318,7 +318,7 @@ describe 'FuzzyFinder', ->
finderView.list.children('li').length > 0
runs ->
expect(LoadPathsTask.once).not.toHaveBeenCalled()
expect(PathLoader.startTask).not.toHaveBeenCalled()
it "doesn't cache buffer paths", ->
spyOn(project, "getEditSessions").andCallThrough()
@ -340,19 +340,19 @@ describe 'FuzzyFinder', ->
expect(project.getEditSessions).toHaveBeenCalled()
it "busts the cache when the window gains focus", ->
spyOn(LoadPathsTask, "once").andCallThrough()
spyOn(PathLoader, "startTask").andCallThrough()
rootView.trigger 'fuzzy-finder:toggle-file-finder'
waitsFor ->
finderView.list.children('li').length > 0
runs ->
expect(LoadPathsTask.once).toHaveBeenCalled()
LoadPathsTask.once.reset()
expect(PathLoader.startTask).toHaveBeenCalled()
PathLoader.startTask.reset()
$(window).trigger 'focus'
rootView.trigger 'fuzzy-finder:toggle-file-finder'
rootView.trigger 'fuzzy-finder:toggle-file-finder'
expect(LoadPathsTask.once).toHaveBeenCalled()
expect(PathLoader.startTask).toHaveBeenCalled()
describe "path ignoring", ->
it "ignores paths that match entries in config.fuzzyFinder.ignoredNames", ->