Bläddra i källkod

Resolve #7889 to fetch only 1 checkpoint call per notebook

ikiw 5 år sedan
förälder
incheckning
2473e178e8
1 ändrade filer med 34 tillägg och 16 borttagningar
  1. 34 16
      packages/services/src/contents/index.ts

+ 34 - 16
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<Contents.ICheckpointModel[]>;
+} = {};
+
 /**
  * A namespace for contents interfaces.
  */
@@ -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];
   }
 
   /**