Quellcode durchsuchen

Throttle setting registry fetch requests at the connector level

Afshin T. Darian vor 5 Jahren
Ursprung
Commit
bff474955e

+ 21 - 2
packages/apputils-extension/src/settingconnector.ts

@@ -1,8 +1,10 @@
 import { PageConfig } from '@jupyterlab/coreutils';
 
+import { ISettingRegistry } from '@jupyterlab/settingregistry';
+
 import { DataConnector, IDataConnector } from '@jupyterlab/statedb';
 
-import { ISettingRegistry } from '@jupyterlab/settingregistry';
+import { Throttler } from '@lumino/polling';
 
 /**
  * A data connector for fetching settings.
@@ -19,8 +21,24 @@ export class SettingConnector extends DataConnector<
     this._connector = connector;
   }
 
+  /**
+   * Fetch settings for a plugin.
+   * @param id - The plugin ID
+   *
+   * #### Notes
+   * The REST API requests are throttled at one request per plugin per 100ms.
+   */
   fetch(id: string): Promise<ISettingRegistry.IPlugin | undefined> {
-    return this._connector.fetch(id);
+    const throttlers = this._throttlers;
+    if (!(id in throttlers)) {
+      throttlers[id] = new Throttler(async () => {
+        const fetched = await this._connector.fetch(id);
+        throttlers[id].dispose();
+        delete throttlers[id];
+        return fetched;
+      }, 100);
+    }
+    return throttlers[id].invoke();
   }
 
   async list(
@@ -44,4 +62,5 @@ export class SettingConnector extends DataConnector<
   }
 
   private _connector: IDataConnector<ISettingRegistry.IPlugin, string>;
+  private _throttlers: { [key: string]: Throttler } = Object.create(null);
 }

+ 0 - 1
packages/docmanager-extension/package.json

@@ -48,7 +48,6 @@
     "@lumino/algorithm": "^1.2.3",
     "@lumino/coreutils": "^1.4.2",
     "@lumino/disposable": "^1.3.5",
-    "@lumino/polling": "^1.1.1",
     "@lumino/widgets": "^1.11.1"
   },
   "devDependencies": {

+ 1 - 12
packages/docmanager-extension/src/index.ts

@@ -44,8 +44,6 @@ import { IDisposable } from '@lumino/disposable';
 
 import { Widget } from '@lumino/widgets';
 
-import { Throttler } from '@lumino/polling';
-
 /**
  * The command IDs used by the document manager plugin.
  */
@@ -244,18 +242,9 @@ ${fileTypes}`;
       }
     });
 
-    // callback to registry change that ensures not to invoke reload method when there is already a promise that is pending
-    let reloadSettingsRegistry = () => {
-      let reloadThrottle = new Throttler(() =>
-        settingRegistry.reload(pluginId)
-      );
-
-      return reloadThrottle.invoke.bind(reloadThrottle);
-    };
-
     // If the document registry gains or loses a factory or file type,
     // regenerate the settings description with the available options.
-    registry.changed.connect(reloadSettingsRegistry());
+    registry.changed.connect(() => settingRegistry.reload(pluginId));
 
     return docManager;
   }