Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Await pending directory change in filebrowser model. #5224

Merged
merged 2 commits into from Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions packages/filebrowser/src/model.ts
Expand Up @@ -238,7 +238,7 @@ export class FileBrowserModel implements IDisposable {
*
* @returns A promise with the contents of the directory.
*/
cd(newValue = '.'): Promise<void> {
async cd(newValue = '.'): Promise<void> {
if (newValue !== '.') {
newValue = Private.normalizePath(
this.manager.services.contents,
Expand All @@ -248,9 +248,13 @@ export class FileBrowserModel implements IDisposable {
} else {
newValue = this._pendingPath || this._model.path;
}
// Collapse requests to the same directory.
if (newValue === this._pendingPath && this._pending) {
return this._pending;
if (this._pending) {
// Collapse requests to the same directory.
if (newValue === this._pendingPath) {
return this._pending;
}
// Otherwise wait for the pending request to complete before continuing.
await this._pending;
}
let oldValue = this.path;
let options: Contents.IFetchOptions = { content: true };
Expand Down Expand Up @@ -289,7 +293,7 @@ export class FileBrowserModel implements IDisposable {
error.message = `Directory not found: "${this._model.path}"`;
console.error(error);
this._connectionFailure.emit(error);
this.cd('/');
return this.cd('/');
} else {
this._refreshDuration = this._baseRefreshDuration * 10;
this._connectionFailure.emit(error);
Expand Down
56 changes: 55 additions & 1 deletion tests/test-filebrowser/src/model.spec.ts
Expand Up @@ -11,7 +11,11 @@ import { DocumentManager, IDocumentManager } from '@jupyterlab/docmanager';

import { DocumentRegistry, TextModelFactory } from '@jupyterlab/docregistry';

import { ServiceManager } from '@jupyterlab/services';
import {
Contents,
ContentsManager,
ServiceManager
} from '@jupyterlab/services';

import {
FileBrowserModel,
Expand All @@ -26,6 +30,29 @@ import {
} from '@jupyterlab/testutils';
import { toArray } from '@phosphor/algorithm';

/**
* A contents manager that delays requests by less each time it is called
* in order to simulate out-of-order responses from the server.
*/
class DelayedContentsManager extends ContentsManager {
get(
path: string,
options?: Contents.IFetchOptions
): Promise<Contents.IModel> {
return new Promise<Contents.IModel>(resolve => {
const delay = this._delay;
this._delay -= 500;
super.get(path, options).then(contents => {
setTimeout(() => {
resolve(contents);
}, Math.max(delay, 0));
});
});
}

private _delay = 1000;
}

describe('filebrowser/model', () => {
let manager: IDocumentManager;
let serviceManager: ServiceManager.IManager;
Expand Down Expand Up @@ -223,6 +250,33 @@ describe('filebrowser/model', () => {
await model.cd('..');
expect(model.path).to.equal('');
});

it('should be resilient to a slow initial fetch', async () => {
let delayedServiceManager = new ServiceManager();
(delayedServiceManager as any).contents = new DelayedContentsManager();
let manager = new DocumentManager({
registry,
opener,
manager: delayedServiceManager
});
model = new FileBrowserModel({ manager, state });

const paths: string[] = [];
// An initial refresh is called in the constructor.
// If it is too slow, it can come in after the directory change,
// causing a directory set by, e.g., the tree handler to be wrong.
// This checks to make sure we are handling that case correctly.
const refresh = model.refresh().then(() => paths.push(model.path));
const cd = model.cd('src').then(() => paths.push(model.path));
await Promise.all([refresh, cd]);
expect(model.path).to.equal('src');
expect(paths).to.eql(['', 'src']);

manager.dispose();
delayedServiceManager.contents.dispose();
delayedServiceManager.dispose();
model.dispose();
});
});

describe('#restore()', () => {
Expand Down