@jupyterlite/settings 0.1.0-beta.2 → 0.1.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/settings.d.ts CHANGED
@@ -1,9 +1,30 @@
1
- import { IPlugin } from './tokens';
1
+ import type localforage from 'localforage';
2
+ import { IPlugin, ISettings } from './tokens';
2
3
  /**
3
4
  * A class to handle requests to /api/settings
4
5
  */
5
- export declare class Settings {
6
- constructor(options?: Settings.IOptions);
6
+ export declare class Settings implements ISettings {
7
+ constructor(options: Settings.IOptions);
8
+ /**
9
+ * A promise that resolves when the settings storage is fully initialized
10
+ */
11
+ get ready(): Promise<void>;
12
+ /**
13
+ * A lazy reference to initialized storage
14
+ */
15
+ protected get storage(): Promise<LocalForage>;
16
+ /**
17
+ * Finish any initialization after server has started and all extensions are applied.
18
+ */
19
+ initialize(): Promise<void>;
20
+ /**
21
+ * Prepare the storage
22
+ */
23
+ protected initStorage(): Promise<void>;
24
+ /**
25
+ * Get default options for localForage instances
26
+ */
27
+ protected get defaultStorageOptions(): LocalForageOptions;
7
28
  /**
8
29
  * Create a settings store.
9
30
  */
@@ -35,8 +56,11 @@ export declare class Settings {
35
56
  * @param pluginId The id of a plugin
36
57
  */
37
58
  private _getFederated;
38
- private _settingsStorageName;
59
+ private _storageName;
60
+ private _storageDrivers;
39
61
  private _storage;
62
+ private _localforage;
63
+ private _ready;
40
64
  }
41
65
  /**
42
66
  * A namespaces for settings metadata.
@@ -46,7 +70,9 @@ declare namespace Settings {
46
70
  * Initialization options for settings.
47
71
  */
48
72
  interface IOptions {
49
- settingsStorageName?: string | null;
73
+ localforage: typeof localforage;
74
+ storageName?: string | null;
75
+ storageDrivers?: string[] | null;
50
76
  }
51
77
  }
52
78
  export {};
package/lib/settings.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { PageConfig, URLExt } from '@jupyterlab/coreutils';
2
2
  import * as json5 from 'json5';
3
- import localforage from 'localforage';
3
+ import { PromiseDelegate } from '@lumino/coreutils';
4
4
  /**
5
5
  * The name of the local storage.
6
6
  */
@@ -10,20 +10,58 @@ const DEFAULT_STORAGE_NAME = 'JupyterLite Storage';
10
10
  */
11
11
  export class Settings {
12
12
  constructor(options) {
13
- this._settingsStorageName = DEFAULT_STORAGE_NAME;
14
- this._settingsStorageName =
15
- (options || {}).settingsStorageName || DEFAULT_STORAGE_NAME;
13
+ this._storageName = DEFAULT_STORAGE_NAME;
14
+ this._storageDrivers = null;
15
+ this._localforage = options.localforage;
16
+ this._storageName = options.storageName || DEFAULT_STORAGE_NAME;
17
+ this._storageDrivers = options.storageDrivers || null;
18
+ this._ready = new PromiseDelegate();
19
+ }
20
+ /**
21
+ * A promise that resolves when the settings storage is fully initialized
22
+ */
23
+ get ready() {
24
+ return this._ready.promise;
25
+ }
26
+ /**
27
+ * A lazy reference to initialized storage
28
+ */
29
+ get storage() {
30
+ return this.ready.then(() => this._storage);
31
+ }
32
+ /**
33
+ * Finish any initialization after server has started and all extensions are applied.
34
+ */
35
+ async initialize() {
36
+ await this.initStorage();
37
+ this._ready.resolve(void 0);
38
+ }
39
+ /**
40
+ * Prepare the storage
41
+ */
42
+ async initStorage() {
16
43
  this._storage = this.defaultSettingsStorage();
17
44
  }
45
+ /**
46
+ * Get default options for localForage instances
47
+ */
48
+ get defaultStorageOptions() {
49
+ var _a;
50
+ const driver = ((_a = this._storageDrivers) === null || _a === void 0 ? void 0 : _a.length) ? this._storageDrivers : null;
51
+ return {
52
+ version: 1,
53
+ name: this._storageName,
54
+ ...(driver ? { driver } : {}),
55
+ };
56
+ }
18
57
  /**
19
58
  * Create a settings store.
20
59
  */
21
60
  defaultSettingsStorage() {
22
- return localforage.createInstance({
23
- name: this._settingsStorageName,
61
+ return this._localforage.createInstance({
24
62
  description: 'Offline Storage for Settings',
25
63
  storeName: 'settings',
26
- version: 1,
64
+ ...this.defaultStorageOptions,
27
65
  });
28
66
  }
29
67
  /**
@@ -49,11 +87,12 @@ export class Settings {
49
87
  async getAll() {
50
88
  var _a;
51
89
  const settingsUrl = (_a = PageConfig.getOption('settingsUrl')) !== null && _a !== void 0 ? _a : '/';
90
+ const storage = await this.storage;
52
91
  const all = (await (await fetch(URLExt.join(settingsUrl, 'all.json'))).json());
53
92
  const settings = await Promise.all(all.map(async (plugin) => {
54
93
  var _a;
55
94
  const { id } = plugin;
56
- const raw = (_a = (await this._storage.getItem(id))) !== null && _a !== void 0 ? _a : plugin.raw;
95
+ const raw = (_a = (await storage.getItem(id))) !== null && _a !== void 0 ? _a : plugin.raw;
57
96
  return {
58
97
  ...Private.override(plugin),
59
98
  raw,
@@ -70,7 +109,7 @@ export class Settings {
70
109
  *
71
110
  */
72
111
  async save(pluginId, raw) {
73
- await this._storage.setItem(pluginId, raw);
112
+ await (await this.storage).setItem(pluginId, raw);
74
113
  }
75
114
  /**
76
115
  * Get the settings for a federated extension
@@ -88,7 +127,7 @@ export class Settings {
88
127
  const packageUrl = URLExt.join(labExtensionsUrl, packageName, 'package.json');
89
128
  const schema = await (await fetch(schemaUrl)).json();
90
129
  const packageJson = await (await fetch(packageUrl)).json();
91
- const raw = (_a = (await this._storage.getItem(pluginId))) !== null && _a !== void 0 ? _a : '{}';
130
+ const raw = (_a = (await (await this.storage).getItem(pluginId))) !== null && _a !== void 0 ? _a : '{}';
92
131
  const settings = json5.parse(raw) || {};
93
132
  return Private.override({
94
133
  id: pluginId,
package/lib/tokens.d.ts CHANGED
@@ -33,6 +33,10 @@ export interface IPlugin extends PartialJSONObject {
33
33
  * The interface for the Settings service.
34
34
  */
35
35
  export interface ISettings {
36
+ /**
37
+ * A promise that resolves after the settings have been full initialized
38
+ */
39
+ ready: Promise<void>;
36
40
  /**
37
41
  * Get settings by plugin id
38
42
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jupyterlite/settings",
3
- "version": "0.1.0-beta.2",
3
+ "version": "0.1.0-beta.3",
4
4
  "description": "JupyterLite - Settings",
5
5
  "homepage": "https://github.com/jupyterlite/jupyterlite",
6
6
  "bugs": {
@@ -42,8 +42,9 @@
42
42
  "watch": "tsc -b --watch"
43
43
  },
44
44
  "dependencies": {
45
- "@jupyterlab/coreutils": "~5.3.0",
46
- "@jupyterlab/settingregistry": "~3.3.0",
45
+ "@jupyterlab/coreutils": "~5.3.2",
46
+ "@jupyterlab/settingregistry": "~3.3.2",
47
+ "@jupyterlite/localforage": "^0.1.0-beta.3",
47
48
  "@lumino/coreutils": "^1.12.0",
48
49
  "json5": "^2.2.0",
49
50
  "localforage": "^1.9.0"
@@ -51,7 +52,7 @@
51
52
  "devDependencies": {
52
53
  "@babel/core": "^7.11.6",
53
54
  "@babel/preset-env": "^7.12.1",
54
- "@jupyterlab/testutils": "~3.3.0",
55
+ "@jupyterlab/testutils": "~3.3.2",
55
56
  "@types/jest": "^26.0.10",
56
57
  "jest": "^26.4.2",
57
58
  "rimraf": "~3.0.0",