Skip to content

Commit

Permalink
Move setting connector to its own file and clarify loading deferred p…
Browse files Browse the repository at this point in the history
…lugin settings logic in a comment.
  • Loading branch information
afshin committed Dec 4, 2019
1 parent 750dcfe commit 531b190
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 26 deletions.
33 changes: 7 additions & 26 deletions packages/apputils-extension/src/index.ts
Expand Up @@ -20,7 +20,6 @@ import {
} from '@jupyterlab/apputils';

import {
DataConnector,
Debouncer,
ISettingRegistry,
IStateDB,
Expand All @@ -39,6 +38,8 @@ import { DisposableDelegate } from '@phosphor/disposable';

import { Palette } from './palette';

import { SettingConnector } from './settingconnector';

import { themesPlugin, themesPaletteMenuPlugin } from './themeplugins';

/**
Expand Down Expand Up @@ -91,38 +92,18 @@ const paletteRestorer: JupyterFrontEndPlugin<void> = {
const settings: JupyterFrontEndPlugin<ISettingRegistry> = {
id: '@jupyterlab/apputils-extension:settings',
activate: async (app: JupyterFrontEnd): Promise<ISettingRegistry> => {
const { isDeferred, isDisabled } = PageConfig.Extension;
const connector = new (class SettingConnector extends DataConnector<
ISettingRegistry.IPlugin,
string
> {
fetch(id: string): Promise<ISettingRegistry.IPlugin> {
return app.serviceManager.settings.fetch(id);
}

async list(
query: 'active' | 'all' = 'all'
): Promise<{ ids: string[]; values: ISettingRegistry.IPlugin[] }> {
let { ids, values } = await app.serviceManager.settings.list();

if (query === 'all') {
return { ids, values };
}

return {
ids: ids.filter(id => !isDeferred(id) && !isDisabled(id)),
values: values.filter(({ id }) => !isDeferred(id) && !isDisabled(id))
};
}
})();
const { isDisabled } = PageConfig.Extension;
const connector = new SettingConnector(app.serviceManager.settings);

const registry = new SettingRegistry({
connector,
plugins: (await connector.list('active')).values
});

// If there are plugins that have schemas that are not in the setting
// registry after the application has restored, try to load them manually.
// registry after the application has restored, try to load them manually
// because otherwise, its settings will never become available in the
// setting registry.
void app.restored.then(async () => {
const plugins = await connector.list('all');
plugins.ids.forEach(async (id, index) => {
Expand Down
44 changes: 44 additions & 0 deletions packages/apputils-extension/src/settingconnector.ts
@@ -0,0 +1,44 @@
import {
DataConnector,
IDataConnector,
ISettingRegistry,
PageConfig
} from '@jupyterlab/coreutils';

/**
* 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(id: string): Promise<ISettingRegistry.IPlugin> {
return this._connector.fetch(id);
}

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))
};
}

private _connector: IDataConnector<ISettingRegistry.IPlugin, string>;
}

0 comments on commit 531b190

Please sign in to comment.