Browse Source

Update CSVWidget to use the new tracker restore functionality.

Afshin Darian 8 years ago
parent
commit
9dff5713d9
1 changed files with 24 additions and 26 deletions
  1. 24 26
      src/csvwidget/plugin.ts

+ 24 - 26
src/csvwidget/plugin.ts

@@ -1,10 +1,19 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
+import {
+  JSONObject
+} from 'phosphor/lib/algorithm/json';
+
+
 import {
   JupyterLab, JupyterLabPlugin
 } from '../application';
 
+import {
+  InstanceTracker
+} from '../common/instancetracker';
+
 import {
   IDocumentRegistry
 } from '../docregistry';
@@ -14,16 +23,10 @@ import {
 } from '../statedb';
 
 import {
-  CSVWidgetFactory
+  CSVWidget, CSVWidgetFactory
 } from './widget';
 
 
-/**
- * The state database namespace for CSV widgets.
- */
-const NAMESPACE = 'csvwidgets';
-
-
 /**
  * The table file handler extension.
  */
@@ -45,27 +48,22 @@ function activateCSVWidget(app: JupyterLab, registry: IDocumentRegistry, state:
     fileExtensions: ['.csv'],
     defaultFor: ['.csv']
   });
+  const tracker = new InstanceTracker<CSVWidget>({
+    restore: {
+      state,
+      command: 'file-operations:open',
+      args: widget => ({ path: widget.context.path }),
+      namespace: 'csvwidgets',
+      when: app.started,
+      registry: app.commands
+    }
+  });
 
   registry.addWidgetFactory(factory);
-
   factory.widgetCreated.connect((sender, widget) => {
-    // Add the CSV path to the state database.
-    let key = `${NAMESPACE}:${widget.context.path}`;
-    state.save(key, { path: widget.context.path });
-    // Remove the CSV path from the state database on disposal.
-    widget.disposed.connect(() => { state.remove(key); });
-    // Keep track of path changes in the state database.
-    widget.context.pathChanged.connect((sender, path) => {
-      state.remove(key);
-      key = `${NAMESPACE}:${path}`;
-      state.save(key, { path });
-    });
+    // Track the widget.
+    tracker.add(widget);
+    // Notify the instance tracker if restore data needs to update.
+    widget.context.pathChanged.connect(() => { tracker.save(widget); });
   });
-
-  // Reload any CSV widgets whose state has been stored.
-  Promise.all([state.fetchNamespace(NAMESPACE), app.started])
-    .then(([items]) => {
-      let open = 'file-operations:open';
-      items.forEach(item => { app.commands.execute(open, item.value); });
-    });
 }