forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settingconnector.ts
61 lines (51 loc) · 1.72 KB
/
settingconnector.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { PageConfig } from '@jupyterlab/coreutils';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { DataConnector, IDataConnector } from '@jupyterlab/statedb';
import { Throttler } from '@lumino/polling';
/**
* A data connector for fetching settings.
*
* #### Notes
* This connector adds a query parameter to the base services setting manager.
*/
export class SettingConnector extends DataConnector<
ISettingRegistry.IPlugin,
string
> {
constructor(connector: IDataConnector<ISettingRegistry.IPlugin, string>) {
super();
this._connector = connector;
}
/**
* Fetch settings for a plugin.
* @param id - The plugin ID
*
* #### Notes
* The REST API requests are throttled at one request per plugin per 100ms.
*/
fetch(id: string): Promise<ISettingRegistry.IPlugin | undefined> {
const throttlers = this._throttlers;
if (!(id in throttlers)) {
throttlers[id] = new Throttler(() => this._connector.fetch(id), 100);
}
return throttlers[id].invoke();
}
async list(
query: 'active' | 'all' = 'all'
): Promise<{ ids: string[]; values: ISettingRegistry.IPlugin[] }> {
const { isDeferred, isDisabled } = PageConfig.Extension;
let { ids, values } = await this._connector.list();
if (query === 'all') {
return { ids, values };
}
return {
ids: ids.filter(id => !isDeferred(id) && !isDisabled(id)),
values: values.filter(({ id }) => !isDeferred(id) && !isDisabled(id))
};
}
async save(id: string, raw: string): Promise<void> {
await this._connector.save(id, raw);
}
private _connector: IDataConnector<ISettingRegistry.IPlugin, string>;
private _throttlers: { [key: string]: Throttler } = Object.create(null);
}