From fe50eb689b4afd0308811af66fb424a5dd0517c2 Mon Sep 17 00:00:00 2001 From: Alan Fleming <> Date: Sun, 2 Jun 2024 21:18:15 +1000 Subject: [PATCH] Added delays for getWidgetManager and get_model if the model isn't immediately available. --- packages/base-manager/src/manager-base.ts | 7 ++++++- python/jupyterlab_widgets/src/manager.ts | 13 +++++++++---- python/jupyterlab_widgets/src/renderer.ts | 2 +- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/base-manager/src/manager-base.ts b/packages/base-manager/src/manager-base.ts index 0806fa2b74..aa63ab24ed 100644 --- a/packages/base-manager/src/manager-base.ts +++ b/packages/base-manager/src/manager-base.ts @@ -215,9 +215,13 @@ export abstract class ManagerBase implements IWidgetManager { * If you would like to synchronously test if a model exists, use .has_model(). */ async get_model(model_id: string): Promise { + let i = 0; + while (!this._models[model_id] && i < this._sleepTimes.length) { + new Promise((r) => setTimeout(r, this._sleepTimes[i++])) + } const modelPromise = this._models[model_id]; if (modelPromise === undefined) { - throw new Error('widget model not found'); + throw new Error(`widget model '${model_id}' not found`); } return modelPromise; } @@ -874,6 +878,7 @@ export abstract class ManagerBase implements IWidgetManager { /** * Dictionary of model ids and model instance promises */ + private _sleepTimes = [2, 50, 200, 800]; private _models: { [key: string]: Promise } = Object.create(null); } diff --git a/python/jupyterlab_widgets/src/manager.ts b/python/jupyterlab_widgets/src/manager.ts index 0895122fb3..a1d9d73d97 100644 --- a/python/jupyterlab_widgets/src/manager.ts +++ b/python/jupyterlab_widgets/src/manager.ts @@ -802,10 +802,15 @@ function configureRendermime( /** * Get the widgetManager that owns the model. */ -export function getWidgetManager(model_id: string): KernelWidgetManager | null { - for (const wManager of Private.kernelWidgetManagers.values()) { - if (wManager.has_model(model_id)) { - return wManager; +export async function getWidgetManager( + model_id: string +): Promise { + for (const sleepTime of [0, 50, 1000]) { + await new Promise((r) => setTimeout(r, sleepTime)); + for (const wManager of Private.kernelWidgetManagers.values()) { + if (wManager.has_model(model_id)) { + return wManager; + } } } return null; diff --git a/python/jupyterlab_widgets/src/renderer.ts b/python/jupyterlab_widgets/src/renderer.ts index 6654ac99ed..6f0efbb731 100644 --- a/python/jupyterlab_widgets/src/renderer.ts +++ b/python/jupyterlab_widgets/src/renderer.ts @@ -73,7 +73,7 @@ export class WidgetRenderer return Promise.resolve(); } if (!this._pendingManagerMessage && !this._managerIsSet) { - this.manager = getWidgetManager(source.model_id); + this.manager = await getWidgetManager(source.model_id); } this.node.textContent = `${ this._pendingManagerMessage || model.data['text/plain']