瀏覽代碼

Clean up save method.

Afshin Darian 7 年之前
父節點
當前提交
fbe92a3472
共有 1 個文件被更改,包括 45 次插入36 次删除
  1. 45 36
      packages/coreutils/src/statedb.ts

+ 45 - 36
packages/coreutils/src/statedb.ts

@@ -216,6 +216,31 @@ class StateDB implements IStateDB {
     return Promise.resolve(undefined);
   }
 
+  /**
+   * Save a value in the database.
+   *
+   * @param id - The identifier for the data being saved.
+   *
+   * @param value - The data being saved.
+   *
+   * @returns A promise that is rejected if saving fails and succeeds otherwise.
+   *
+   * #### Notes
+   * The `id` values of stored items in the state database are formatted:
+   * `'namespace:identifier'`, which is the same convention that command
+   * identifiers in JupyterLab use as well. While this is not a technical
+   * requirement for `fetch()`, `remove()`, and `save()`, it *is* necessary for
+   * using the `fetchNamespace()` method.
+   */
+  save(id: string, value: ReadonlyJSONValue): Promise<void> {
+    try {
+      this._save(id, value);
+      return Promise.resolve(undefined);
+    } catch (error) {
+      return Promise.reject(error);
+    }
+  }
+
   /**
    * Return a serialized copy of the state database's entire contents.
    *
@@ -249,42 +274,6 @@ class StateDB implements IStateDB {
     return Promise.resolve(contents);
   }
 
-  /**
-   * Save a value in the database.
-   *
-   * @param id - The identifier for the data being saved.
-   *
-   * @param value - The data being saved.
-   *
-   * @returns A promise that is rejected if saving fails and succeeds otherwise.
-   *
-   * #### Notes
-   * The `id` values of stored items in the state database are formatted:
-   * `'namespace:identifier'`, which is the same convention that command
-   * identifiers in JupyterLab use as well. While this is not a technical
-   * requirement for `fetch()`, `remove()`, and `save()`, it *is* necessary for
-   * using the `fetchNamespace()` method.
-   */
-  save(id: string, value: ReadonlyJSONValue): Promise<void> {
-    try {
-      const key = `${this.namespace}:${id}`;
-      const envelope: Private.Envelope = { v: value };
-      const serialized = JSON.stringify(envelope);
-      const length = serialized.length;
-      const max = this.maxLength;
-
-      if (length > max) {
-        throw new Error(`Data length (${length}) exceeds maximum (${max})`);
-      }
-
-      window.localStorage.setItem(key, serialized);
-
-      return Promise.resolve(undefined);
-    } catch (error) {
-      return Promise.reject(error);
-    }
-  }
-
   /**
    * Clear the entire database.
    *
@@ -322,6 +311,26 @@ class StateDB implements IStateDB {
     localStorage.setItem(key, 'sentinel');
     when.then(() => { localStorage.removeItem(key); });
   }
+
+  /**
+   * Save a key and its value in the database.
+   *
+   * #### Notes
+   * Unlike the public `save` method, this method is synchronous.
+   */
+  private _save(id: string, value: ReadonlyJSONValue): void {
+    const key = `${this.namespace}:${id}`;
+    const envelope: Private.Envelope = { v: value };
+    const serialized = JSON.stringify(envelope);
+    const length = serialized.length;
+    const max = this.maxLength;
+
+    if (length > max) {
+      throw new Error(`Data length (${length}) exceeds maximum (${max})`);
+    }
+
+    window.localStorage.setItem(key, serialized);
+  }
 }
 
 /**