@jupyterlite/settings 0.1.0-alpha.9 → 0.1.0-beta.10

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,8 +1,34 @@
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
+ 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;
28
+ /**
29
+ * Create a settings store.
30
+ */
31
+ protected defaultSettingsStorage(): LocalForage;
6
32
  /**
7
33
  * Get settings by plugin id
8
34
  *
@@ -30,5 +56,22 @@ export declare class Settings {
30
56
  * @param pluginId The id of a plugin
31
57
  */
32
58
  private _getFederated;
59
+ private _storageName;
60
+ private _storageDrivers;
33
61
  private _storage;
62
+ private _localforage;
63
+ private _ready;
64
+ }
65
+ /**
66
+ * A namespace for settings metadata.
67
+ */
68
+ export declare namespace Settings {
69
+ /**
70
+ * Initialization options for settings.
71
+ */
72
+ interface IOptions {
73
+ localforage: typeof localforage;
74
+ storageName?: string | null;
75
+ storageDrivers?: string[] | null;
76
+ }
34
77
  }
package/lib/settings.js CHANGED
@@ -1,20 +1,67 @@
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
  */
7
- const STORAGE_NAME = 'JupyterLite Storage';
7
+ const DEFAULT_STORAGE_NAME = 'JupyterLite Storage';
8
8
  /**
9
9
  * A class to handle requests to /api/settings
10
10
  */
11
11
  export class Settings {
12
- constructor() {
13
- this._storage = localforage.createInstance({
14
- name: STORAGE_NAME,
12
+ constructor(options) {
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() {
43
+ this._storage = this.defaultSettingsStorage();
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
+ }
57
+ /**
58
+ * Create a settings store.
59
+ */
60
+ defaultSettingsStorage() {
61
+ return this._localforage.createInstance({
15
62
  description: 'Offline Storage for Settings',
16
63
  storeName: 'settings',
17
- version: 1
64
+ ...this.defaultStorageOptions,
18
65
  });
19
66
  }
20
67
  /**
@@ -40,15 +87,16 @@ export class Settings {
40
87
  async getAll() {
41
88
  var _a;
42
89
  const settingsUrl = (_a = PageConfig.getOption('settingsUrl')) !== null && _a !== void 0 ? _a : '/';
90
+ const storage = await this.storage;
43
91
  const all = (await (await fetch(URLExt.join(settingsUrl, 'all.json'))).json());
44
92
  const settings = await Promise.all(all.map(async (plugin) => {
45
93
  var _a;
46
94
  const { id } = plugin;
47
- 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;
48
96
  return {
49
97
  ...Private.override(plugin),
50
98
  raw,
51
- settings: json5.parse(raw)
99
+ settings: json5.parse(raw),
52
100
  };
53
101
  }));
54
102
  return { settings };
@@ -61,7 +109,7 @@ export class Settings {
61
109
  *
62
110
  */
63
111
  async save(pluginId, raw) {
64
- await this._storage.setItem(pluginId, raw);
112
+ await (await this.storage).setItem(pluginId, raw);
65
113
  }
66
114
  /**
67
115
  * Get the settings for a federated extension
@@ -79,14 +127,14 @@ export class Settings {
79
127
  const packageUrl = URLExt.join(labExtensionsUrl, packageName, 'package.json');
80
128
  const schema = await (await fetch(schemaUrl)).json();
81
129
  const packageJson = await (await fetch(packageUrl)).json();
82
- 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 : '{}';
83
131
  const settings = json5.parse(raw) || {};
84
132
  return Private.override({
85
133
  id: pluginId,
86
134
  raw,
87
135
  schema,
88
136
  settings,
89
- version: packageJson.version || '3.0.8'
137
+ version: packageJson.version || '3.0.8',
90
138
  });
91
139
  }
92
140
  }
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
  *
@@ -55,26 +59,3 @@ export interface ISettings {
55
59
  */
56
60
  save(pluginId: string, raw: string): Promise<void>;
57
61
  }
58
- /**
59
- * The interface for a federated extension, as it appears in `jupyter-config-data`
60
- *
61
- * TODO: sync with schema, Lab core, etc.
62
- */
63
- export interface IFederatedExtension {
64
- /**
65
- * The npm-compatible name of the package
66
- */
67
- name: string;
68
- /**
69
- * The relative path to the extension
70
- */
71
- extension: string;
72
- /**
73
- * The relative entrypoint to the WebPack remoteEntry
74
- */
75
- load: string;
76
- /**
77
- * Optional path to the style module
78
- */
79
- style?: string;
80
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jupyterlite/settings",
3
- "version": "0.1.0-alpha.9",
3
+ "version": "0.1.0-beta.10",
4
4
  "description": "JupyterLite - Settings",
5
5
  "homepage": "https://github.com/jupyterlite/jupyterlite",
6
6
  "bugs": {
@@ -42,21 +42,22 @@
42
42
  "watch": "tsc -b --watch"
43
43
  },
44
44
  "dependencies": {
45
- "@jupyterlab/coreutils": "~5.1.12",
46
- "@jupyterlab/settingregistry": "~3.1.12",
47
- "@lumino/coreutils": "^1.8.0",
45
+ "@jupyterlab/coreutils": "~5.4.3",
46
+ "@jupyterlab/settingregistry": "~3.4.3",
47
+ "@jupyterlite/localforage": "^0.1.0-beta.10",
48
+ "@lumino/coreutils": "^1.12.0",
48
49
  "json5": "^2.2.0",
49
50
  "localforage": "^1.9.0"
50
51
  },
51
52
  "devDependencies": {
52
53
  "@babel/core": "^7.11.6",
53
54
  "@babel/preset-env": "^7.12.1",
54
- "@jupyterlab/testutils": "~3.1.12",
55
+ "@jupyterlab/testutils": "~3.4.3",
55
56
  "@types/jest": "^26.0.10",
56
57
  "jest": "^26.4.2",
57
58
  "rimraf": "~3.0.0",
58
59
  "ts-jest": "^26.3.0",
59
- "typescript": "~4.2.3"
60
+ "typescript": "~4.5.2"
60
61
  },
61
62
  "publishConfig": {
62
63
  "access": "public"