Browse Source

Add convenience getValue/setValue functions to `IModelDB`.

Fixes.
Ian Rose 8 years ago
parent
commit
afe67f2058

+ 5 - 5
packages/cells/src/model.ts

@@ -18,7 +18,7 @@ import {
 } from '@jupyterlab/coreutils';
 
 import {
-  IObservableJSON, IModelDB, IObservableValue
+  IObservableJSON, IModelDB
 } from '@jupyterlab/coreutils';
 
 import {
@@ -193,7 +193,7 @@ class CellModel extends CodeEditor.Model implements ICellModel {
    * Get the trusted state of the model.
    */
   get trusted(): boolean {
-    return (this.modelDB.get('trusted') as IObservableValue).get() as boolean;
+    return this.modelDB.getValue('trusted') as boolean;
   }
 
   /**
@@ -204,7 +204,7 @@ class CellModel extends CodeEditor.Model implements ICellModel {
     if (oldValue === newValue) {
       return;
     }
-    (this.modelDB.get('trusted') as IObservableValue).set(newValue);
+    this.modelDB.setValue('trusted', newValue);
   }
 
   /**
@@ -358,14 +358,14 @@ class CodeCellModel extends CellModel implements ICodeCellModel {
    * The execution count of the cell.
    */
   get executionCount(): nbformat.ExecutionCount {
-    return (this.modelDB.get('executionCount') as IObservableValue).get() as number || null;
+    return this.modelDB.getValue('executionCount') as nbformat.ExecutionCount;
   }
   set executionCount(newValue: nbformat.ExecutionCount) {
     let oldValue = this.executionCount;
     if (newValue === oldValue) {
       return;
     }
-    (this.modelDB.get('executionCount') as IObservableValue).set(newValue || null);
+    this.modelDB.setValue('executionCount', newValue || null);
   }
 
   /**

+ 3 - 3
packages/codeeditor/src/editor.ts

@@ -14,7 +14,7 @@ import {
 } from '@phosphor/signaling';
 
 import {
-  IModelDB, ModelDB, IObservableValue,
+  IModelDB, ModelDB,
   IObservableMap, IObservableString, IChangedArgs
 } from '@jupyterlab/coreutils';
 
@@ -226,14 +226,14 @@ namespace CodeEditor {
      * A mime type of the model.
      */
     get mimeType(): string {
-      return (this.modelDB.get('mimeType') as IObservableValue).get() as string;
+      return this.modelDB.getValue('mimeType') as string;
     }
     set mimeType(newValue: string) {
       const oldValue = this.mimeType;
       if (oldValue === newValue) {
         return;
       }
-      (this.modelDB.get('mimeType') as IObservableValue).set(newValue);
+      this.modelDB.setValue('mimeType', newValue);
     }
 
     /**

+ 57 - 3
packages/coreutils/src/modeldb.ts

@@ -65,7 +65,7 @@ interface IObservableValue extends IObservable {
   /**
    * The type of this object.
    */
-  type: 'Value';
+  readonly type: 'Value';
 
   /**
    * The changed signal.
@@ -185,7 +185,7 @@ interface IModelDB extends IDisposable {
    *
    * @param path: the path for the object.
    *
-   * @returns the object that wsa created.
+   * @returns the object that was created.
    */
   createJSON(path: string): IObservableJSON;
 
@@ -198,6 +198,24 @@ interface IModelDB extends IDisposable {
    */
   createValue(path: string): IObservableValue;
 
+  /**
+   * Get a value at a path. That value must already have
+   * been created using `createValue`.
+   *
+   * @param path: the path for the value.
+   */
+  getValue(path: string): JSONValue;
+
+  /**
+   * Set a value at a path. That value must already have
+   * been created using `createValue`.
+   *
+   * @param path: the path for the value.
+   *
+   * @param value: the new value.
+   */
+  setValue(path: string, value: JSONValue): void;
+
   /**
    * Create a view onto a subtree of the model database.
    *
@@ -231,7 +249,9 @@ class ObservableValue implements IObservableValue {
   /**
    * The observable type.
    */
-  readonly type: 'Value';
+  get type(): 'Value' {
+    return 'Value';
+  }
 
   /**
    * Whether the value has been disposed.
@@ -476,6 +496,40 @@ class ModelDB implements IModelDB {
     return val;
   }
 
+  /**
+   * Get a value at a path. That value must already have
+   * been created using `createValue`.
+   *
+   * @param path: the path for the value.
+   */
+  getValue(path: string): JSONValue {
+    let val = this.get(path);
+    if (val.type !== 'Value') {
+        throw Error('Can only call getValue for an ObservableValue');
+    }
+    return (val as ObservableValue).get();
+  }
+
+
+  /**
+
+  /**
+   * Set a value at a path. That value must already have
+   * been created using `createValue`.
+   *
+   * @param path: the path for the value.
+   *
+   * @param value: the new value.
+   */
+  setValue(path: string, value: JSONValue): void {
+    let val = this.get(path);
+    if (val.type !== 'Value') {
+        throw Error('Can only call setValue on an ObservableValue');
+    }
+    (val as ObservableValue).set(value);
+  }
+
+
   /**
    * Create a view onto a subtree of the model database.
    *

+ 3 - 1
packages/coreutils/src/observablemap.ts

@@ -120,7 +120,9 @@ class ObservableMap<T> implements IObservableMap<T> {
   /**
    * The type of the Observable.
    */
-  type: 'Map';
+  get type(): 'Map' {
+    return 'Map';
+  }
 
 
   /**

+ 3 - 1
packages/coreutils/src/observablestring.ts

@@ -79,7 +79,9 @@ class ObservableString implements IObservableString {
   /**
    * The type of the Observable.
    */
-  type: 'String';
+  get type(): 'String' {
+    return 'String';
+  }
 
   /**
    * A signal emitted when the string has changed.

+ 3 - 1
packages/coreutils/src/observablevector.ts

@@ -322,7 +322,9 @@ class ObservableVector<T> extends Vector<T> implements IObservableVector<T> {
   /**
    * The type of the Observable.
    */
-  type: 'Vector';
+  get type(): 'Vector' {
+    return 'Vector';
+  }
 
   /**
    * A signal emitted when the vector has changed.

+ 0 - 1
packages/notebook/src/model.ts

@@ -331,7 +331,6 @@ namespace NotebookModel {
      */
     contentFactory?: IContentFactory;
 
-
     /**
      * An optional modelDB for storing notebook data.
      */