@@ -85,7 +85,6 @@ export class TsunamiModel {
8585 clientId : string ;
8686 serverId : string ;
8787 viewRef : React . RefObject < HTMLDivElement > = { current : null } ;
88- remountCallback : ( ( ) => void ) | null = null ;
8988 vdomRoot : jotai . PrimitiveAtom < VDomElem > = jotai . atom ( ) ;
9089 atoms : Map < string , AtomContainer > = new Map ( ) ; // key is atomname
9190 refs : Map < string , RefContainer > = new Map ( ) ; // key is refid
@@ -486,11 +485,35 @@ export class TsunamiModel {
486485 }
487486
488487 handleStateSync ( update : VDomBackendUpdate , idMap : Map < string , VDomElem > ) {
489- if ( update . statesync == null ) {
490- return ;
491- }
492- for ( let sync of update . statesync ) {
493- this . setAtomValue ( sync . atom , sync . value , true , idMap ) ;
488+ if ( update . fullupdate ) {
489+ if ( update . statesync == null ) {
490+ this . atoms . clear ( ) ;
491+ return ;
492+ }
493+
494+ const sentAtoms = new Set < string > ( ) ;
495+ for ( let sync of update . statesync ) {
496+ sentAtoms . add ( sync . atom ) ;
497+ this . setAtomValue ( sync . atom , sync . value , true , idMap ) ;
498+ }
499+
500+ const atomsToRemove : string [ ] = [ ] ;
501+ for ( let atomName of this . atoms . keys ( ) ) {
502+ if ( ! sentAtoms . has ( atomName ) ) {
503+ atomsToRemove . push ( atomName ) ;
504+ }
505+ }
506+
507+ for ( let atomName of atomsToRemove ) {
508+ this . atoms . delete ( atomName ) ;
509+ }
510+ } else {
511+ if ( update . statesync == null ) {
512+ return ;
513+ }
514+ for ( let sync of update . statesync ) {
515+ this . setAtomValue ( sync . atom , sync . value , true , idMap ) ;
516+ }
494517 }
495518 }
496519
@@ -562,13 +585,11 @@ export class TsunamiModel {
562585 return ;
563586 }
564587
565- // Check if serverId is changing and trigger remount if needed
588+ // Check if serverId is changing and reset if needed
566589 if ( this . serverId != null && this . serverId !== update . serverid ) {
567- // Server ID changed - need to remount the entire app
568- if ( this . remountCallback ) {
569- this . remountCallback ( ) ;
570- }
571- return ;
590+ // Server ID changed - reset the model state
591+ this . reset ( ) ;
592+ this . setupServerEventSource ( ) ;
572593 }
573594
574595 this . serverId = update . serverid ;
0 commit comments