@@ -242,33 +242,18 @@ export class WidgetManager {
242
242
const model = await this . manager . get_model ( model_id ) ;
243
243
const { buffer_paths, buffers } =
244
244
await this . ipywidgets_state . get_model_buffers ( model_id ) ;
245
- log ( "handleBuffersChange: " , model_id , buffer_paths ) ;
245
+ log ( "handleBuffersChange: " , { model_id, buffer_paths, buffers } ) ;
246
246
const deserialized_state = model . get_state ( true ) ;
247
- const serializers = ( model . constructor as any ) . serializers ;
248
247
const change : { [ key : string ] : any } = { } ;
249
248
for ( let i = 0 ; i < buffer_paths . length ; i ++ ) {
250
- // TODO/concern: what if buffer_paths is deeper (length > 1)?
251
- // Will that break something? We do set things properly later.
252
249
const key = buffer_paths [ i ] [ 0 ] ;
253
- const buffer = buffers [ i ] ;
254
- if ( deserialized_state [ key ] == null || buffer == null ) {
255
- change [ key ] = null ;
256
- continue ;
257
- }
258
- let s ;
259
- const f = serializers [ key ] ;
260
- if ( f != null ) {
261
- s = f . serialize ( deserialized_state [ key ] ) ;
262
- s . value = { buffer } ;
263
- s = f . deserialize ( s ) ;
264
- } else {
265
- s = { ...deserialized_state [ key ] , value : buffer } ;
266
- }
267
- change [ key ] = s ;
250
+ setInObject ( deserialized_state [ key ] , buffer_paths [ i ] , buffers [ i ] ) ;
251
+ change [ key ] = deserialized_state [ key ] ;
268
252
}
269
253
log ( "handleBuffersChange: " , model_id , change ) ;
270
- window . x = { model, model_id, change } ;
271
- model . set_state ( change ) ;
254
+ if ( len ( change ) > 0 ) {
255
+ model . set_state ( change ) ;
256
+ }
272
257
} ;
273
258
274
259
private ipywidgets_state_MessageChange = async ( model_id : string ) => {
@@ -580,15 +565,10 @@ class Environment implements WidgetEnvironment {
580
565
}
581
566
const { buffer_paths, buffers } =
582
567
await this . manager . ipywidgets_state . get_model_buffers ( model_id ) ;
568
+
583
569
for ( let i = 0 ; i < buffer_paths . length ; i ++ ) {
584
- // TODO/concern: what if buffer_paths is deeper (length > 1)?
585
- // Will that break something? We do set things properly later.
586
570
const buffer = buffers [ i ] ;
587
- const key = buffer_paths [ i ] [ 0 ] ;
588
- // TODO -- but what about deeply nested key!?
589
- // I only figured out to use value:{buffer} from looking at
590
- // the bqplot source code.
591
- state [ key ] = { ...state [ key ] , value : { buffer } } ;
571
+ setInObject ( state , buffer_paths [ i ] , buffer ) ;
592
572
}
593
573
594
574
setTimeout ( ( ) => this . manager . watchModel ( model_id ) , 1 ) ;
@@ -646,7 +626,7 @@ WidgetModel.prototype.sync = () => {};
646
626
// to be non-fatal, so it's more flexible wrt to our realtime sync setup.
647
627
export function put_buffers (
648
628
state ,
649
- buffer_paths : ( string | number ) [ ] [ ] ,
629
+ buffer_paths : string [ ] [ ] ,
650
630
buffers : any [ ] ,
651
631
) : void {
652
632
for ( let i = 0 ; i < buffer_paths . length ; i ++ ) {
@@ -658,19 +638,22 @@ export function put_buffers(
658
638
buffer instanceof ArrayBuffer ? buffer : buffer . buffer ,
659
639
) ;
660
640
}
661
- // say we want to set state[x][y][z] = buffer
662
- let obj = state as any ;
663
- // we first get obj = state[x][y]
664
- for ( let j = 0 ; j < buffer_path . length - 1 ; j ++ ) {
665
- if ( obj [ buffer_path [ j ] ] == null ) {
666
- // doesn't exist, so create it. This makes things work in
667
- // possibly more random order, rather than crashing. I hit this,
668
- // e.g., when defining animations for k3d.
669
- obj [ buffer_path [ j ] ] = { } ;
670
- }
671
- obj = obj [ buffer_path [ j ] ] ;
672
- }
673
- // and then set: obj[z] = buffer
674
- obj [ buffer_path [ buffer_path . length - 1 ] ] = buffer ;
641
+ setInObject ( state , buffer_path , buffer ) ;
642
+ }
643
+ }
644
+
645
+ function setInObject ( obj : any , path : string [ ] , value : any ) {
646
+ // say we want to set obj[x][y][z] = value
647
+ // we first get obj = state[x][y]
648
+ for ( let j = 0 ; j < path . length - 1 ; j ++ ) {
649
+ if ( obj [ path [ j ] ] == null ) {
650
+ // doesn't exist, so create it. This makes things work in
651
+ // possibly more random order, rather than crashing. I hit this,
652
+ // e.g., when defining animations for k3d.
653
+ obj [ path [ j ] ] = { } ;
654
+ }
655
+ obj = obj [ path [ j ] ] ;
675
656
}
657
+ // and then set: obj[z] = value
658
+ obj [ path [ path . length - 1 ] ] = value ;
676
659
}
0 commit comments