From 2473e178e8c30cbf299a96274cc584025f689b6b Mon Sep 17 00:00:00 2001 From: ikiw Date: Tue, 25 Feb 2020 23:06:09 +0530 Subject: [PATCH] Resolve #7889 to fetch only 1 checkpoint call per notebook --- packages/services/src/contents/index.ts | 50 +++++++++++++++++-------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/packages/services/src/contents/index.ts b/packages/services/src/contents/index.ts index 0377aa57fa86..b3505cad522f 100644 --- a/packages/services/src/contents/index.ts +++ b/packages/services/src/contents/index.ts @@ -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; +} = {}; + /** * A namespace for contents interfaces. */ @@ -1309,22 +1317,32 @@ export class Drive implements Contents.IDrive { */ listCheckpoints(localPath: string): Promise { 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]; } /**