From 8661290c6affbd30479679ed14a022c050cc4add Mon Sep 17 00:00:00 2001 From: Corey Johnson & Kevin Sawicki Date: Fri, 14 Jun 2013 15:00:22 -0700 Subject: [PATCH] Rename LoadPathsTask to PathLoader --- .../fuzzy-finder/lib/fuzzy-finder-view.coffee | 4 +- .../fuzzy-finder/lib/fuzzy-finder.coffee | 4 +- .../lib/load-paths-handler.coffee | 98 +++++++++---------- ...d-paths-task.coffee => path-loader.coffee} | 3 +- .../spec/fuzzy-finder-spec.coffee | 18 ++-- 5 files changed, 59 insertions(+), 68 deletions(-) rename src/packages/fuzzy-finder/lib/{load-paths-task.coffee => path-loader.coffee} (92%) diff --git a/src/packages/fuzzy-finder/lib/fuzzy-finder-view.coffee b/src/packages/fuzzy-finder/lib/fuzzy-finder-view.coffee index 704b7f5d8..e17f18829 100644 --- a/src/packages/fuzzy-finder/lib/fuzzy-finder-view.coffee +++ b/src/packages/fuzzy-finder/lib/fuzzy-finder-view.coffee @@ -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) diff --git a/src/packages/fuzzy-finder/lib/fuzzy-finder.coffee b/src/packages/fuzzy-finder/lib/fuzzy-finder.coffee index e9f2428b6..b0375a6b4 100644 --- a/src/packages/fuzzy-finder/lib/fuzzy-finder.coffee +++ b/src/packages/fuzzy-finder/lib/fuzzy-finder.coffee @@ -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()] diff --git a/src/packages/fuzzy-finder/lib/load-paths-handler.coffee b/src/packages/fuzzy-finder/lib/load-paths-handler.coffee index f01172931..02b6b0b60 100644 --- a/src/packages/fuzzy-finder/lib/load-paths-handler.coffee +++ b/src/packages/fuzzy-finder/lib/load-paths-handler.coffee @@ -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) diff --git a/src/packages/fuzzy-finder/lib/load-paths-task.coffee b/src/packages/fuzzy-finder/lib/path-loader.coffee similarity index 92% rename from src/packages/fuzzy-finder/lib/load-paths-task.coffee rename to src/packages/fuzzy-finder/lib/path-loader.coffee index ab464184d..c0c0e0456 100644 --- a/src/packages/fuzzy-finder/lib/load-paths-task.coffee +++ b/src/packages/fuzzy-finder/lib/path-loader.coffee @@ -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') ? [] diff --git a/src/packages/fuzzy-finder/spec/fuzzy-finder-spec.coffee b/src/packages/fuzzy-finder/spec/fuzzy-finder-spec.coffee index 085c785d8..c918bc3d2 100644 --- a/src/packages/fuzzy-finder/spec/fuzzy-finder-spec.coffee +++ b/src/packages/fuzzy-finder/spec/fuzzy-finder-spec.coffee @@ -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", ->