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

Resolve #7889 to avoid redundant checkpoint calls on loading notebook #7926

Merged
merged 1 commit into from Mar 7, 2020
Merged
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
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