瀏覽代碼

Make restore options generic.

Afshin Darian 8 年之前
父節點
當前提交
0d31860074

+ 3 - 3
src/common/instancetracker.ts

@@ -267,7 +267,7 @@ class InstanceTracker<T extends Widget> implements IInstanceTracker<T>, IDisposa
   /**
    * Restore the widgets in this tracker's namespace.
    */
-  restore(options: InstanceTracker.IRestoreOptions): Promise<any> {
+  restore(options: InstanceTracker.IRestoreOptions<T>): Promise<any> {
     this._restore = options;
 
     let { command, namespace, registry, state, when } = options;
@@ -359,7 +359,7 @@ class InstanceTracker<T extends Widget> implements IInstanceTracker<T>, IDisposa
   }
 
   private _currentWidget: T = null;
-  private _restore: InstanceTracker.IRestoreOptions = null;
+  private _restore: InstanceTracker.IRestoreOptions<T> = null;
   private _widgets = new Set<T>();
 }
 
@@ -378,7 +378,7 @@ namespace InstanceTracker {
    * The state restoration configuration options.
    */
   export
-  interface IRestoreOptions extends ILayoutRestorer.IRestoreOptions {
+  interface IRestoreOptions<T extends Widget> extends ILayoutRestorer.IRestoreOptions<T> {
     /*
      * The layout restorer to use to re-arrange restored tabs.
      */

+ 2 - 2
src/console/plugin.ts

@@ -153,8 +153,8 @@ function activateConsole(app: JupyterLab, services: IServiceManager, rendermime:
   layout.restore(tracker, {
     namespace: 'console',
     command: 'console:create',
-    args: (p: ConsolePanel) => ({ id: p.content.session.id }),
-    name: (p: ConsolePanel) => p.content.session && p.content.session.id,
+    args: panel => ({ id: panel.content.session.id }),
+    name: panel => panel.content.session && panel.content.session.id,
     when: manager.ready
   });
 

+ 2 - 2
src/csvwidget/plugin.ts

@@ -59,8 +59,8 @@ function activateCSVWidget(app: JupyterLab, registry: IDocumentRegistry, state:
   layout.restore(tracker, {
     namespace: 'csvwidget',
     command: 'file-operations:open',
-    args: (w: CSVWidget) => ({ path: w.context.path, factory: FACTORY }),
-    name: (w: CSVWidget) => w.context.path
+    args: widget => ({ path: widget.context.path, factory: FACTORY }),
+    name: widget => widget.context.path
   });
 
   registry.addWidgetFactory(factory);

+ 2 - 2
src/editorwidget/plugin.ts

@@ -93,8 +93,8 @@ function activateEditorHandler(app: JupyterLab, registry: IDocumentRegistry, sta
   layout.restore(tracker, {
     namespace: 'editor',
     command: 'file-operations:open',
-    args: (w: EditorWidget) => ({ path: w.context.path, factory: FACTORY }),
-    name: (w: EditorWidget) => w.context.path
+    args: widget => ({ path: widget.context.path, factory: FACTORY }),
+    name: widget => widget.context.path
   });
 
   // Sync tracker with currently focused widget.

+ 2 - 2
src/imagewidget/plugin.ts

@@ -74,8 +74,8 @@ function activateImageWidget(app: JupyterLab, registry: IDocumentRegistry, palet
   layout.restore(tracker, {
     namespace: 'imagewidget',
     command: 'file-operations:open',
-    args: (w: ImageWidget) => ({ path: w.context.path, factory: FACTORY }),
-    name: (w: ImageWidget) => w.context.path
+    args: widget => ({ path: widget.context.path, factory: FACTORY }),
+    name: widget => widget.context.path
   });
 
   // Sync tracker with currently focused widget.

+ 7 - 9
src/layoutrestorer/layoutrestorer.ts

@@ -75,7 +75,7 @@ interface ILayoutRestorer {
    *
    * @param options - The restoration options.
    */
-  restore(tracker: InstanceTracker<any>, options: ILayoutRestorer.IRestoreOptions): void;
+  restore(tracker: InstanceTracker<any>, options: ILayoutRestorer.IRestoreOptions<any>): void;
 }
 
 
@@ -99,7 +99,7 @@ namespace ILayoutRestorer {
    * The state restoration configuration options.
    */
   export
-  interface IRestoreOptions {
+  interface IRestoreOptions<T extends Widget> {
     /**
      * The command to execute when restoring instances.
      */
@@ -108,12 +108,12 @@ namespace ILayoutRestorer {
     /**
      * A function that returns the args needed to restore an instance.
      */
-    args: (widget: Widget) => JSONObject;
+    args: (widget: T) => JSONObject;
 
     /**
      * A function that returns a unique persistent name for this instance.
      */
-    name: (widget: Widget) => string;
+    name: (widget: T) => string;
 
     /**
      * The namespace to occupy in the state database for restoration data.
@@ -151,11 +151,10 @@ class LayoutRestorer implements ILayoutRestorer {
    * Create a layout restorer.
    */
   constructor(options: LayoutRestorer.IOptions) {
+    this._registry = options.registry;
     this._state = options.state;
-    this._first = options.first;
-    this._first.then(() => Promise.all(this._promises)).then(() => {
+    options.first.then(() => Promise.all(this._promises)).then(() => {
       // Release the promises held in memory.
-      this._first = null;
       this._promises = null;
       // Restore the application state.
       return this._restore();
@@ -190,7 +189,7 @@ class LayoutRestorer implements ILayoutRestorer {
    *
    * @param options - The restoration options.
    */
-  restore(tracker: InstanceTracker<any>, options: ILayoutRestorer.IRestoreOptions): void {
+  restore(tracker: InstanceTracker<any>, options: ILayoutRestorer.IRestoreOptions<any>): void {
     if (!this._promises) {
       console.warn('restore can only be called before app has started.');
       return;
@@ -244,7 +243,6 @@ class LayoutRestorer implements ILayoutRestorer {
     });
   }
 
-  private _first: Promise<any> = null;
   private _promises: Promise<any>[] = [];
   private _restored = new utils.PromiseDelegate<void>();
   private _registry: CommandRegistry = null;

+ 2 - 2
src/markdownwidget/plugin.ts

@@ -66,8 +66,8 @@ const plugin: JupyterLabPlugin<void> = {
     layout.restore(tracker, {
       namespace: 'rendered-markdown',
       command: 'file-operations:open',
-      args: (w: MarkdownWidget) => ({ path: w.context.path, factory: FACTORY }),
-      name: (w: MarkdownWidget) => w.context.path
+      args: widget => ({ path: widget.context.path, factory: FACTORY }),
+      name: widget => widget.context.path
     });
 
     let icon = `${PORTRAIT_ICON_CLASS} ${TEXTEDITOR_ICON_CLASS}`;

+ 2 - 2
src/notebook/plugin.ts

@@ -182,8 +182,8 @@ function activateNotebookHandler(app: JupyterLab, registry: IDocumentRegistry, s
   layout.restore(tracker, {
     namespace: 'notebook',
     command: 'file-operations:open',
-    args: (p: NotebookPanel) => ({ path: p.context.path, factory: FACTORY }),
-    name: (p: NotebookPanel) => p.context.path,
+    args: panel => ({ path: panel.context.path, factory: FACTORY }),
+    name: panel => panel.context.path,
     when: services.ready
   });
 

+ 2 - 2
src/terminal/plugin.ts

@@ -92,8 +92,8 @@ function activateTerminal(app: JupyterLab, services: IServiceManager, mainMenu:
   layout.restore(tracker, {
     namespace: 'terminal',
     command: newTerminalId,
-    args: (w: TerminalWidget) => ({ name: w.session.name }),
-    name: (w: TerminalWidget) => w.session && w.session.name
+    args: widget => ({ name: widget.session.name }),
+    name: widget => widget.session && widget.session.name
   });
 
   // Sync tracker with currently focused widget.