Browse Source

use debouncer

Steven Silvester 6 năm trước cách đây
mục cha
commit
33cb675a04
1 tập tin đã thay đổi với 14 bổ sung15 xóa
  1. 14 15
      packages/inspector/src/handler.ts

+ 14 - 15
packages/inspector/src/handler.ts

@@ -3,7 +3,7 @@
 
 import { CodeEditor } from '@jupyterlab/codeeditor';
 
-import { IDataConnector, Text, ActivityMonitor } from '@jupyterlab/coreutils';
+import { IDataConnector, Text, Debouncer } from '@jupyterlab/coreutils';
 
 import { MimeModel, IRenderMimeRegistry } from '@jupyterlab/rendermime';
 
@@ -25,6 +25,7 @@ export class InspectionHandler implements IDisposable, IInspector.IInspectable {
   constructor(options: InspectionHandler.IOptions) {
     this._connector = options.connector;
     this._rendermime = options.rendermime;
+    this._debouncer = new Debouncer(() => this.onEditorChange, 250);
   }
 
   /**
@@ -58,12 +59,9 @@ export class InspectionHandler implements IDisposable, IInspector.IInspectable {
     if (newValue === this._editor) {
       return;
     }
+    // Remove all of our listeners.
+    Signal.disconnectReceiver(this);
 
-    if (this._editor && !this._editor.isDisposed) {
-      this._monitors.forEach(monitor => {
-        monitor.activityStopped.disconnect(this.onEditorChange, this);
-      });
-    }
     let editor = (this._editor = newValue);
     if (editor) {
       // Clear the inspector in preparation for a new editor.
@@ -71,14 +69,8 @@ export class InspectionHandler implements IDisposable, IInspector.IInspectable {
       // Call onEditorChange to cover the case where the user changes
       // the active cell
       this.onEditorChange();
-      let signals: ISignal<any, any>[] = [
-        editor.model.selections.changed
-      ];
-      this._monitors = signals.map(s => {
-        let m = new ActivityMonitor({ signal: s, timeout: 250 });
-        m.activityStopped.connect(this.onEditorChange, this);
-        return m;
-      });
+      editor.model.selections.changed.connect(this._onChange, this);
+      editor.model.value.changed.connect(this._onChange, this);
     }
   }
 
@@ -171,6 +163,13 @@ export class InspectionHandler implements IDisposable, IInspector.IInspectable {
       });
   }
 
+  /**
+   * Handle changes to the editor state, debouncing.
+   */
+  private _onChange(): void {
+    this._debouncer.invoke();
+  }
+
   private _cleared = new Signal<InspectionHandler, void>(this);
   private _connector: IDataConnector<
     InspectionHandler.IReply,
@@ -184,7 +183,7 @@ export class InspectionHandler implements IDisposable, IInspector.IInspectable {
   private _pending = 0;
   private _rendermime: IRenderMimeRegistry;
   private _standby = true;
-  private _monitors: ActivityMonitor<any, any>[];
+  private _debouncer: Debouncer;
 }
 
 /**