فهرست منبع

Remove layout restorer references from instance tracker.

Afshin Darian 8 سال پیش
والد
کامیت
c60a168a51
2فایلهای تغییر یافته به همراه46 افزوده شده و 21 حذف شده
  1. 31 15
      src/application/instancetracker.ts
  2. 15 6
      src/apputils/layoutrestorer.ts

+ 31 - 15
src/application/instancetracker.ts

@@ -5,6 +5,10 @@ import {
   ArrayExt, each, find
 } from '@phosphor/algorithm';
 
+import {
+  JSONObject
+} from '@phosphor/coreutils';
+
 import {
   IDisposable
 } from '@phosphor/disposable';
@@ -29,10 +33,6 @@ import {
   Widget
 } from '@phosphor/widgets';
 
-import {
-  ILayoutRestorer
-} from '../apputils';
-
 import {
   IStateDB
 } from '../statedb';
@@ -205,7 +205,7 @@ class InstanceTracker<T extends Widget> implements IInstanceTracker<T>, IDisposa
 
     // Handle widget state restoration.
     if (this._restore) {
-      let { restorer, state } = this._restore;
+      let { state } = this._restore;
       let widgetName = this._restore.name(widget);
 
       if (widgetName) {
@@ -214,10 +214,6 @@ class InstanceTracker<T extends Widget> implements IInstanceTracker<T>, IDisposa
 
         Private.nameProperty.set(widget, name);
         promise = state.save(name, { data });
-
-        if (restorer) {
-          restorer.add(widget, name);
-        }
       }
     }
 
@@ -421,9 +417,10 @@ class InstanceTracker<T extends Widget> implements IInstanceTracker<T>, IDisposa
     if (!this._restore) {
       return;
     }
+
     // If restore data was saved, delete it from the database.
-    let { state } = this._restore;
-    let name = Private.nameProperty.get(widget);
+    const { state } = this._restore;
+    const name = Private.nameProperty.get(widget);
 
     if (name) {
       state.remove(name);
@@ -466,11 +463,21 @@ namespace InstanceTracker {
    * The state restoration configuration options.
    */
   export
-  interface IRestoreOptions<T extends Widget> extends ILayoutRestorer.IRestoreOptions<T> {
-    /*
-     * The layout restorer to use to recreate restored widgets.
+  interface IRestoreOptions<T extends Widget> {
+    /**
+     * The command to execute when restoring instances.
      */
-    restorer: ILayoutRestorer;
+    command: string;
+
+    /**
+     * A function that returns the args needed to restore an instance.
+     */
+    args: (widget: T) => JSONObject;
+
+    /**
+     * A function that returns a unique persistent name for this instance.
+     */
+    name: (widget: T) => string;
 
     /**
      * The command registry which holds the restore command.
@@ -481,6 +488,15 @@ namespace InstanceTracker {
      * The state database instance.
      */
     state: IStateDB;
+
+    /**
+     * The point after which it is safe to restore state.
+     *
+     * #### Notes
+     * By definition, this promise or promises will happen after the application
+     * has `started`.
+     */
+    when?: Promise<any> | Array<Promise<any>>;
   }
 }
 

+ 15 - 6
src/apputils/layoutrestorer.ts

@@ -233,26 +233,35 @@ class LayoutRestorer implements ILayoutRestorer {
    */
   restore(tracker: InstanceTracker<Widget>, options: ILayoutRestorer.IRestoreOptions<Widget>): Promise<any> {
     if (!this._promises) {
-      let warning = 'restore() can only be called before `first` has resolved.';
+      const warning = 'restore() can only be called before `first` has resolved.';
       console.warn(warning);
       return Promise.reject(warning);
     }
 
-    let { namespace } = tracker;
+    const { namespace } = tracker;
     if (this._trackers.has(namespace)) {
       let warning = `A tracker namespaced ${namespace} was already restored.`;
       console.warn(warning);
       return Promise.reject(warning);
     }
+
+    const { args, command, name, when } = options;
+
+    // Add the tracker to the private trackers collection.
     this._trackers.add(namespace);
 
-    let { args, command, name, when } = options;
-    let first = this._first;
+    // Whenever a new widget is added to the tracker, record its name.
+    tracker.widgetAdded.connect((sender: any, widget: Widget) => {
+      const widgetName = name(widget);
+      if (widgetName) {
+        this.add(widget, widgetName);
+      }
+    }, this);
 
-    let promise = tracker.restore({
+    const first = this._first;
+    const promise = tracker.restore({
       args, command, name,
       registry: this._registry,
-      restorer: this,
       state: this._state,
       when: when ? [first].concat(when) : first
     });