@@ -228,6 +228,71 @@ describe("ArraySchema Tests", () => {
228228 } ) ;
229229 } ) ;
230230
231+ it ( "should allow mutating primitive value by index" , ( ) => {
232+ class State extends Schema {
233+ @type ( [ 'number' ] ) primativeArr = new ArraySchema < Number > ( )
234+ }
235+
236+ const state = new State ( ) ;
237+ const decodedState = createInstanceFromReflection ( state ) ;
238+
239+ state . primativeArr . push ( 1 ) ;
240+ state . primativeArr . push ( 2 ) ;
241+ state . primativeArr . push ( 3 ) ;
242+
243+ decodedState . decode ( state . encode ( ) ) ;
244+ assert . strictEqual ( 3 , decodedState . primativeArr . length ) ;
245+
246+ state . primativeArr [ 0 ] = - 1
247+ decodedState . decode ( state . encode ( ) ) ;
248+ assert . strictEqual ( 3 , decodedState . primativeArr . length ) ;
249+
250+ state . primativeArr [ 1 ] = - 2
251+ decodedState . decode ( state . encode ( ) ) ;
252+ assert . strictEqual ( 3 , decodedState . primativeArr . length ) ;
253+
254+ state . primativeArr [ 2 ] = - 3
255+ decodedState . decode ( state . encode ( ) ) ;
256+ assert . strictEqual ( 3 , decodedState . primativeArr . length ) ;
257+
258+ assert . deepStrictEqual ( state . toJSON ( ) , decodedState . toJSON ( ) ) ;
259+ assertRefIdCounts ( state , decodedState ) ;
260+ } ) ;
261+
262+ it ( "should allow mutating Schema value by index" , ( ) => {
263+ class Item extends Schema {
264+ @type ( 'number' ) i : number ;
265+ }
266+ class State extends Schema {
267+ @type ( [ Item ] ) schemaArr = new ArraySchema < Item > ( )
268+ }
269+
270+ const state = new State ( ) ;
271+ const decodedState = createInstanceFromReflection ( state ) ;
272+
273+ state . schemaArr . push ( new Item ( ) . assign ( { i : 1 } ) ) ;
274+ state . schemaArr . push ( new Item ( ) . assign ( { i : 2 } ) ) ;
275+ state . schemaArr . push ( new Item ( ) . assign ( { i : 3 } ) ) ;
276+
277+ decodedState . decode ( state . encode ( ) ) ;
278+ assert . strictEqual ( 3 , decodedState . schemaArr . length ) ;
279+
280+ state . schemaArr [ 0 ] = new Item ( { i : - 1 } ) ;
281+ decodedState . decode ( state . encode ( ) ) ;
282+ assert . strictEqual ( 3 , decodedState . schemaArr . length ) ;
283+
284+ state . schemaArr [ 1 ] = new Item ( { i : - 2 } )
285+ decodedState . decode ( state . encode ( ) ) ;
286+ assert . strictEqual ( 3 , decodedState . schemaArr . length ) ;
287+
288+ state . schemaArr [ 2 ] = new Item ( { i : - 3 } )
289+ decodedState . decode ( state . encode ( ) ) ;
290+ assert . strictEqual ( 3 , decodedState . schemaArr . length ) ;
291+
292+ assert . deepStrictEqual ( state . toJSON ( ) , decodedState . toJSON ( ) ) ;
293+ assertRefIdCounts ( state , decodedState ) ;
294+ } ) ;
295+
231296 it ( "should not crash when pushing an undefined value" , ( ) => {
232297 class Block extends Schema {
233298 @type ( "number" ) num : number ;
@@ -2009,23 +2074,6 @@ describe("ArraySchema Tests", () => {
20092074 @type ( [ Card ] ) cards = new ArraySchema < Card > ( ) ;
20102075 }
20112076
2012- function shuffle ( array ) {
2013- let currentIndex = array . length ;
2014-
2015- // While there remain elements to shuffle...
2016- while ( currentIndex != 0 ) {
2017-
2018- // Pick a remaining element...
2019- let randomIndex = Math . floor ( Math . random ( ) * currentIndex ) ;
2020- currentIndex -- ;
2021-
2022- // console.log(`[array[${currentIndex}], array[${randomIndex}]] = [array[${randomIndex}], array[${currentIndex}]];`);
2023-
2024- // And swap it with the current element.
2025- [ array [ currentIndex ] , array [ randomIndex ] ] = [ array [ randomIndex ] , array [ currentIndex ] ] ;
2026- }
2027- }
2028-
20292077 it ( "should push and move entries" , ( ) => {
20302078 const state = new MyState ( ) ;
20312079 state . cards . push ( new Card ( ) . assign ( { id : 2 } ) ) ;
@@ -2038,11 +2086,13 @@ describe("ArraySchema Tests", () => {
20382086
20392087 state . cards . push ( new Card ( ) . assign ( { id : 6 } ) ) ;
20402088
2041- [ state . cards [ 4 ] , state . cards [ 3 ] ] = [ state . cards [ 3 ] , state . cards [ 4 ] ] ;
2042- [ state . cards [ 3 ] , state . cards [ 2 ] ] = [ state . cards [ 2 ] , state . cards [ 3 ] ] ;
2043- [ state . cards [ 2 ] , state . cards [ 0 ] ] = [ state . cards [ 0 ] , state . cards [ 2 ] ] ;
2044- [ state . cards [ 1 ] , state . cards [ 1 ] ] = [ state . cards [ 1 ] , state . cards [ 1 ] ] ;
2045- [ state . cards [ 0 ] , state . cards [ 0 ] ] = [ state . cards [ 0 ] , state . cards [ 0 ] ] ;
2089+ state . cards . move ( ( ) => {
2090+ [ state . cards [ 4 ] , state . cards [ 3 ] ] = [ state . cards [ 3 ] , state . cards [ 4 ] ] ;
2091+ [ state . cards [ 3 ] , state . cards [ 2 ] ] = [ state . cards [ 2 ] , state . cards [ 3 ] ] ;
2092+ [ state . cards [ 2 ] , state . cards [ 0 ] ] = [ state . cards [ 0 ] , state . cards [ 2 ] ] ;
2093+ [ state . cards [ 1 ] , state . cards [ 1 ] ] = [ state . cards [ 1 ] , state . cards [ 1 ] ] ;
2094+ [ state . cards [ 0 ] , state . cards [ 0 ] ] = [ state . cards [ 0 ] , state . cards [ 0 ] ] ;
2095+ } ) ;
20462096
20472097 decodedState . decode ( state . encode ( ) ) ;
20482098
@@ -2076,7 +2126,7 @@ describe("ArraySchema Tests", () => {
20762126 decodedState . decode ( state . encode ( ) ) ;
20772127
20782128 state . cards . pop ( ) ;
2079- shuffle ( state . cards ) ;
2129+ state . cards . shuffle ( ) ;
20802130
20812131 decodedState . decode ( state . encode ( ) ) ;
20822132 assert . deepStrictEqual (
@@ -2122,9 +2172,9 @@ describe("ArraySchema Tests", () => {
21222172 } ) ;
21232173
21242174 // swap refId 5 <=> 2
2125- console . log ( "WILL SWAP!" ) ;
2126- [ state . cards [ 2 ] , state . cards [ 0 ] ] = [ state . cards [ 0 ] , state . cards [ 2 ] ] ;
2127- console . log ( "SWAPPED." ) ;
2175+ state . cards . move ( ( ) => {
2176+ [ state . cards [ 2 ] , state . cards [ 0 ] ] = [ state . cards [ 0 ] , state . cards [ 2 ] ] ;
2177+ } ) ;
21282178
21292179 console . log ( Schema . debugChangesDeep ( state ) ) ;
21302180
@@ -2171,10 +2221,7 @@ describe("ArraySchema Tests", () => {
21712221 decodedState . decode ( state . encode ( ) ) ;
21722222
21732223 state . cards . splice ( 2 , 1 ) ;
2174-
2175- [ state . cards [ 2 ] , state . cards [ 0 ] ] = [ state . cards [ 0 ] , state . cards [ 2 ] ] ;
2176- [ state . cards [ 1 ] , state . cards [ 0 ] ] = [ state . cards [ 0 ] , state . cards [ 1 ] ] ;
2177- [ state . cards [ 0 ] , state . cards [ 0 ] ] = [ state . cards [ 0 ] , state . cards [ 0 ] ] ;
2224+ state . cards . shuffle ( ) ;
21782225
21792226 decodedState . decode ( state . encode ( ) ) ;
21802227
0 commit comments