Browse Source

Strict null checks in image viewer

Steven Silvester 7 years ago
parent
commit
4c35f50ebf
1 changed files with 17 additions and 18 deletions
  1. 17 18
      packages/imageviewer/src/widget.ts

+ 17 - 18
packages/imageviewer/src/widget.ts

@@ -1,6 +1,10 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
+import {
+  PathExt
+} from '@jupyterlab/coreutils';
+
 import {
   ABCWidgetFactory, DocumentRegistry
 } from '@jupyterlab/docregistry';
@@ -34,7 +38,7 @@ class ImageViewer extends Widget implements DocumentRegistry.IReadyWidget {
    */
   constructor(context: DocumentRegistry.Context) {
     super({ node: Private.createNode() });
-    this._context = context;
+    this.context = context;
     this.node.tabIndex = -1;
     this.addClass(IMAGE_CLASS);
 
@@ -42,6 +46,9 @@ class ImageViewer extends Widget implements DocumentRegistry.IReadyWidget {
     context.pathChanged.connect(this._onTitleChanged, this);
 
     context.ready.then(() => {
+      if (this.isDisposed) {
+        return;
+      }
       this._render();
       context.model.contentChanged.connect(this.update, this);
       context.fileChanged.connect(this.update, this);
@@ -52,9 +59,7 @@ class ImageViewer extends Widget implements DocumentRegistry.IReadyWidget {
   /**
    * The image widget's context.
    */
-  get context(): DocumentRegistry.Context {
-    return this._context;
-  }
+  readonly context: DocumentRegistry.Context;
 
   /**
    * A promise that resolves when the image viewer is ready.
@@ -80,20 +85,11 @@ class ImageViewer extends Widget implements DocumentRegistry.IReadyWidget {
     scaleNode.style.transform = transform;
   }
 
-  /**
-   * Dispose of the resources used by the widget.
-   */
-  dispose(): void {
-    this._context = null;
-    super.dispose();
-  }
-
   /**
    * Handle `update-request` messages for the widget.
    */
   protected onUpdateRequest(msg: Message): void {
-    let context = this._context;
-    if (this.isDisposed || !context.isReady) {
+    if (this.isDisposed || !this.context.isReady) {
       return;
     }
     this._render();
@@ -110,21 +106,24 @@ class ImageViewer extends Widget implements DocumentRegistry.IReadyWidget {
    * Handle a change to the title.
    */
   private _onTitleChanged(): void {
-    this.title.label = this._context.path.split('/').pop();
+    this.title.label = PathExt.basename(this.context.path);
   }
 
   /**
    * Render the widget content.
    */
   private _render(): void {
-    let context = this._context;
+    let context = this.context;
     let cm = context.contentsModel;
+    if (!cm) {
+      return;
+    }
     let content = context.model.toString();
     let src = `data:${cm.mimetype};${cm.format},${content}`;
-    this.node.querySelector('img').setAttribute('src', src);
+    let node = this.node.querySelector('img') as HTMLImageElement;
+    node.setAttribute('src', src);
   }
 
-  private _context: DocumentRegistry.Context;
   private _scale = 1;
   private _ready = new PromiseDelegate<void>();
 }