Skip to content

Commit 6e8c971

Browse files
committed
Fix pattern in JupyterLab
1 parent 46e1b09 commit 6e8c971

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

src/widget.ts

+38-14
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,35 @@ export class CanvasManagerModel extends WidgetModel {
377377
static model_module_version = MODULE_VERSION;
378378
}
379379

380-
export class Path2DModel extends WidgetModel {
380+
export class AsyncValueWidgetModel<ValueType> extends WidgetModel {
381+
initialize(attributes: any, options: any) {
382+
super.initialize(attributes, options);
383+
384+
this._initPromise = new Promise(resolve => {
385+
this._resolve = resolve;
386+
});
387+
}
388+
389+
async initialized(): Promise<ValueType> {
390+
return this._initPromise;
391+
}
392+
393+
set value(v: ValueType) {
394+
this._underlyingValue = v;
395+
this._resolve(v);
396+
}
397+
398+
get value(): ValueType | undefined {
399+
return this._underlyingValue;
400+
}
401+
402+
protected _underlyingValue: ValueType | undefined = undefined;
403+
404+
private _initPromise: Promise<ValueType>;
405+
private _resolve: (value: ValueType) => void;
406+
}
407+
408+
export class Path2DModel extends AsyncValueWidgetModel<Path2D> {
381409
defaults() {
382410
return {
383411
...super.defaults(),
@@ -394,14 +422,12 @@ export class Path2DModel extends WidgetModel {
394422
this.value = new Path2D(this.get('value'));
395423
}
396424

397-
value: Path2D;
398-
399425
static model_name = 'Path2DModel';
400426
static model_module = MODULE_NAME;
401427
static model_module_version = MODULE_VERSION;
402428
}
403429

404-
export class PatternModel extends WidgetModel {
430+
export class PatternModel extends AsyncValueWidgetModel<CanvasPattern> {
405431
defaults() {
406432
return {
407433
...super.defaults(),
@@ -430,7 +456,7 @@ export class PatternModel extends WidgetModel {
430456
}
431457

432458
if (patternSource == undefined) {
433-
throw 'Could not understand the souce for the pattern';
459+
throw 'Could not understand the source for the pattern';
434460
}
435461

436462
const pattern = PatternModel.ctx.createPattern(
@@ -450,8 +476,6 @@ export class PatternModel extends WidgetModel {
450476
image: { deserialize: unpack_models as any }
451477
};
452478

453-
value: CanvasPattern;
454-
455479
static model_name = 'PatternModel';
456480
static model_module = MODULE_NAME;
457481
static model_module_version = MODULE_VERSION;
@@ -462,7 +486,7 @@ export class PatternModel extends WidgetModel {
462486
);
463487
}
464488

465-
class GradientModel extends WidgetModel {
489+
class GradientModel extends AsyncValueWidgetModel<CanvasGradient> {
466490
defaults() {
467491
return {
468492
...super.defaults(),
@@ -481,8 +505,10 @@ class GradientModel extends WidgetModel {
481505

482506
this.createGradient();
483507

484-
for (const colorStop of this.get('color_stops')) {
485-
this.value.addColorStop(colorStop[0], colorStop[1]);
508+
if (this.value) {
509+
for (const colorStop of this.get('color_stops')) {
510+
this.value.addColorStop(colorStop[0], colorStop[1]);
511+
}
486512
}
487513
}
488514

@@ -495,8 +521,6 @@ class GradientModel extends WidgetModel {
495521
);
496522
}
497523

498-
value: CanvasGradient;
499-
500524
static model_module = MODULE_NAME;
501525
static model_module_version = MODULE_VERSION;
502526

@@ -1077,11 +1101,11 @@ export class CanvasModel extends DOMWidgetModel {
10771101

10781102
async setAttr(attr: number, value: any) {
10791103
if (typeof value === 'string' && value.startsWith('IPY')) {
1080-
const widgetModel: GradientModel = await unpack_models(
1104+
const widgetModel: AsyncValueWidgetModel<any> = await unpack_models(
10811105
value,
10821106
this.widget_manager
10831107
);
1084-
value = widgetModel.value;
1108+
value = await widgetModel.initialized();
10851109
}
10861110

10871111
(this.ctx as any)[CanvasModel.ATTRS[attr]] = value;

0 commit comments

Comments
 (0)