Forráskód Böngészése

Check JupyterLab version and if there is a statedb mismatch, clear the DB.

Afshin Darian 8 éve
szülő
commit
7e15d0a6c3
2 módosított fájl, 29 hozzáadás és 1 törlés
  1. 21 1
      src/statedb/plugin.ts
  2. 8 0
      src/statedb/statedb.ts

+ 21 - 1
src/statedb/plugin.ts

@@ -1,6 +1,10 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
+import {
+  JSONObject
+} from 'phosphor/lib/algorithm/json';
+
 import {
   JupyterLabPlugin
 } from '../application';
@@ -20,7 +24,23 @@ import {
 export
 const stateProvider: JupyterLabPlugin<IStateDB> = {
   id: 'jupyter.services.statedb',
-  activate: (): IStateDB => new StateDB(),
+  activate: activateState,
   autoStart: true,
   provides: IStateDB
 };
+
+
+function activateState(): Promise<IStateDB> {
+  let state = new StateDB();
+  let version = (window as any).jupyter.version;
+  let key = 'statedb:version';
+  let fetch = state.fetch(key);
+  let save = () => state.save(key, { version });
+  let overwrite = () => state.clear().then(save);
+  let check = (value: JSONObject) => {
+    if (!value || (value as any).version !== version) {
+      return overwrite();
+    }
+  };
+  return fetch.then(check, overwrite).then(() => state);
+}

+ 8 - 0
src/statedb/statedb.ts

@@ -20,6 +20,14 @@ class StateDB implements IStateDB {
    */
   readonly maxLength = 2000;
 
+  /**
+   * Clear the entire database.
+   */
+  clear(): Promise<void> {
+    window.localStorage.clear();
+    return Promise.resolve(void 0);
+  }
+
   /**
    * Retrieve a saved bundle from the database.
    *