Forráskód Böngészése

strict null checks for inspector

Steven Silvester 7 éve
szülő
commit
354a2cada2
2 módosított fájl, 38 hozzáadás és 51 törlés
  1. 11 8
      packages/inspector/src/handler.ts
  2. 27 43
      packages/inspector/src/inspector.ts

+ 11 - 8
packages/inspector/src/handler.ts

@@ -76,10 +76,10 @@ class InspectionHandler implements IDisposable, IInspector.IInspectable {
   /**
    * The editor widget used by the inspection handler.
    */
-  get editor(): CodeEditor.IEditor {
+  get editor(): CodeEditor.IEditor | null {
     return this._editor;
   }
-  set editor(newValue: CodeEditor.IEditor) {
+  set editor(newValue: CodeEditor.IEditor | null) {
     if (newValue === this._editor) {
       return;
     }
@@ -116,18 +116,17 @@ class InspectionHandler implements IDisposable, IInspector.IInspectable {
    * This is a read-only property.
    */
   get isDisposed(): boolean {
-    return this._editor === null;
+    return this._isDisposed;
   }
 
   /**
    * Dispose of the resources used by the handler.
    */
   dispose(): void {
-    if (this._editor === null) {
+    if (this.isDisposed) {
       return;
     }
-    this._editor = null;
-    this._rendermime = null;
+    this._isDisposed = true;
     this._disposed.emit(void 0);
     Signal.clearData(this);
   }
@@ -145,6 +144,9 @@ class InspectionHandler implements IDisposable, IInspector.IInspectable {
     }
 
     const editor = this.editor;
+    if (!editor) {
+      return;
+    }
     const code = editor.model.value.text;
     const position = editor.getCursorPosition();
     const offset = Text.jsIndexToCharIndex(editor.getOffsetAt(position), code);
@@ -200,12 +202,13 @@ class InspectionHandler implements IDisposable, IInspector.IInspectable {
   }
 
   private _disposed = new Signal<this, void>(this);
-  private _editor: CodeEditor.IEditor = null;
+  private _editor: CodeEditor.IEditor | null = null;
   private _ephemeralCleared = new Signal<InspectionHandler, void>(this);
   private _inspected = new Signal<this, IInspector.IInspectorUpdate>(this);
   private _pending = 0;
-  private _rendermime: RenderMime = null;
+  private _rendermime: RenderMime;
   private _standby = true;
+  private _isDisposed = false;
 }
 
 

+ 27 - 43
packages/inspector/src/inspector.ts

@@ -87,7 +87,7 @@ interface IInspector {
   /**
    * The source of events the inspector listens for.
    */
-  source: IInspector.IInspectable;
+  source: IInspector.IInspectable | null;
 }
 
 
@@ -168,7 +168,7 @@ namespace IInspector {
     /**
      * The content being sent to the inspector for display.
      */
-    content: Widget;
+    content: Widget | null;
 
     /**
      * The type of the inspector being updated.
@@ -194,10 +194,10 @@ class InspectorPanel extends TabPanel implements IInspector {
   /**
    * The source of events the inspector panel listens for.
    */
-  get source(): IInspector.IInspectable {
+  get source(): IInspector.IInspectable | null {
     return this._source;
   }
-  set source(source: IInspector.IInspectable) {
+  set source(source: IInspector.IInspectable | null) {
     if (this._source === source) {
       return;
     }
@@ -210,9 +210,7 @@ class InspectorPanel extends TabPanel implements IInspector {
     }
 
     // Clear the inspector child items (but maintain history) if necessary.
-    if (this._items) {
-      Object.keys(this._items).forEach(i => this._items[i].content = null);
-    }
+    Object.keys(this._items).forEach(i => this._items[i].content = null);
 
     this._source = source;
 
@@ -270,18 +268,14 @@ class InspectorPanel extends TabPanel implements IInspector {
    * Dispose of the resources held by the widget.
    */
   dispose(): void {
-    if (this._items == null) {
+    if (this.isDisposed) {
       return;
     }
-    let items = this._items;
-    this._items = null;
     this.source = null;
+    let items = this._items;
 
     // Dispose the inspector child items.
-    if (items) {
-      Object.keys(items).forEach(i => { items[i].dispose(); });
-    }
-
+    Object.keys(items).forEach(i => { items[i].dispose(); });
     super.dispose();
   }
 
@@ -329,16 +323,16 @@ class InspectorPanel extends TabPanel implements IInspector {
 
     // If the inspector was emptied, show the next best ranked inspector.
     let lowest = Infinity;
-    widget = null;
+    let newWidget: Widget | null = null;
     for (let type in items) {
       let inspector = this._items[type];
       if (inspector.rank < lowest && inspector.content) {
         lowest = inspector.rank;
-        widget = inspector;
+        newWidget = inspector;
       }
     }
-    if (widget) {
-      this.currentWidget = widget;
+    if (newWidget) {
+      this.currentWidget = newWidget;
     }
   }
 
@@ -350,7 +344,7 @@ class InspectorPanel extends TabPanel implements IInspector {
   }
 
   private _items: { [type: string]: InspectorItem } = Object.create(null);
-  private _source: IInspector.IInspectable = null;
+  private _source: IInspector.IInspectable | null = null;
 }
 
 
@@ -372,10 +366,10 @@ class InspectorItem extends Widget {
   /**
    * The text of the inspector.
    */
-  get content(): Widget {
+  get content(): Widget | null {
     return this._content;
   }
-  set content(newValue: Widget) {
+  set content(newValue: Widget | null) {
     if (newValue === this._content) {
       return;
     }
@@ -387,9 +381,9 @@ class InspectorItem extends Widget {
       }
     }
     this._content = newValue;
-    if (this._content) {
-      this._content.addClass(CONTENT_CLASS);
-      (this.layout as PanelLayout).addWidget(this._content);
+    if (newValue) {
+      newValue.addClass(CONTENT_CLASS);
+      (this.layout as PanelLayout).addWidget(newValue);
       if (this.remembers) {
         this._history.push(newValue);
         this._index++;
@@ -407,10 +401,9 @@ class InspectorItem extends Widget {
     if (newValue === this._remembers) {
       return;
     }
-    this._clear();
     this._remembers = newValue;
-    if (!this._remembers) {
-      this._history = null;
+    if (!newValue) {
+      this._clear();
     }
     this.update();
   }
@@ -429,18 +422,11 @@ class InspectorItem extends Widget {
    * Dispose of the resources held by the widget.
    */
   dispose(): void {
-    if (this._toolbar === null) {
+    if (this.isDisposed) {
       return;
     }
-    let toolbar = this._toolbar;
-    let history = this._history;
-    this._toolbar = null;
-    this._history = null;
-
-    if (history) {
-      history.forEach(widget => widget.dispose());
-    }
-    toolbar.dispose();
+    this._history.forEach(widget => widget.dispose());
+    this._toolbar.dispose();
     super.dispose();
   }
 
@@ -457,9 +443,7 @@ class InspectorItem extends Widget {
    * Clear history.
    */
   private _clear(): void {
-    if (this._history) {
-      this._history.forEach(widget => widget.dispose());
-    }
+    this._history.forEach(widget => widget.dispose());
     this._history = [];
     this._index = -1;
   }
@@ -519,10 +503,10 @@ class InspectorItem extends Widget {
     this._content.show();
   }
 
-  private _content: Widget = null;
-  private _history: Widget[] = null;
+  private _content: Widget | null = null;
+  private _history: Widget[] = [];
   private _index: number = -1;
   private _rank: number = Infinity;
   private _remembers: boolean = false;
-  private _toolbar: Toolbar<Widget> = null;
+  private _toolbar: Toolbar<Widget>;
 }