Skip to content

Commit

Permalink
Resolve jupyterlab#7889 to fetch only 1 checkpoint call per notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
ikiw committed Feb 25, 2020
1 parent d3c1ffb commit 2473e17
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 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 checkpointPromiseStore: {
[key: string]: Promise<Contents.ICheckpointModel[]>;
} = {};

/**
* A namespace for contents interfaces.
*/
Expand Down Expand Up @@ -1309,22 +1317,32 @@ 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 (!checkpointPromiseStore[localPath]) {
checkpointPromiseStore[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 checkpointPromiseStore[localPath];
});
}
return checkpointPromiseStore[localPath];
}

/**
Expand Down

0 comments on commit 2473e17

Please sign in to comment.