Switch to the delegate pattern,

eliminating a nasty race condition and deleting a lot of code.
This commit is contained in:
Michael Bolin
2015-06-02 17:40:27 -04:00
parent fa3fd9c50c
commit 4eb30f3925
3 changed files with 38 additions and 72 deletions

View File

@@ -832,10 +832,12 @@ class Workspace extends Model
throw Error("Could not find directory searcher for #{directory.getPath()}")
# Now that we are sure every Directory has a searcher, construct the search options.
onSearchResult = (result) ->
iterator(result) unless atom.project.isPathModified(result.filePath)
onSearchError = (error) ->
iterator(null, error)
delegateProto = {
onDidMatch: (result) ->
iterator(result) unless atom.project.isPathModified(result.filePath)
onDidError: (error) ->
iterator(null, error)
}
# Define the onPathsSearched callback.
if _.isFunction(options.onPathsSearched)
@@ -856,22 +858,18 @@ class Workspace extends Model
# Kick off all of the searches and unify them into one Promise.
allSearches = []
disposables = new CompositeDisposable
for entry in searchersAndDirectories
{searcher, directory} = entry
directorySearcher = searcher.search(directory, searchOptions)
disposables.add(directorySearcher.onDidMatch(onSearchResult))
disposables.add(directorySearcher.onDidError(onSearchError))
recordNumberOfPathsSearched = onPathsSearched.bind(undefined, directory)
disposables.add(directorySearcher.onDidSearchPaths(recordNumberOfPathsSearched))
delegate = Object.create(delegateProto, {
onDidSearchPaths: {
value: recordNumberOfPathsSearched,
}
})
directorySearcher = searcher.search(directory, delegate, searchOptions)
allSearches.push(directorySearcher)
searchPromise = Promise.all(allSearches)
# Make sure to clean up the disposables once the searchPromise is determined.
disposeAll = (args...) ->
disposables.dispose()
searchPromise.then(disposeAll, disposeAll)
for buffer in atom.project.getBuffers() when buffer.isModified()
filePath = buffer.getPath()
continue unless atom.project.contains(filePath)