Skip to content

Commit

Permalink
1.Resolve jupyterlab#7889 to fetch only 1 checkpoint call per noteboo…
Browse files Browse the repository at this point in the history
…k. 2.Refactor PR 7879 to use debouncer
  • Loading branch information
ikiw committed Feb 18, 2020
1 parent b66bb79 commit 3cc7de2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
14 changes: 6 additions & 8 deletions packages/docmanager-extension/src/index.ts
Expand Up @@ -44,6 +44,8 @@ import { IDisposable } from '@lumino/disposable';

import { Widget } from '@lumino/widgets';

import { Debouncer } from '@lumino/polling';

/**
* The command IDs used by the document manager plugin.
*/
Expand Down Expand Up @@ -244,15 +246,11 @@ ${fileTypes}`;

// callback to registry change that ensures not to invoke reload method when there is already a promise that is pending
let reloadSettingsRegistry = () => {
let promisePending = false;
let reloadDebounce = new Debouncer(() =>
settingRegistry.reload(pluginId)
);

return async () => {
if (!promisePending) {
promisePending = true;
await settingRegistry.reload(pluginId);
promisePending = false;
}
};
return reloadDebounce.invoke.bind(reloadDebounce);
};

// If the document registry gains or loses a factory or file type,
Expand Down
52 changes: 36 additions & 16 deletions packages/services/src/contents/index.ts
Expand Up @@ -27,6 +27,14 @@ const SERVICE_DRIVE_URL = 'api/contents';
*/
const FILES_URL = 'files';

/**
* The object to reference listCheckpoints promises
* used as a flag to check and return the existing promise if it's pending
*/
let checkpointPromiseQueue: {
[key: string]: Promise<Contents.ICheckpointModel[]>;
} = {};

/**
* A namespace for contents interfaces.
*/
Expand Down Expand Up @@ -1309,22 +1317,34 @@ export class Drive implements Contents.IDrive {
*/
listCheckpoints(localPath: string): Promise<Contents.ICheckpointModel[]> {
let url = this._getUrl(localPath, 'checkpoints');
return ServerConnection.makeRequest(url, {}, this.serverSettings)
.then(response => {
if (response.status !== 200) {
throw new ServerConnection.ResponseError(response);
}
return response.json();
})
.then(data => {
if (!Array.isArray(data)) {
throw new Error('Invalid Checkpoint list');
}
for (let i = 0; i < data.length; i++) {
validate.validateCheckpointModel(data[i]);
}
return data;
});

if (!checkpointPromiseQueue[localPath]) {
checkpointPromiseQueue[localPath] = ServerConnection.makeRequest(
url,
{},
this.serverSettings
)
.then(response => {
if (response.status !== 200) {
throw new ServerConnection.ResponseError(response);
}
return response.json();
})
.then(data => {
if (!Array.isArray(data)) {
throw new Error('Invalid Checkpoint list');
}
for (let i = 0; i < data.length; i++) {
validate.validateCheckpointModel(data[i]);
}
return data;
})
.finally(() => {
delete checkpointPromiseQueue[localPath];
});
}

return checkpointPromiseQueue[localPath];
}

/**
Expand Down

0 comments on commit 3cc7de2

Please sign in to comment.