@@ -238,6 +238,62 @@ describe("Callbacks (new API)", () => {
238238 assert . deepStrictEqual ( addedIndexes , [ 0 , 1 ] ) ;
239239 assert . deepStrictEqual ( addedAmounts , [ 10 , 20 ] ) ;
240240 } ) ;
241+
242+ it ( "should not register onAdd callback twice when collection becomes available" , ( ) => {
243+ class Player extends Schema {
244+ @type ( "string" ) name : string ;
245+ }
246+
247+ class State extends Schema {
248+ @type ( { map : Player } ) players : MapSchema < Player > ;
249+ }
250+
251+ const state = new State ( ) ;
252+ // Initially no players collection (undefined)
253+
254+ const decodedState = createInstanceFromReflection ( state ) ;
255+ const decoder = getDecoder ( decodedState ) ;
256+ const callbacks = Callbacks . get ( decoder ) ;
257+
258+ let addCount = 0 ;
259+ const addedNames : string [ ] = [ ] ;
260+
261+ // Register onAdd while collection is NOT available
262+ callbacks . onAdd (
263+ "players" ,
264+ ( player , sessionId ) => {
265+ addCount ++ ;
266+ addedNames . push ( player . name ) ;
267+ }
268+ ) ;
269+
270+ // First decode - no collection yet
271+ decodedState . decode ( state . encode ( ) ) ;
272+ assert . strictEqual ( addCount , 0 ) ;
273+
274+ // Now set the collection with a player
275+ state . players = new MapSchema < Player > ( ) ;
276+ state . players . set ( "one" , new Player ( ) . assign ( { name : "Alice" } ) ) ;
277+ decodedState . decode ( state . encodeAll ( ) ) ;
278+ decodedState . decode ( state . encode ( ) ) ;
279+
280+ // Should be called exactly once for Alice
281+ assert . strictEqual ( addCount , 1 , `Expected addCount to be 1, but was ${ addCount } . Names: ${ addedNames . join ( ", " ) } ` ) ;
282+
283+ // Add another player to the same collection
284+ state . players . set ( "two" , new Player ( ) . assign ( { name : "Bob" } ) ) ;
285+ decodedState . decode ( state . encode ( ) ) ;
286+
287+ // Should be called once for Bob (total 2)
288+ assert . strictEqual ( addCount , 2 , `Expected addCount to be 2, but was ${ addCount } . Names: ${ addedNames . join ( ", " ) } ` ) ;
289+
290+ // Add another player to the same collection
291+ state . players . set ( "three" , new Player ( ) . assign ( { name : "Charlie" } ) ) ;
292+ decodedState . decode ( state . encode ( ) ) ;
293+
294+ // Should be called once for Charlie (total 3)
295+ assert . strictEqual ( addCount , 3 , `Expected addCount to be 3, but was ${ addCount } . Names: ${ addedNames . join ( ", " ) } ` ) ;
296+ } ) ;
241297 } ) ;
242298
243299 describe ( "onChange" , ( ) => {
0 commit comments