@@ -460,4 +460,48 @@ describe("Instance sharing", () => {
460
460
461
461
} ) ;
462
462
463
+ xit ( "replacing collection of items while keeping a reference to an item" , ( ) => {
464
+ class Song extends Schema {
465
+ @type ( "string" ) url : string ;
466
+ }
467
+
468
+ class Player extends Schema {
469
+ @type ( [ Song ] ) queue = new ArraySchema < Song > ( ) ;
470
+ }
471
+
472
+ class State extends Schema {
473
+ @type ( Song ) playing : Song = new Song ( ) ;
474
+ @type ( [ Song ] ) queue = new ArraySchema < Song > ( ) ;
475
+ @type ( { map : Player } ) buckets = new MapSchema < Player > ( ) ;
476
+ }
477
+
478
+ const sessionId = "" ;
479
+
480
+ const state = new State ( ) ;
481
+ const decodedState = new State ( ) ;
482
+
483
+ console . log ( ">> encode()" )
484
+ decodedState . decode ( state . encode ( ) ) ;
485
+
486
+ state . buckets . set ( sessionId , new Player ( ) ) ;
487
+
488
+ console . log ( ">> encode()" )
489
+ decodedState . decode ( state . encode ( ) ) ;
490
+ console . log ( Schema . debugRefIds ( state ) ) ;
491
+
492
+ const newSong = new Song ( ) . assign ( { url : "song2" } ) ;
493
+ state . buckets . get ( sessionId ) . queue . push ( newSong ) ;
494
+
495
+ state . queue = new ArraySchema < Song > ( ) ;
496
+ state . queue . push ( newSong ) ;
497
+
498
+ state . playing = state . buckets . get ( sessionId ) . queue . shift ( ) ;
499
+ state . queue = new ArraySchema < Song > ( ) ;
500
+
501
+ console . log ( ">> encode()" )
502
+ console . log ( Schema . debugRefIds ( state ) ) ;
503
+ decodedState . decode ( state . encode ( ) ) ;
504
+ assert . deepStrictEqual ( state . toJSON ( ) , decodedState . toJSON ( ) ) ;
505
+ } ) ;
506
+
463
507
} ) ;
0 commit comments