@@ -377,6 +377,34 @@ export class CanvasManagerModel extends WidgetModel {
377377 static model_module_version = MODULE_VERSION ;
378378}
379379
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+
380408export class Path2DModel extends WidgetModel {
381409 defaults ( ) {
382410 return {
@@ -401,7 +429,7 @@ export class Path2DModel extends WidgetModel {
401429 static model_module_version = MODULE_VERSION ;
402430}
403431
404- export class PatternModel extends WidgetModel {
432+ export class PatternModel extends AsyncValueWidgetModel < CanvasPattern > {
405433 defaults ( ) {
406434 return {
407435 ...super . defaults ( ) ,
@@ -430,7 +458,7 @@ export class PatternModel extends WidgetModel {
430458 }
431459
432460 if ( patternSource == undefined ) {
433- throw 'Could not understand the souce for the pattern' ;
461+ throw 'Could not understand the source for the pattern' ;
434462 }
435463
436464 const pattern = PatternModel . ctx . createPattern (
@@ -450,8 +478,6 @@ export class PatternModel extends WidgetModel {
450478 image : { deserialize : unpack_models as any }
451479 } ;
452480
453- value : CanvasPattern ;
454-
455481 static model_name = 'PatternModel' ;
456482 static model_module = MODULE_NAME ;
457483 static model_module_version = MODULE_VERSION ;
@@ -462,7 +488,7 @@ export class PatternModel extends WidgetModel {
462488 ) ;
463489}
464490
465- class GradientModel extends WidgetModel {
491+ class GradientModel extends AsyncValueWidgetModel < CanvasGradient > {
466492 defaults ( ) {
467493 return {
468494 ...super . defaults ( ) ,
@@ -481,8 +507,10 @@ class GradientModel extends WidgetModel {
481507
482508 this . createGradient ( ) ;
483509
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+ }
486514 }
487515 }
488516
@@ -495,8 +523,6 @@ class GradientModel extends WidgetModel {
495523 ) ;
496524 }
497525
498- value : CanvasGradient ;
499-
500526 static model_module = MODULE_NAME ;
501527 static model_module_version = MODULE_VERSION ;
502528
@@ -1077,11 +1103,11 @@ export class CanvasModel extends DOMWidgetModel {
10771103
10781104 async setAttr ( attr : number , value : any ) {
10791105 if ( typeof value === 'string' && value . startsWith ( 'IPY' ) ) {
1080- const widgetModel : GradientModel = await unpack_models (
1106+ const widgetModel : AsyncValueWidgetModel < any > = await unpack_models (
10811107 value ,
10821108 this . widget_manager
10831109 ) ;
1084- value = widgetModel . value ;
1110+ value = await widgetModel . initialized ( ) ;
10851111 }
10861112
10871113 ( this . ctx as any ) [ CanvasModel . ATTRS [ attr ] ] = value ;
0 commit comments