@@ -377,7 +377,35 @@ export class CanvasManagerModel extends WidgetModel {
377
377
static model_module_version = MODULE_VERSION ;
378
378
}
379
379
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 > {
381
409
defaults ( ) {
382
410
return {
383
411
...super . defaults ( ) ,
@@ -394,14 +422,12 @@ export class Path2DModel extends WidgetModel {
394
422
this . value = new Path2D ( this . get ( 'value' ) ) ;
395
423
}
396
424
397
- value : Path2D ;
398
-
399
425
static model_name = 'Path2DModel' ;
400
426
static model_module = MODULE_NAME ;
401
427
static model_module_version = MODULE_VERSION ;
402
428
}
403
429
404
- export class PatternModel extends WidgetModel {
430
+ export class PatternModel extends AsyncValueWidgetModel < CanvasPattern > {
405
431
defaults ( ) {
406
432
return {
407
433
...super . defaults ( ) ,
@@ -430,7 +456,7 @@ export class PatternModel extends WidgetModel {
430
456
}
431
457
432
458
if ( patternSource == undefined ) {
433
- throw 'Could not understand the souce for the pattern' ;
459
+ throw 'Could not understand the source for the pattern' ;
434
460
}
435
461
436
462
const pattern = PatternModel . ctx . createPattern (
@@ -450,8 +476,6 @@ export class PatternModel extends WidgetModel {
450
476
image : { deserialize : unpack_models as any }
451
477
} ;
452
478
453
- value : CanvasPattern ;
454
-
455
479
static model_name = 'PatternModel' ;
456
480
static model_module = MODULE_NAME ;
457
481
static model_module_version = MODULE_VERSION ;
@@ -462,7 +486,7 @@ export class PatternModel extends WidgetModel {
462
486
) ;
463
487
}
464
488
465
- class GradientModel extends WidgetModel {
489
+ class GradientModel extends AsyncValueWidgetModel < CanvasGradient > {
466
490
defaults ( ) {
467
491
return {
468
492
...super . defaults ( ) ,
@@ -481,8 +505,10 @@ class GradientModel extends WidgetModel {
481
505
482
506
this . createGradient ( ) ;
483
507
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
+ }
486
512
}
487
513
}
488
514
@@ -495,8 +521,6 @@ class GradientModel extends WidgetModel {
495
521
) ;
496
522
}
497
523
498
- value : CanvasGradient ;
499
-
500
524
static model_module = MODULE_NAME ;
501
525
static model_module_version = MODULE_VERSION ;
502
526
@@ -1077,11 +1101,11 @@ export class CanvasModel extends DOMWidgetModel {
1077
1101
1078
1102
async setAttr ( attr : number , value : any ) {
1079
1103
if ( typeof value === 'string' && value . startsWith ( 'IPY' ) ) {
1080
- const widgetModel : GradientModel = await unpack_models (
1104
+ const widgetModel : AsyncValueWidgetModel < any > = await unpack_models (
1081
1105
value ,
1082
1106
this . widget_manager
1083
1107
) ;
1084
- value = widgetModel . value ;
1108
+ value = await widgetModel . initialized ( ) ;
1085
1109
}
1086
1110
1087
1111
( this . ctx as any ) [ CanvasModel . ATTRS [ attr ] ] = value ;
0 commit comments