@@ -280,13 +280,16 @@ describe("StateView", () => {
280
280
client1 . view . add ( state . item , Tag . TWO ) ;
281
281
const encodedTag2 = encodeMultiple ( encoder , state , [ client1 ] ) [ 0 ] ;
282
282
283
+ // TODO: can we reduce the amount of bytes here?
284
+ // assert.strictEqual(4, Array.from(encodedTag1).length, "should encode only the new field");
285
+
283
286
// compare encode1 with encode2
284
- assert . strictEqual ( 4 , Array . from ( encodedTag1 ) . length , "should encode only the new field" ) ;
287
+ assert . strictEqual ( 8 , Array . from ( encodedTag1 ) . length , "should encode only the new field" ) ;
285
288
assert . strictEqual ( Array . from ( encodedTag1 ) . length , Array . from ( encodedTag2 ) . length , "encode size should be the same" ) ;
286
289
assert . strictEqual ( Array . from ( encodedTag1 ) [ 0 ] , Array . from ( encodedTag2 ) [ 0 ] ) ;
287
290
assert . strictEqual ( Array . from ( encodedTag1 ) [ 1 ] , Array . from ( encodedTag2 ) [ 1 ] ) ;
288
- assert . strictEqual ( Array . from ( encodedTag1 ) [ 2 ] + 1 , Array . from ( encodedTag2 ) [ 2 ] ) ; // field index (+1 so 1 -> 2)
289
- assert . strictEqual ( Array . from ( encodedTag1 ) [ 3 ] + 10 , Array . from ( encodedTag2 ) [ 3 ] ) ; // value (+ 10 so 20 -> 30)
291
+ assert . strictEqual ( Array . from ( encodedTag1 ) [ 2 ] , Array . from ( encodedTag2 ) [ 2 ] ) ;
292
+ assert . strictEqual ( Array . from ( encodedTag1 ) [ 3 ] , Array . from ( encodedTag2 ) [ 3 ] ) ;
290
293
291
294
assert . strictEqual ( 10 , client1 . state . item . amount ) ;
292
295
assert . strictEqual ( 20 , client1 . state . item . fov1 ) ;
@@ -432,7 +435,6 @@ describe("StateView", () => {
432
435
433
436
assert . strictEqual ( client2 . state . prop1 , state . prop1 ) ;
434
437
assert . strictEqual ( client2 . state . items , undefined ) ;
435
-
436
438
assertEncodeAllMultiple ( encoder , state , [ client1 ] )
437
439
} ) ;
438
440
@@ -479,7 +481,17 @@ describe("StateView", () => {
479
481
assert . strictEqual ( client2 . state . items . size , 2 ) ;
480
482
assert . strictEqual ( client2 . state . items . get ( "4" ) . amount , state . items . get ( "4" ) . amount ) ;
481
483
assert . strictEqual ( client2 . state . items . get ( "5" ) . amount , state . items . get ( "5" ) . amount ) ;
482
- //
484
+
485
+ assertEncodeAllMultiple ( encoder , state , [ client1 , client2 ] )
486
+
487
+ // removing items...
488
+ state . items . delete ( "2" ) ; // none of the clients have this item
489
+ state . items . delete ( "4" ) ; // shared item between client1 and client2
490
+ state . items . delete ( "6" ) ; // none of the clients have this item
491
+ encodeMultiple ( encoder , state , [ client1 , client2 ] ) ;
492
+
493
+ assert . strictEqual ( undefined , client1 . state . items . get ( "4" ) ) ;
494
+ assert . strictEqual ( undefined , client2 . state . items . get ( "4" ) ) ;
483
495
484
496
assertEncodeAllMultiple ( encoder , state , [ client1 , client2 ] )
485
497
} ) ;
@@ -614,6 +626,39 @@ describe("StateView", () => {
614
626
615
627
assert . strictEqual ( undefined , client2 . state . entities ) ;
616
628
} ) ;
629
+
630
+ it ( "adding to view item that has been removed from state" , ( ) => {
631
+ class Item extends Schema {
632
+ @type ( "number" ) amount : number ;
633
+ }
634
+
635
+ class State extends Schema {
636
+ @view ( ) @type ( { map : Item } ) items = new MapSchema < Item > ( ) ;
637
+ }
638
+
639
+ const state = new State ( ) ;
640
+ const encoder = getEncoder ( state ) ;
641
+
642
+ const client1 = createClientWithView ( state ) ;
643
+ encodeMultiple ( encoder , state , [ client1 ] ) ;
644
+
645
+ for ( let i = 0 ; i < 5 ; i ++ ) {
646
+ state . items . set ( i . toString ( ) , new Item ( ) . assign ( { amount : i } ) ) ;
647
+ }
648
+
649
+ client1 . view . add ( state . items . get ( "3" ) ) ;
650
+ encodeMultiple ( encoder , state , [ client1 ] ) ;
651
+
652
+ assert . strictEqual ( client1 . state . items . size , 1 ) ;
653
+ assert . strictEqual ( client1 . state . items . get ( "3" ) . amount , state . items . get ( "3" ) . amount ) ;
654
+
655
+ client1 . view . add ( state . items . get ( "3" ) ) ;
656
+ state . items . delete ( "3" ) ;
657
+
658
+ encodeMultiple ( encoder , state , [ client1 ] ) ;
659
+ assert . strictEqual ( client1 . state . items . size , 0 ) ;
660
+
661
+ } ) ;
617
662
} ) ;
618
663
619
664
describe ( "ArraySchema" , ( ) => {
@@ -832,6 +877,47 @@ describe("StateView", () => {
832
877
assertEncodeAllMultiple ( encoder , state , [ client1 ] )
833
878
} ) ;
834
879
880
+ it ( "removing and item should remove from their views" , ( ) => {
881
+ class Item extends Schema {
882
+ @type ( "number" ) amount : number ;
883
+ }
884
+
885
+ class State extends Schema {
886
+ @view ( ) @type ( [ Item ] ) items = new ArraySchema < Item > ( ) ;
887
+ }
888
+
889
+ const state = new State ( ) ;
890
+ for ( let i = 0 ; i < 5 ; i ++ ) {
891
+ state . items . push ( new Item ( ) . assign ( { amount : i } ) ) ;
892
+ }
893
+
894
+ const encoder = getEncoder ( state ) ;
895
+
896
+ const client1 = createClientWithView ( state ) ;
897
+ const client2 = createClientWithView ( state ) ;
898
+
899
+ client1 . view . add ( state . items . at ( 2 ) ) ;
900
+ client1 . view . add ( state . items . at ( 3 ) ) ;
901
+
902
+ client2 . view . add ( state . items . at ( 3 ) ) ;
903
+ client2 . view . add ( state . items . at ( 4 ) ) ;
904
+
905
+ encodeMultiple ( encoder , state , [ client1 , client2 ] ) ;
906
+
907
+ assert . deepStrictEqual ( client1 . state . items . map ( i => i . amount ) , [ 2 , 3 ] ) ;
908
+ assert . deepStrictEqual ( client2 . state . items . map ( i => i . amount ) , [ 3 , 4 ] ) ;
909
+
910
+ state . items . splice ( 3 , 1 )
911
+ assert . deepStrictEqual ( state . items . map ( i => i . amount ) , [ 0 , 1 , 2 , 4 ] ) ;
912
+
913
+ encodeMultiple ( encoder , state , [ client1 , client2 ] ) ;
914
+
915
+ assert . deepStrictEqual ( client1 . state . items . map ( i => i . amount ) , [ 2 ] ) ;
916
+ assert . deepStrictEqual ( client2 . state . items . map ( i => i . amount ) , [ 4 ] ) ;
917
+
918
+ assertEncodeAllMultiple ( encoder , state , [ client1 , client2 ] )
919
+ } ) ;
920
+
835
921
it ( "visibility change should trigger onAdd/onRemove on arrays" , ( ) => {
836
922
class Item extends Schema {
837
923
@type ( "number" ) amount : number ;
@@ -925,74 +1011,6 @@ describe("StateView", () => {
925
1011
encodeAllMultiple ( encoder , state , [ client1 ] ) ) ;
926
1012
} ) ;
927
1013
928
- xit ( "nested with MapSchema" , ( ) => {
929
- // TODO: couldn't reproduce original issue reported by @Indalamar
930
- class Component extends Schema {
931
- @type ( 'string' ) name : string ;
932
- }
933
- class Spell extends Schema {
934
- @type ( 'number' ) id : number ;
935
- @type ( 'string' ) name : string ;
936
- @type ( [ 'number' ] ) relation = new ArraySchema < number > ( ) ;
937
- constructor ( ) {
938
- super ( ) ;
939
- this . relation . push ( 1 , 2 , 3 ) ;
940
- }
941
- }
942
- class SpellBook extends Component {
943
- @type ( { map : Spell } ) spells : Map < string , Spell > ;
944
- constructor ( ) {
945
- super ( ) ;
946
- this . name = "SpellBook" ;
947
- this . spells = new MapSchema < Spell > ( ) ;
948
- this . spells . set ( "one" , new Spell ( ) . assign ( { id : 1 , name : "Fireball" , } ) ) ;
949
- this . spells . set ( "two" , new Spell ( ) . assign ( { id : 2 , name : "Iceball" , } ) ) ;
950
- this . spells . set ( "three" , new Spell ( ) . assign ( { id : 3 , name : "Thunderball" , } ) ) ;
951
- }
952
- }
953
- class Entity extends Schema {
954
- @type ( "string" ) id : string = nanoid ( 9 ) ;
955
- @type ( [ Component ] ) components : Component [ ] ;
956
- constructor ( ) {
957
- super ( ) ;
958
- this . components = new ArraySchema < Component > ( )
959
- this . components . push ( new SpellBook ( ) ) ;
960
- }
961
- }
962
- class State extends Schema {
963
- @view ( ) @type ( { map : Entity } ) entities = new MapSchema < Entity > ( ) ;
964
- }
965
-
966
- const state = new State ( ) ;
967
- const encoder = getEncoder ( state ) ;
968
-
969
- const client1 = createClientWithView ( state ) ;
970
- const client2 = createClientWithView ( state ) ;
971
-
972
- encodeMultiple ( encoder , state , [ client1 , client2 ] ) ;
973
-
974
- const entity1 = new Entity ( ) ;
975
- entity1 . components . push ( new Component ( ) . assign ( { name : "health" } ) ) ;
976
- state . entities . set ( "one" , entity1 ) ;
977
-
978
- client1 . view . add ( entity1 ) ;
979
- encodeMultiple ( encoder , state , [ client1 , client2 ] ) ;
980
-
981
- const entity2 = new Entity ( ) ;
982
- entity2 . components . push ( new Component ( ) . assign ( { name : "health" } ) ) ;
983
- state . entities . set ( "one" , entity2 ) ;
984
-
985
- client2 . view . add ( entity2 ) ;
986
- client2 . view . add ( entity1 ) ;
987
-
988
- encodeMultiple ( encoder , state , [ client1 , client2 ] ) ;
989
-
990
- console . log ( client1 . state . toJSON ( ) ) ;
991
- console . log ( client2 . state . toJSON ( ) ) ;
992
-
993
- assertEncodeAllMultiple ( encoder , state , [ client1 , client2 ] )
994
- } )
995
-
996
1014
it ( "setting a non-view field to undefined should not interfere on encoding" , ( ) => {
997
1015
class Song extends Schema {
998
1016
@type ( "string" ) url : string ;
0 commit comments