浏览代码

Make SearchInstance implement IDisposable

Andrew Schlaepfer 6 年之前
父节点
当前提交
fb7f998867
共有 1 个文件被更改,包括 33 次插入21 次删除
  1. 33 21
      packages/documentsearch-extension/src/searchinstance.ts

+ 33 - 21
packages/documentsearch-extension/src/searchinstance.ts

@@ -7,11 +7,12 @@ import { Signal } from '@phosphor/signaling';
 
 import { createSearchOverlay } from './searchoverlay';
 import { IDisplayState, ISearchProvider } from '.';
+import { IDisposable } from '@phosphor/disposable';
 
 /**
  * Represents a search on a single widget.
  */
-export class SearchInstance {
+export class SearchInstance implements IDisposable {
   constructor(widget: Widget, searchProvider: ISearchProvider) {
     this._widget = widget;
     this._activeProvider = searchProvider;
@@ -24,7 +25,7 @@ export class SearchInstance {
       this._highlightNext.bind(this),
       this._highlightPrevious.bind(this),
       this._startSearch.bind(this),
-      this._endSearch.bind(this)
+      this.dispose.bind(this)
     );
 
     // TODO: this does not update if the toolbar changes height.
@@ -77,32 +78,42 @@ export class SearchInstance {
     this._displayUpdateSignal.emit(this._displayState);
   }
 
-  private _startSearch(query: RegExp) {
+  private async _startSearch(query: RegExp) {
     // save the last query (or set it to the current query if this is the first)
     this._displayState.query = query;
-    let cleanupPromise = Promise.resolve();
     if (this._activeProvider) {
-      cleanupPromise = this._activeProvider.endSearch();
+      await this._activeProvider.endSearch();
     }
-    cleanupPromise.then(() =>
-      this._activeProvider.startSearch(query, this._widget).then(() => {
-        this.updateIndices();
-        // this signal should get injected when the widget is
-        // created and hooked up to react!
-        this._activeProvider.changed.connect(
-          this.updateIndices,
-          this
-        );
-      })
+    await this._activeProvider.startSearch(query, this._widget);
+    this.updateIndices();
+
+    // this signal should get injected when the widget is
+    // created and hooked up to react!
+    this._activeProvider.changed.connect(
+      this.updateIndices,
+      this
     );
   }
 
-  private _endSearch() {
-    this._activeProvider.endSearch().then(() => {
-      Signal.disconnectAll(this);
-      this._searchWidget.dispose();
-      this._activeProvider.changed.disconnect(this.updateIndices, this);
-    });
+  /**
+   * Dispose of the resources held by the search instance.
+   */
+  dispose() {
+    if (this.isDisposed) {
+      return;
+    }
+    this._isDisposed = true;
+
+    this._activeProvider.endSearch();
+    this._searchWidget.dispose();
+    Signal.clearData(this);
+  }
+
+  /**
+   * Test whether the tracker is disposed.
+   */
+  get isDisposed(): boolean {
+    return this._isDisposed;
   }
 
   private _highlightNext() {
@@ -145,4 +156,5 @@ export class SearchInstance {
   private _displayUpdateSignal = new Signal<this, IDisplayState>(this);
   private _activeProvider: ISearchProvider;
   private _searchWidget: Widget;
+  private _isDisposed = false;
 }