Quellcode durchsuchen

Create a LabStatus standalone class.

Afshin Darian vor 6 Jahren
Ursprung
Commit
9cfd7239d1

+ 4 - 4
packages/application-extension/src/index.tsx

@@ -162,7 +162,7 @@ const main: JupyterFrontEndPlugin<void> = {
     // For more information, see:
     // https://developer.mozilla.org/en/docs/Web/Events/beforeunload
     window.addEventListener('beforeunload', event => {
-      if (app.isDirty) {
+      if (app.status.isDirty) {
         return ((event as any).returnValue = message);
       }
     });
@@ -310,7 +310,8 @@ const notfound: JupyterFrontEndPlugin<void> = {
  */
 const busy: JupyterFrontEndPlugin<void> = {
   id: '@jupyterlab/application-extension:faviconbusy',
-  activate: async (app: JupyterFrontEnd, status: ILabStatus) => {
+  requires: [ILabStatus],
+  activate: async (_: JupyterFrontEnd, status: ILabStatus) => {
     status.busySignal.connect((_, isBusy) => {
       const favicon = document.querySelector(
         `link[rel="icon"]${isBusy ? '.idle.favicon' : '.busy.favicon'}`
@@ -335,7 +336,6 @@ const busy: JupyterFrontEndPlugin<void> = {
       }
     });
   },
-  requires: [ILabStatus],
   autoStart: true
 };
 
@@ -561,7 +561,7 @@ const status: JupyterFrontEndPlugin<ILabStatus> = {
     if (!(app instanceof JupyterLab)) {
       throw new Error(`${status.id} must be activated in JupyterLab.`);
     }
-    return app;
+    return app.status;
   },
   autoStart: true,
   provides: ILabStatus

+ 3 - 1
packages/application/src/index.ts

@@ -6,7 +6,7 @@ import '../style/index.css';
 
 export { JupyterFrontEnd, JupyterFrontEndPlugin } from './frontend';
 
-export { ILabStatus, JupyterLab } from './lab';
+export { JupyterLab } from './lab';
 
 export { ILayoutRestorer, LayoutRestorer } from './layoutrestorer';
 
@@ -15,3 +15,5 @@ export { IMimeDocumentTracker } from './mimerenderers';
 export { IRouter, Router } from './router';
 
 export { ILabShell, LabShell } from './shell';
+
+export { ILabStatus } from './status';

+ 6 - 126
packages/application/src/lab.ts

@@ -9,69 +9,18 @@ import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
 
 import { Token } from '@phosphor/coreutils';
 
-import { DisposableDelegate, IDisposable } from '@phosphor/disposable';
-
-import { ISignal, Signal } from '@phosphor/signaling';
-
 import { JupyterFrontEnd, JupyterFrontEndPlugin } from './frontend';
 
 import { createRendermimePlugins } from './mimerenderers';
 
 import { ILabShell, LabShell } from './shell';
 
-/* tslint:disable */
-/**
- * The application status token.
- */
-export const ILabStatus = new Token<ILabStatus>(
-  '@jupyterlab/application:ILabStatus'
-);
-/* tslint:enable */
-
-/**
- * An interface for JupyterLab-like application status functionality.
- */
-export interface ILabStatus {
-  /**
-   * A signal for when application changes its busy status.
-   */
-  readonly busySignal: ISignal<JupyterFrontEnd, boolean>;
-
-  /**
-   * A signal for when application changes its dirty status.
-   */
-  readonly dirtySignal: ISignal<JupyterFrontEnd, boolean>;
-
-  /**
-   * Whether the application is busy.
-   */
-  readonly isBusy: boolean;
-
-  /**
-   * Whether the application is dirty.
-   */
-  readonly isDirty: boolean;
-
-  /**
-   * Set the application state to busy.
-   *
-   * @returns A disposable used to clear the busy state for the caller.
-   */
-  setBusy(): IDisposable;
-
-  /**
-   * Set the application state to dirty.
-   *
-   * @returns A disposable used to clear the dirty state for the caller.
-   */
-  setDirty(): IDisposable;
-}
+import { LabStatus } from './status';
 
 /**
  * JupyterLab is the main application class. It is instantiated once and shared.
  */
-export class JupyterLab extends JupyterFrontEnd<ILabShell>
-  implements ILabStatus {
+export class JupyterLab extends JupyterFrontEnd<ILabShell> {
   /**
    * Construct a new JupyterLab object.
    */
@@ -80,8 +29,6 @@ export class JupyterLab extends JupyterFrontEnd<ILabShell>
     this.restored = this.shell.restored
       .then(() => undefined)
       .catch(() => undefined);
-    this._busySignal = new Signal(this);
-    this._dirtySignal = new Signal(this);
 
     // Create an IInfo dictionary from the options to override the defaults.
     const info = Object.keys(JupyterLab.defaultInfo).reduce(
@@ -163,23 +110,14 @@ export class JupyterLab extends JupyterFrontEnd<ILabShell>
   readonly restored: Promise<void>;
 
   /**
-   * The version of the JupyterLab application.
-   */
-  readonly version = PageConfig.getOption('appVersion') || 'unknown';
-
-  /**
-   * Returns a signal for when application changes its busy status.
+   * The application busy and dirty status signals and flags.
    */
-  get busySignal(): ISignal<JupyterLab, boolean> {
-    return this._busySignal;
-  }
+  readonly status = new LabStatus(this);
 
   /**
-   * Returns a signal for when application changes its dirty status.
+   * The version of the JupyterLab application.
    */
-  get dirtySignal(): ISignal<JupyterLab, boolean> {
-    return this._dirtySignal;
-  }
+  readonly version = PageConfig.getOption('appVersion') || 'unknown';
 
   /**
    * The JupyterLab application information dictionary.
@@ -188,20 +126,6 @@ export class JupyterLab extends JupyterFrontEnd<ILabShell>
     return this._info;
   }
 
-  /**
-   * Whether the application is busy.
-   */
-  get isBusy(): boolean {
-    return this._busyCount > 0;
-  }
-
-  /**
-   * Whether the application is dirty.
-   */
-  get isDirty(): boolean {
-    return this._dirtyCount > 0;
-  }
-
   /**
    * The JupyterLab application paths dictionary.
    */
@@ -209,46 +133,6 @@ export class JupyterLab extends JupyterFrontEnd<ILabShell>
     return this._paths;
   }
 
-  /**
-   * Set the application state to dirty.
-   *
-   * @returns A disposable used to clear the dirty state for the caller.
-   */
-  setDirty(): IDisposable {
-    const oldDirty = this.isDirty;
-    this._dirtyCount++;
-    if (this.isDirty !== oldDirty) {
-      this._dirtySignal.emit(this.isDirty);
-    }
-    return new DisposableDelegate(() => {
-      const oldDirty = this.isDirty;
-      this._dirtyCount = Math.max(0, this._dirtyCount - 1);
-      if (this.isDirty !== oldDirty) {
-        this._dirtySignal.emit(this.isDirty);
-      }
-    });
-  }
-
-  /**
-   * Set the application state to busy.
-   *
-   * @returns A disposable used to clear the busy state for the caller.
-   */
-  setBusy(): IDisposable {
-    const oldBusy = this.isBusy;
-    this._busyCount++;
-    if (this.isBusy !== oldBusy) {
-      this._busySignal.emit(this.isBusy);
-    }
-    return new DisposableDelegate(() => {
-      const oldBusy = this.isBusy;
-      this._busyCount--;
-      if (this.isBusy !== oldBusy) {
-        this._busySignal.emit(this.isBusy);
-      }
-    });
-  }
-
   /**
    * Register plugins from a plugin module.
    *
@@ -283,10 +167,6 @@ export class JupyterLab extends JupyterFrontEnd<ILabShell>
     });
   }
 
-  private _busyCount = 0;
-  private _busySignal: Signal<JupyterLab, boolean>;
-  private _dirtyCount = 0;
-  private _dirtySignal: Signal<JupyterLab, boolean>;
   private _info: JupyterLab.IInfo;
   private _paths: JupyterFrontEnd.IPaths;
 }

+ 144 - 0
packages/application/src/status.ts

@@ -0,0 +1,144 @@
+// Copyright (c) Jupyter Development Team.
+// Distributed under the terms of the Modified BSD License.
+
+import { Token } from '@phosphor/coreutils';
+
+import { DisposableDelegate, IDisposable } from '@phosphor/disposable';
+
+import { ISignal, Signal } from '@phosphor/signaling';
+
+import { JupyterFrontEnd } from './frontend';
+
+/* tslint:disable */
+/**
+ * The application status token.
+ */
+export const ILabStatus = new Token<ILabStatus>(
+  '@jupyterlab/application:ILabStatus'
+);
+/* tslint:enable */
+
+/**
+ * An interface for JupyterLab-like application status functionality.
+ */
+export interface ILabStatus {
+  /**
+   * A signal for when application changes its busy status.
+   */
+  readonly busySignal: ISignal<JupyterFrontEnd, boolean>;
+
+  /**
+   * A signal for when application changes its dirty status.
+   */
+  readonly dirtySignal: ISignal<JupyterFrontEnd, boolean>;
+
+  /**
+   * Whether the application is busy.
+   */
+  readonly isBusy: boolean;
+
+  /**
+   * Whether the application is dirty.
+   */
+  readonly isDirty: boolean;
+
+  /**
+   * Set the application state to busy.
+   *
+   * @returns A disposable used to clear the busy state for the caller.
+   */
+  setBusy(): IDisposable;
+
+  /**
+   * Set the application state to dirty.
+   *
+   * @returns A disposable used to clear the dirty state for the caller.
+   */
+  setDirty(): IDisposable;
+}
+
+/**
+ * The application status signals and flags class.
+ */
+export class LabStatus implements ILabStatus {
+  /**
+   * Construct a new  status object.
+   */
+  constructor(app: JupyterFrontEnd) {
+    this._busySignal = new Signal(app);
+    this._dirtySignal = new Signal(app);
+  }
+
+  /**
+   * Returns a signal for when application changes its busy status.
+   */
+  get busySignal(): ISignal<JupyterFrontEnd, boolean> {
+    return this._busySignal;
+  }
+
+  /**
+   * Returns a signal for when application changes its dirty status.
+   */
+  get dirtySignal(): ISignal<JupyterFrontEnd, boolean> {
+    return this._dirtySignal;
+  }
+
+  /**
+   * Whether the application is busy.
+   */
+  get isBusy(): boolean {
+    return this._busyCount > 0;
+  }
+
+  /**
+   * Whether the application is dirty.
+   */
+  get isDirty(): boolean {
+    return this._dirtyCount > 0;
+  }
+
+  /**
+   * Set the application state to dirty.
+   *
+   * @returns A disposable used to clear the dirty state for the caller.
+   */
+  setDirty(): IDisposable {
+    const oldDirty = this.isDirty;
+    this._dirtyCount++;
+    if (this.isDirty !== oldDirty) {
+      this._dirtySignal.emit(this.isDirty);
+    }
+    return new DisposableDelegate(() => {
+      const oldDirty = this.isDirty;
+      this._dirtyCount = Math.max(0, this._dirtyCount - 1);
+      if (this.isDirty !== oldDirty) {
+        this._dirtySignal.emit(this.isDirty);
+      }
+    });
+  }
+
+  /**
+   * Set the application state to busy.
+   *
+   * @returns A disposable used to clear the busy state for the caller.
+   */
+  setBusy(): IDisposable {
+    const oldBusy = this.isBusy;
+    this._busyCount++;
+    if (this.isBusy !== oldBusy) {
+      this._busySignal.emit(this.isBusy);
+    }
+    return new DisposableDelegate(() => {
+      const oldBusy = this.isBusy;
+      this._busyCount--;
+      if (this.isBusy !== oldBusy) {
+        this._busySignal.emit(this.isBusy);
+      }
+    });
+  }
+
+  private _busyCount = 0;
+  private _busySignal: Signal<JupyterFrontEnd, boolean>;
+  private _dirtyCount = 0;
+  private _dirtySignal: Signal<JupyterFrontEnd, boolean>;
+}