@@ -228,6 +228,71 @@ describe("ArraySchema Tests", () => {
228
228
} ) ;
229
229
} ) ;
230
230
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
+
231
296
it ( "should not crash when pushing an undefined value" , ( ) => {
232
297
class Block extends Schema {
233
298
@type ( "number" ) num : number ;
@@ -2009,23 +2074,6 @@ describe("ArraySchema Tests", () => {
2009
2074
@type ( [ Card ] ) cards = new ArraySchema < Card > ( ) ;
2010
2075
}
2011
2076
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
-
2029
2077
it ( "should push and move entries" , ( ) => {
2030
2078
const state = new MyState ( ) ;
2031
2079
state . cards . push ( new Card ( ) . assign ( { id : 2 } ) ) ;
@@ -2038,11 +2086,13 @@ describe("ArraySchema Tests", () => {
2038
2086
2039
2087
state . cards . push ( new Card ( ) . assign ( { id : 6 } ) ) ;
2040
2088
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
+ } ) ;
2046
2096
2047
2097
decodedState . decode ( state . encode ( ) ) ;
2048
2098
@@ -2076,7 +2126,7 @@ describe("ArraySchema Tests", () => {
2076
2126
decodedState . decode ( state . encode ( ) ) ;
2077
2127
2078
2128
state . cards . pop ( ) ;
2079
- shuffle ( state . cards ) ;
2129
+ state . cards . shuffle ( ) ;
2080
2130
2081
2131
decodedState . decode ( state . encode ( ) ) ;
2082
2132
assert . deepStrictEqual (
@@ -2122,9 +2172,9 @@ describe("ArraySchema Tests", () => {
2122
2172
} ) ;
2123
2173
2124
2174
// 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
+ } ) ;
2128
2178
2129
2179
console . log ( Schema . debugChangesDeep ( state ) ) ;
2130
2180
@@ -2171,10 +2221,7 @@ describe("ArraySchema Tests", () => {
2171
2221
decodedState . decode ( state . encode ( ) ) ;
2172
2222
2173
2223
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 ( ) ;
2178
2225
2179
2226
decodedState . decode ( state . encode ( ) ) ;
2180
2227
0 commit comments