Procházet zdrojové kódy

Add `remove()` support.

Afshin Darian před 8 roky
rodič
revize
00bafc7b8b
2 změnil soubory, kde provedl 29 přidání a 8 odebrání
  1. 13 4
      src/statedb/index.ts
  2. 16 4
      src/statedb/plugin.ts

+ 13 - 4
src/statedb/index.ts

@@ -35,8 +35,8 @@ interface IStateDB {
    * 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()` and `save()`, it *is* necessary for using the
-   * `fetchNamespace()` method.
+   * requirement for `fetch()`, `remove()`, and `save()`, it *is* necessary for
+   * using the `fetchNamespace()` method.
    *
    * The promise returned by this method may be rejected if an error occurs in
    * retrieving the data. Non-existence of an `id` will succeed, however.
@@ -61,6 +61,15 @@ interface IStateDB {
    */
   fetchNamespace(namespace: string): Promise<JSONValue[]>;
 
+  /**
+   * Remove a value from the database.
+   *
+   * @param id - The identifier for the data being removed.
+   *
+   * @returns A promise that is rejected if remove fails and succeeds otherwise.
+   */
+  remove(id: string): Promise<void>;
+
   /**
    * Save a value in the database.
    *
@@ -74,8 +83,8 @@ interface IStateDB {
    * 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()` and `save()`, it *is* necessary for using the
-   * `fetchNamespace()` method.
+   * requirement for `fetch()`, `remove()`, and `save()`, it *is* necessary for
+   * using the `fetchNamespace()` method.
    */
   save(id: string, data: JSONValue): Promise<void>;
 }

+ 16 - 4
src/statedb/plugin.ts

@@ -40,8 +40,8 @@ class StateDB implements IStateDB {
    * 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()` and `save()`, it *is* necessary for using the
-   * `fetchNamespace()` method.
+   * requirement for `fetch()`, `remove()`, and `save()`, it *is* necessary for
+   * using the `fetchNamespace()` method.
    *
    * The promise returned by this method may be rejected if an error occurs in
    * retrieving the data. Non-existence of an `id` will succeed, however.
@@ -85,6 +85,18 @@ class StateDB implements IStateDB {
     return Promise.resolve(data);
   }
 
+  /**
+   * Remove a value from the database.
+   *
+   * @param id - The identifier for the data being removed.
+   *
+   * @returns A promise that is rejected if remove fails and succeeds otherwise.
+   */
+  remove(id: string): Promise<void> {
+    window.localStorage.removeItem(id);
+    return Promise.resolve(void 0);
+  }
+
   /**
    * Save a value in the database.
    *
@@ -98,8 +110,8 @@ class StateDB implements IStateDB {
    * 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()` and `save()`, it *is* necessary for using the
-   * `fetchNamespace()` method.
+   * requirement for `fetch()`, `remove()`, and `save()`, it *is* necessary for
+   * using the `fetchNamespace()` method.
    */
   save(id: string, data: JSONValue): Promise<void> {
     window.localStorage.setItem(id, JSON.stringify(data));