plugin.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. JSONObject
  5. } from 'phosphor/lib/algorithm/json';
  6. import {
  7. JupyterLabPlugin
  8. } from '../application';
  9. import {
  10. IStateDB
  11. } from './index';
  12. import {
  13. StateDB
  14. } from './statedb';
  15. /**
  16. * The default state database for storing application state.
  17. */
  18. export
  19. const stateProvider: JupyterLabPlugin<IStateDB> = {
  20. id: 'jupyter.services.statedb',
  21. activate: activateState,
  22. autoStart: true,
  23. provides: IStateDB
  24. };
  25. /**
  26. * Activate the state database.
  27. */
  28. function activateState(): Promise<IStateDB> {
  29. let state = new StateDB();
  30. let version = (window as any).jupyter.version;
  31. let key = 'statedb:version';
  32. let fetch = state.fetch(key);
  33. let save = () => state.save(key, { version });
  34. let reset = () => state.clear().then(save);
  35. let check = (value: JSONObject) => {
  36. let old = value && (value as any).version;
  37. if (!old || old !== version) {
  38. console.log(`Upgraded: ${old || 'unknown'} to ${version}. Resetting DB.`);
  39. return reset();
  40. }
  41. };
  42. return fetch.then(check, reset).then(() => state);
  43. }