Skip to content

Commit

Permalink
Added delays for getWidgetManager and get_model if the model isn't im…
Browse files Browse the repository at this point in the history
…mediately available.
  • Loading branch information
Alan Fleming committed Jun 2, 2024
1 parent 6d69933 commit fe50eb6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
7 changes: 6 additions & 1 deletion packages/base-manager/src/manager-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<WidgetModel> {
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;
}
Expand Down Expand Up @@ -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<WidgetModel> } =
Object.create(null);
}
Expand Down
13 changes: 9 additions & 4 deletions python/jupyterlab_widgets/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<KernelWidgetManager | null> {
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;
Expand Down
2 changes: 1 addition & 1 deletion python/jupyterlab_widgets/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down

0 comments on commit fe50eb6

Please sign in to comment.