@@ -377,6 +377,34 @@ export class CanvasManagerModel extends WidgetModel {
377
377
static model_module_version = MODULE_VERSION ;
378
378
}
379
379
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
+
380
408
export class Path2DModel extends WidgetModel {
381
409
defaults ( ) {
382
410
return {
@@ -401,7 +429,7 @@ export class Path2DModel extends WidgetModel {
401
429
static model_module_version = MODULE_VERSION ;
402
430
}
403
431
404
- export class PatternModel extends WidgetModel {
432
+ export class PatternModel extends AsyncValueWidgetModel < CanvasPattern > {
405
433
defaults ( ) {
406
434
return {
407
435
...super . defaults ( ) ,
@@ -430,7 +458,7 @@ export class PatternModel extends WidgetModel {
430
458
}
431
459
432
460
if ( patternSource == undefined ) {
433
- throw 'Could not understand the souce for the pattern' ;
461
+ throw 'Could not understand the source for the pattern' ;
434
462
}
435
463
436
464
const pattern = PatternModel . ctx . createPattern (
@@ -450,8 +478,6 @@ export class PatternModel extends WidgetModel {
450
478
image : { deserialize : unpack_models as any }
451
479
} ;
452
480
453
- value : CanvasPattern ;
454
-
455
481
static model_name = 'PatternModel' ;
456
482
static model_module = MODULE_NAME ;
457
483
static model_module_version = MODULE_VERSION ;
@@ -462,7 +488,7 @@ export class PatternModel extends WidgetModel {
462
488
) ;
463
489
}
464
490
465
- class GradientModel extends WidgetModel {
491
+ class GradientModel extends AsyncValueWidgetModel < CanvasGradient > {
466
492
defaults ( ) {
467
493
return {
468
494
...super . defaults ( ) ,
@@ -481,8 +507,10 @@ class GradientModel extends WidgetModel {
481
507
482
508
this . createGradient ( ) ;
483
509
484
- for ( const colorStop of this . get ( 'color_stops' ) ) {
485
- this . value . addColorStop ( colorStop [ 0 ] , colorStop [ 1 ] ) ;
510
+ if ( this . value ) {
511
+ for ( const colorStop of this . get ( 'color_stops' ) ) {
512
+ this . value . addColorStop ( colorStop [ 0 ] , colorStop [ 1 ] ) ;
513
+ }
486
514
}
487
515
}
488
516
@@ -495,8 +523,6 @@ class GradientModel extends WidgetModel {
495
523
) ;
496
524
}
497
525
498
- value : CanvasGradient ;
499
-
500
526
static model_module = MODULE_NAME ;
501
527
static model_module_version = MODULE_VERSION ;
502
528
@@ -1077,11 +1103,11 @@ export class CanvasModel extends DOMWidgetModel {
1077
1103
1078
1104
async setAttr ( attr : number , value : any ) {
1079
1105
if ( typeof value === 'string' && value . startsWith ( 'IPY' ) ) {
1080
- const widgetModel : GradientModel = await unpack_models (
1106
+ const widgetModel : AsyncValueWidgetModel < any > = await unpack_models (
1081
1107
value ,
1082
1108
this . widget_manager
1083
1109
) ;
1084
- value = widgetModel . value ;
1110
+ value = await widgetModel . initialized ( ) ;
1085
1111
}
1086
1112
1087
1113
( this . ctx as any ) [ CanvasModel . ATTRS [ attr ] ] = value ;
0 commit comments