Merge pull request #16080 from atom/fb-hw-directory-watcher

Allow directory providers to implement a custom onDidChangeFiles
This commit is contained in:
Ash Wilson 2017-11-02 18:30:59 -07:00 committed by GitHub
commit 809a2d6348
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 2 deletions

View File

@ -335,9 +335,14 @@ describe('Project', () => {
isRoot () { return true } isRoot () { return true }
existsSync () { return this.path.endsWith('does-exist') } existsSync () { return this.path.endsWith('does-exist') }
contains (filePath) { return filePath.startsWith(this.path) } contains (filePath) { return filePath.startsWith(this.path) }
onDidChangeFiles (callback) {
onDidChangeFilesCallback = callback
return {dispose: () => {}}
}
} }
let serviceDisposable = null let serviceDisposable = null
let onDidChangeFilesCallback = null
beforeEach(() => { beforeEach(() => {
serviceDisposable = atom.packages.serviceHub.provide('atom.directory-provider', '0.1.0', { serviceDisposable = atom.packages.serviceHub.provide('atom.directory-provider', '0.1.0', {
@ -349,6 +354,7 @@ describe('Project', () => {
} }
} }
}) })
onDidChangeFilesCallback = null
waitsFor(() => atom.project.directoryProviders.length > 0) waitsFor(() => atom.project.directoryProviders.length > 0)
}) })
@ -383,6 +389,28 @@ describe('Project', () => {
atom.project.setPaths(['ssh://foreign-directory:8080/does-exist']) atom.project.setPaths(['ssh://foreign-directory:8080/does-exist'])
expect(atom.project.getDirectories().length).toBe(0) expect(atom.project.getDirectories().length).toBe(0)
}) })
it('uses the custom onDidChangeFiles as the watcher if available', () => {
// Ensure that all preexisting watchers are stopped
waitsForPromise(() => stopAllWatchers())
const remotePath = 'ssh://another-directory:8080/does-exist'
runs(() => atom.project.setPaths([remotePath]))
waitsForPromise(() => atom.project.getWatcherPromise(remotePath))
runs(() => {
expect(onDidChangeFilesCallback).not.toBeNull()
const changeSpy = jasmine.createSpy('atom.project.onDidChangeFiles')
const disposable = atom.project.onDidChangeFiles(changeSpy)
const events = [{action: 'created', path: remotePath + '/test.txt'}]
onDidChangeFilesCallback(events)
expect(changeSpy).toHaveBeenCalledWith(events)
disposable.dispose()
})
})
}) })
describe('.open(path)', () => { describe('.open(path)', () => {

View File

@ -338,13 +338,21 @@ class Project extends Model {
} }
this.rootDirectories.push(directory) this.rootDirectories.push(directory)
this.watcherPromisesByPath[directory.getPath()] = watchPath(directory.getPath(), {}, events => {
const didChangeCallback = events => {
// Stop event delivery immediately on removal of a rootDirectory, even if its watcher // Stop event delivery immediately on removal of a rootDirectory, even if its watcher
// promise has yet to resolve at the time of removal // promise has yet to resolve at the time of removal
if (this.rootDirectories.includes(directory)) { if (this.rootDirectories.includes(directory)) {
this.emitter.emit('did-change-files', events) this.emitter.emit('did-change-files', events)
} }
}) }
// We'll use the directory's custom onDidChangeFiles callback, if available.
// CustomDirectory::onDidChangeFiles should match the signature of
// Project::onDidChangeFiles below (although it may resolve asynchronously)
this.watcherPromisesByPath[directory.getPath()] =
directory.onDidChangeFiles != null
? Promise.resolve(directory.onDidChangeFiles(didChangeCallback))
: watchPath(directory.getPath(), {}, didChangeCallback)
for (let watchedPath in this.watcherPromisesByPath) { for (let watchedPath in this.watcherPromisesByPath) {
if (!this.rootDirectories.find(dir => dir.getPath() === watchedPath)) { if (!this.rootDirectories.find(dir => dir.getPath() === watchedPath)) {