Selaa lähdekoodia

Use the service manager signals

Steven Silvester 8 vuotta sitten
vanhempi
commit
d31b2f7f90
4 muutettua tiedostoa jossa 73 lisäystä ja 37 poistoa
  1. 4 0
      src/filebrowser/listing.ts
  2. 30 19
      src/filebrowser/model.ts
  3. 32 12
      src/running/index.ts
  4. 7 6
      src/terminal/plugin.ts

+ 4 - 0
src/filebrowser/listing.ts

@@ -19,6 +19,10 @@ import {
   Message
 } from 'phosphor-messaging';
 
+import {
+  clearSignalData
+} from 'phosphor-signaling';
+
 import {
   Widget
 } from 'phosphor-widget';

+ 30 - 19
src/filebrowser/model.ts

@@ -17,6 +17,10 @@ import {
   ISignal, Signal, clearSignalData
 } from 'phosphor-signaling';
 
+import {
+  deepEqual
+} from '../notebook/common/json';
+
 
 /**
  * An implementation of a file browser model.
@@ -34,6 +38,7 @@ class FileBrowserModel implements IDisposable {
     this._manager = options.manager;
     this._model = { path: '', name: '/', type: 'directory', content: [] };
     this.cd();
+    this._manager.sessions.runningChanged.connect(this._onRunningChanged, this);
   }
 
   /**
@@ -128,10 +133,14 @@ class FileBrowserModel implements IDisposable {
     if (newValue === '.') {
       newValue = '';
     }
+    if (oldValue !== newValue) {
+      this._sessions = [];
+    }
     this._pending = this._manager.contents.get(newValue, options).then(contents => {
       this._model = contents;
-      return this._findSessions();
-    }).then(() => {
+      return this._manager.sessions.listRunning();
+    }).then(models => {
+      this._onRunningChanged(this._manager.sessions, models);
       if (oldValue !== newValue) {
         this.pathChanged.emit({
           name: 'path',
@@ -139,8 +148,8 @@ class FileBrowserModel implements IDisposable {
           newValue
         });
       }
-      this._pendingPath = '';
       this.refreshed.emit(void 0);
+      this._pendingPath = '';
     });
     return this._pending;
   }
@@ -354,25 +363,27 @@ class FileBrowserModel implements IDisposable {
   }
 
   /**
-   * Get the sessions for the current directory.
+   * Handle a change to the running sessions.
    */
-  private _findSessions(): Promise<void> {
+  private _onRunningChanged(sender: ISession.IManager, models: ISession.IModel[]): void {
+    if (deepEqual(models, this._sessions)) {
+      return;
+    }
     this._sessions = [];
-
-    return this._manager.sessions.listRunning().then(models => {
-      if (!models.length) {
-        return;
-      }
-      let paths = this._model.content.map((contents: IContents.IModel) => {
-        return contents.path;
-      });
-      for (let model of models) {
-        let index = paths.indexOf(model.notebook.path);
-        if (index !== -1) {
-          this._sessions.push(model);
-        }
-      }
+    if (!models.length) {
+      this.refreshed.emit(void 0);
+      return;
+    }
+    let paths = this._model.content.map((contents: IContents.IModel) => {
+      return contents.path;
     });
+    for (let model of models) {
+      let index = paths.indexOf(model.notebook.path);
+      if (index !== -1) {
+        this._sessions.push(model);
+      }
+    }
+    this.refreshed.emit(void 0);
   }
 
   private _maxUploadSizeMb = 15;

+ 32 - 12
src/running/index.ts

@@ -186,6 +186,9 @@ class RunningSessions extends Widget {
     sessionList.className = LIST_CLASS;
     sessionContainer.appendChild(sessionList);
     sessionNode.appendChild(sessionContainer);
+
+    this._manager.terminals.runningChanged.connect(this._onTerminalsChanged, this);
+    this._manager.sessions.runningChanged.connect(this._onSessionsChanged, this);
   }
 
   /**
@@ -239,20 +242,14 @@ class RunningSessions extends Widget {
    * Refresh the widget.
    */
   refresh(): Promise<void> {
+    let terminals = this._manager.terminals;
+    let sessions = this._manager.sessions;
     clearTimeout(this._refreshId);
-    return this._manager.terminals.listRunning().then(running => {
-      this._runningTerminals = running;
-      return this._manager.sessions.listRunning();
+    return terminals.listRunning().then(running => {
+      this._onTerminalsChanged(terminals, running);
+      return sessions.listRunning();
     }).then(running => {
-      // Strip out non-file backed sessions.
-      this._runningSessions = [];
-      for (let session of running) {
-        let name = session.notebook.path.split('/').pop();
-        if (name.indexOf('.') !== -1) {
-          this._runningSessions.push(session);
-        }
-      }
-      this.update();
+      this._onSessionsChanged(sessions, running);
       this._refreshId = setTimeout(() => {
         this.refresh();
       }, REFRESH_DURATION);
@@ -389,6 +386,29 @@ class RunningSessions extends Widget {
     }
   }
 
+  /**
+   * Handle a change to the running sessions.
+   */
+  private _onSessionsChanged(sender: ISession.IManager, models: ISession.IModel[]): void {
+    // Strip out non-file backed sessions.
+    this._runningSessions = [];
+    for (let session of models) {
+      let name = session.notebook.path.split('/').pop();
+      if (name.indexOf('.') !== -1) {
+        this._runningSessions.push(session);
+      }
+    }
+    this.update();
+  }
+
+  /**
+   * Handle a change to the running terminals.
+   */
+  private _onTerminalsChanged(sender: ITerminalSession.IManager, models: ITerminalSession.IModel[]): void {
+    this._runningTerminals = models;
+    this.update();
+  }
+
   private _manager: IServiceManager = null;
   private _renderer: RunningSessions.IRenderer = null;
   private _runningSessions: ISession.IModel[] = [];

+ 7 - 6
src/terminal/plugin.ts

@@ -18,11 +18,11 @@ import {
 } from './index';
 
 import {
-  MainMenu, mainMenuProvider
+  MainMenu
 } from '../mainmenu/plugin';
 
 import {
-  MenuItem, Menu, IMenuItemOptions, MenuItemType
+  MenuItem, Menu
 } from 'phosphor-menus';
 
 
@@ -72,6 +72,8 @@ function activateTerminal(app: Application, services: ServiceManager, mainMenu:
         tracker.addWidget(term);
         services.terminals.create().then(session => {
           term.session = session;
+          // Trigger an update of the running kernels.
+          services.terminals.listRunning();
         });
       }
     },
@@ -167,14 +169,13 @@ function activateTerminal(app: Application, services: ServiceManager, mainMenu:
       if (options.background === 'black') {
         options.background = 'white';
         options.color = 'black';
-      }
-      else {
+      } else {
         options.background = 'black';
         options.color = 'white';
       }
       for (let i = 0; i < widgets.length; i++) {
-          widgets[i].background = options.background;
-          widgets[i].color = options.color;
+        widgets[i].background = options.background;
+        widgets[i].color = options.color;
       }
     }
   }