diff --git a/src/ExecuteTimeWidget.ts b/src/ExecuteTimeWidget.ts index 439bc28..b8c40e5 100644 --- a/src/ExecuteTimeWidget.ts +++ b/src/ExecuteTimeWidget.ts @@ -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)); } /** @@ -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, diff --git a/src/index.ts b/src/index.ts index 92d5028..d5ac714 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; } @@ -22,10 +22,10 @@ class ExecuteTimeWidgetExtension implements DocumentRegistry.WidgetExtension { panel: NotebookPanel, context: DocumentRegistry.IContext ) { - 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; } @@ -36,14 +36,38 @@ const extension: JupyterFrontEndPlugin = { 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