Browse Source

Merge pull request #727 from afshin/console-history

Console history
Steven Silvester 8 years ago
parent
commit
8d32724bd5
2 changed files with 67 additions and 35 deletions
  1. 66 34
      src/console/history.ts
  2. 1 1
      src/console/widget.ts

+ 66 - 34
src/console/history.ts

@@ -59,6 +59,16 @@ interface IConsoleHistory extends IDisposable {
  */
 export
 class ConsoleHistory implements IConsoleHistory {
+  /**
+   * Construct a new console history object.
+   */
+  constructor(options?: ConsoleHistory.IOptions) {
+    this._history = [];
+    if (options.kernel) {
+      this.kernel = options.kernel;
+    }
+  }
+
   /**
    * Get whether the console history manager is disposed.
    *
@@ -79,35 +89,27 @@ class ConsoleHistory implements IConsoleHistory {
     if (newValue === this._kernel) {
       return;
     }
-    let contents = Private.initialRequest;
+
     this._kernel = newValue;
-    this._kernel.history(contents).then((value: KernelMessage.IHistoryReplyMsg) => {
-      this._history = [];
-      let last = '';
-      let current = '';
-      for (let i = 0; i < value.content.history.length; i++) {
-        // History entries have the shape:
-        // [session: number, line: number, input: string]
-        // Contiguous duplicates are stripped out of the API response.
-        current = (value.content.history[i] as string[])[2];
-        if (current !== last) {
-          this._history.push(last = current);
-        }
-      }
-      // Reset the history navigation cursor back to the bottom.
-      this._cursor = this._history.length;
-    });
 
+    if (!this._kernel) {
+      this._history.length = 0;
+      return;
+    }
+
+    this._kernel.history(Private.initialRequest).then(v => this.onHistory(v));
   }
 
   /**
-   * Construct a new console history object.
+   * Dispose of the resources held by the console history manager.
    */
-  constructor(kernel: IKernel) {
-    this._history = [];
-    if (kernel) {
-      this.kernel = kernel;
+  dispose(): void {
+    if (this.isDisposed) {
+      return;
     }
+    clearSignalData(this);
+    this._history.length = 0;
+    this._history = null;
   }
 
   /**
@@ -121,18 +123,6 @@ class ConsoleHistory implements IConsoleHistory {
     return Promise.resolve(content);
   }
 
-  /**
-   * Dispose of the resources held by the console history manager.
-   */
-  dispose(): void {
-    if (this.isDisposed) {
-      return;
-    }
-    clearSignalData(this);
-    this._history.length = 0;
-    this._history = null;
-  }
-
   /**
    * Get the next item in the console history.
    *
@@ -164,12 +154,54 @@ class ConsoleHistory implements IConsoleHistory {
     this._cursor = this._history.length;
   }
 
+  /**
+   * Populate the history collection on history reply from a kernel.
+   *
+   * @param value The kernel message history reply.
+   *
+   * #### Notes
+   * History entries have the shape:
+   * [session: number, line: number, input: string]
+   * Contiguous duplicates are stripped out of the API response.
+   */
+  protected onHistory(value: KernelMessage.IHistoryReplyMsg): void {
+    this._history = [];
+    let last = '';
+    let current = '';
+    for (let i = 0; i < value.content.history.length; i++) {
+      current = (value.content.history[i] as string[])[2];
+      if (current !== last) {
+        this._history.push(last = current);
+      }
+    }
+    // Reset the history navigation cursor back to the bottom.
+    this._cursor = this._history.length;
+  }
+
   private _cursor = 0;
   private _history: string[] = null;
   private _kernel: IKernel = null;
 }
 
 
+/**
+ * A namespace for ConsoleHistory statics.
+ */
+export
+namespace ConsoleHistory {
+  /**
+   * The initialization options for a console history object.
+   */
+  export
+  interface IOptions {
+    /**
+     * The kernel instance to query for history.
+     */
+    kernel?: IKernel;
+  }
+}
+
+
 /**
  * A namespace for private data.
  */

+ 1 - 1
src/console/widget.ts

@@ -97,7 +97,7 @@ class ConsoleWidget extends Widget {
     this._rendermime = options.rendermime;
     this._session = options.session;
 
-    this._history = new ConsoleHistory(this._session.kernel);
+    this._history = new ConsoleHistory({ kernel: this._session.kernel });
 
     // Instantiate tab completion widget.
     let completion = options.completion || new CompletionWidget({