settingclientdataconnector.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) Jupyter Development Team.
  3. | Distributed under the terms of the Modified BSD License.
  4. |----------------------------------------------------------------------------*/
  5. import {
  6. IDataConnector, ISettingRegistry, StateDB
  7. } from '@jupyterlab/coreutils';
  8. import {
  9. JSONObject
  10. } from '@phosphor/coreutils';
  11. /**
  12. * A client-side data connector for setting schemas.
  13. *
  14. * #### Notes
  15. * This class is deprecated. Its use is only as a storage mechanism of settings
  16. * data while an API for server-side persistence is being implemented.
  17. */
  18. export
  19. class SettingClientDataConnector extends StateDB implements IDataConnector<ISettingRegistry.IPlugin, JSONObject> {
  20. /**
  21. * Create a new setting client data connector.
  22. */
  23. constructor() {
  24. super({ namespace: 'setting-client-data-connector' });
  25. }
  26. /**
  27. * Retrieve a saved bundle from the data connector.
  28. */
  29. fetch(id: string): Promise<ISettingRegistry.IPlugin | undefined> {
  30. return super.fetch(id).then(user => {
  31. if (!user && !Private.schemas[id]) {
  32. return undefined;
  33. }
  34. user = user || { };
  35. const schema = Private.schemas[id] || { type: 'object' };
  36. const result = { data: { composite: { }, user }, id, schema };
  37. return result;
  38. });
  39. }
  40. /**
  41. * Remove a value from the data connector.
  42. */
  43. remove(id: string): Promise<void> {
  44. return super.remove(id);
  45. }
  46. /**
  47. * Save the user setting data in the data connector.
  48. */
  49. save(id: string, user: JSONObject): Promise<void> {
  50. return super.save(id, user);
  51. }
  52. }
  53. /**
  54. * A namespace for `SettingClientDataConnector` statics.
  55. */
  56. export
  57. namespace SettingClientDataConnector {
  58. /**
  59. * Preload the schema for a plugin.
  60. */
  61. export
  62. function preload(plugin: string, schema: ISettingRegistry.ISchema): void {
  63. Private.schemas[plugin] = schema;
  64. }
  65. }
  66. /**
  67. * A namespace for private module data.
  68. */
  69. namespace Private {
  70. /* tslint:disable */
  71. export
  72. const schemas: { [key: string]: ISettingRegistry.ISchema } = { };
  73. /* tslint:enable */
  74. }