Skip to content

Commit fe50eb6

Browse files
author
Alan Fleming
committed
Added delays for getWidgetManager and get_model if the model isn't immediately available.
1 parent 6d69933 commit fe50eb6

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

packages/base-manager/src/manager-base.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,13 @@ export abstract class ManagerBase implements IWidgetManager {
215215
* If you would like to synchronously test if a model exists, use .has_model().
216216
*/
217217
async get_model(model_id: string): Promise<WidgetModel> {
218+
let i = 0;
219+
while (!this._models[model_id] && i < this._sleepTimes.length) {
220+
new Promise((r) => setTimeout(r, this._sleepTimes[i++]))
221+
}
218222
const modelPromise = this._models[model_id];
219223
if (modelPromise === undefined) {
220-
throw new Error('widget model not found');
224+
throw new Error(`widget model '${model_id}' not found`);
221225
}
222226
return modelPromise;
223227
}
@@ -874,6 +878,7 @@ export abstract class ManagerBase implements IWidgetManager {
874878
/**
875879
* Dictionary of model ids and model instance promises
876880
*/
881+
private _sleepTimes = [2, 50, 200, 800];
877882
private _models: { [key: string]: Promise<WidgetModel> } =
878883
Object.create(null);
879884
}

python/jupyterlab_widgets/src/manager.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -802,10 +802,15 @@ function configureRendermime(
802802
/**
803803
* Get the widgetManager that owns the model.
804804
*/
805-
export function getWidgetManager(model_id: string): KernelWidgetManager | null {
806-
for (const wManager of Private.kernelWidgetManagers.values()) {
807-
if (wManager.has_model(model_id)) {
808-
return wManager;
805+
export async function getWidgetManager(
806+
model_id: string
807+
): Promise<KernelWidgetManager | null> {
808+
for (const sleepTime of [0, 50, 1000]) {
809+
await new Promise((r) => setTimeout(r, sleepTime));
810+
for (const wManager of Private.kernelWidgetManagers.values()) {
811+
if (wManager.has_model(model_id)) {
812+
return wManager;
813+
}
809814
}
810815
}
811816
return null;

python/jupyterlab_widgets/src/renderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class WidgetRenderer
7373
return Promise.resolve();
7474
}
7575
if (!this._pendingManagerMessage && !this._managerIsSet) {
76-
this.manager = getWidgetManager(source.model_id);
76+
this.manager = await getWidgetManager(source.model_id);
7777
}
7878
this.node.textContent = `${
7979
this._pendingManagerMessage || model.data['text/plain']

0 commit comments

Comments
 (0)