plugin.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. JupyterLab, JupyterLabPlugin
  8. } from '../application';
  9. import {
  10. ICommandPalette
  11. } from '../commandpalette';
  12. import {
  13. IStateDB
  14. } from './index';
  15. import {
  16. StateDB
  17. } from './statedb';
  18. /**
  19. * The default state database for storing application state.
  20. */
  21. export
  22. const stateProvider: JupyterLabPlugin<IStateDB> = {
  23. id: 'jupyter.services.statedb',
  24. activate: activateState,
  25. autoStart: true,
  26. provides: IStateDB,
  27. requires: [ICommandPalette]
  28. };
  29. /**
  30. * Activate the state database.
  31. */
  32. function activateState(app: JupyterLab, palette: ICommandPalette): Promise<IStateDB> {
  33. let state = new StateDB();
  34. let jupyter = (window as any).jupyter;
  35. let version = jupyter ? jupyter.version : 'unknown';
  36. let command = 'statedb:clear';
  37. let category = 'Help';
  38. let key = 'statedb:version';
  39. let fetch = state.fetch(key);
  40. let save = () => state.save(key, { version });
  41. let reset = () => state.clear().then(save);
  42. let check = (value: JSONObject) => {
  43. let old = value && (value as any).version;
  44. if (!old || old !== version) {
  45. console.log(`Upgraded: ${old || 'unknown'} to ${version}; Resetting DB.`);
  46. return reset();
  47. }
  48. };
  49. app.commands.addCommand(command, {
  50. label: 'Clear Application Restore State',
  51. execute: () => state.clear()
  52. });
  53. palette.addItem({ command, category });
  54. return fetch.then(check, reset).then(() => state);
  55. }