Skip to content

Commit

Permalink
Load the settings once, without waiting for a notebook to open
Browse files Browse the repository at this point in the history
This fixes an issue where settings were not available until a notebook
was opened.
  • Loading branch information
krassowski committed May 16, 2024
1 parent a79b72e commit 179bd9a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
34 changes: 4 additions & 30 deletions src/ExecuteTimeWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,16 @@ export default class ExecuteTimeWidget extends Widget {
constructor(
panel: NotebookPanel,
tracker: INotebookTracker,
settingRegistry: ISettingRegistry
settings: ISettingRegistry.ISettings
) {
super();
this._panel = panel;
this._tracker = tracker;
this._settingRegistry = settingRegistry;

this.updateConnectedCell = this.updateConnectedCell.bind(this);
settingRegistry.load(`${PLUGIN_NAME}:settings`).then(
(settings: ISettingRegistry.ISettings) => {
this._updateSettings(settings);
settings.changed.connect(this._updateSettings.bind(this));

// If the plugin is enabled, force recording of timing
// We only do this once (not on every settings update) in case the user tries to turn it off
if (settings.get('enabled').composite) {
this._settingRegistry
.load('@jupyterlab/notebook-extension:tracker')
.then(
(nbSettings: ISettingRegistry.ISettings) =>
nbSettings.set('recordTiming', true),
(err: Error) => {
console.error(
`jupyterlab-execute-time: Could not force metadata recording: ${err}`
);
}
);
}
},
(err: Error) => {
console.error(
`jupyterlab-execute-time: Could not load settings, so did not active ${PLUGIN_NAME}: ${err}`
);
}
);

this._updateSettings(settings);
settings.changed.connect(this._updateSettings.bind(this));
}

/**
Expand Down Expand Up @@ -517,7 +492,6 @@ export default class ExecuteTimeWidget extends Widget {
} = {};
private _tracker: INotebookTracker;
private _panel: NotebookPanel;
private _settingRegistry: ISettingRegistry;
private _settings: IExecuteTimeSettings = {
enabled: false,
highlight: true,
Expand Down
36 changes: 30 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { DocumentRegistry } from '@jupyterlab/docregistry';
import ExecuteTimeWidget, { PLUGIN_NAME } from './ExecuteTimeWidget';

class ExecuteTimeWidgetExtension implements DocumentRegistry.WidgetExtension {
constructor(tracker: INotebookTracker, settingRegistry: ISettingRegistry) {
this._settingRegistry = settingRegistry;
constructor(tracker: INotebookTracker, settings: ISettingRegistry.ISettings) {
this._settings = settings;
this._tracker = tracker;
}

Expand All @@ -22,10 +22,10 @@ class ExecuteTimeWidgetExtension implements DocumentRegistry.WidgetExtension {
panel: NotebookPanel,
context: DocumentRegistry.IContext<INotebookModel>
) {
return new ExecuteTimeWidget(panel, this._tracker, this._settingRegistry);
return new ExecuteTimeWidget(panel, this._tracker, this._settings);
}

private _settingRegistry: ISettingRegistry;
private _settings: ISettingRegistry.ISettings;
private _tracker: INotebookTracker;
}

Expand All @@ -36,14 +36,38 @@ const extension: JupyterFrontEndPlugin<void> = {
id: PLUGIN_NAME,
autoStart: true,
requires: [INotebookTracker, ISettingRegistry],
activate: (
activate: async (
app: JupyterFrontEnd,
tracker: INotebookTracker,
settingRegistry: ISettingRegistry
) => {
let settings: ISettingRegistry.ISettings;
try {
settings = await settingRegistry.load(`${PLUGIN_NAME}:settings`);
} catch (err: unknown) {
console.error(
`jupyterlab-execute-time: Could not load settings, so did not active ${PLUGIN_NAME}: ${err}`
);
return;
}

// If the plugin is enabled, force recording of timing
// We only do this once (not on every settings update) in case the user tries to turn it off
if (settings.get('enabled').composite) {
settingRegistry.load('@jupyterlab/notebook-extension:tracker').then(
(nbSettings: ISettingRegistry.ISettings) =>
nbSettings.set('recordTiming', true),
(err: Error) => {
console.error(
`jupyterlab-execute-time: Could not force metadata recording: ${err}`
);
}
);
}

app.docRegistry.addWidgetExtension(
'Notebook',
new ExecuteTimeWidgetExtension(tracker, settingRegistry)
new ExecuteTimeWidgetExtension(tracker, settings)
);

// eslint-disable-next-line no-console
Expand Down

0 comments on commit 179bd9a

Please sign in to comment.