@@ -321,6 +321,7 @@ const requestedDeviceLocalPrefResets = new Set<SyncedPrefKey>();
321321let applyingServerPrefs = false ;
322322let pendingScope = "" ;
323323let pendingPrefs : ServerUiPrefs | null = null ;
324+ let pendingPersistedKeys = new Set < SyncedPrefKey > ( ) ;
324325let pushWriter : ServerUiPrefsWriter | null = null ;
325326let pushScope = "" ;
326327let pushAfterCommit : ( ( commit : ServerUiPrefsCommit ) => void ) | undefined ;
@@ -341,23 +342,39 @@ function clearConflictRedrain(): void {
341342 }
342343 consecutiveConflictRedrains = 0 ;
343344}
344- function readStorage ( root : string , scope : string ) : string | null {
345+ function readStorageState (
346+ root : string ,
347+ scope : string ,
348+ ) : { available : boolean ; value : string | null } {
345349 try {
346- return globalThis . localStorage ?. getItem ( `${ root } :${ scope } ` ) ?? null ;
350+ const storage = globalThis . localStorage ;
351+ if ( ! storage ) {
352+ return { available : false , value : null } ;
353+ }
354+ return { available : true , value : storage . getItem ( `${ root } :${ scope } ` ) } ;
347355 } catch {
348- return null ;
356+ return { available : false , value : null } ;
349357 }
350358}
351- function writeStorage ( root : string , scope : string , value : string | null ) : void {
359+ function readStorage ( root : string , scope : string ) : string | null {
360+ return readStorageState ( root , scope ) . value ;
361+ }
362+ function writeStorage ( root : string , scope : string , value : string | null ) : boolean {
352363 try {
364+ const storage = globalThis . localStorage ;
365+ if ( ! storage ) {
366+ return false ;
367+ }
353368 const key = `${ root } :${ scope } ` ;
354369 if ( value === null ) {
355- globalThis . localStorage ? .removeItem ( key ) ;
370+ storage . removeItem ( key ) ;
356371 } else {
357- globalThis . localStorage ? .setItem ( key , value ) ;
372+ storage . setItem ( key , value ) ;
358373 }
374+ return true ;
359375 } catch {
360376 // Quota/security failures degrade to in-memory tracking for this session.
377+ return false ;
361378 }
362379}
363380function parseStoredPrefs ( raw : string | null ) : ServerUiPrefs | null {
@@ -368,6 +385,16 @@ function parseStoredPrefs(raw: string | null): ServerUiPrefs | null {
368385 return null ;
369386 }
370387}
388+ function readStoredPrefs (
389+ root : string ,
390+ scope : string ,
391+ ) : { available : boolean ; prefs : ServerUiPrefs | null } {
392+ const stored = readStorageState ( root , scope ) ;
393+ return {
394+ available : stored . available ,
395+ prefs : parseStoredPrefs ( stored . value ) ,
396+ } ;
397+ }
371398function readRetainedLocalKeys ( scope : string ) : Set < SyncedPrefKey > {
372399 const stored = parseStoredPrefs ( readStorage ( RETAINED_LOCAL_KEY , scope ) ) ;
373400 return new Set (
@@ -406,12 +433,26 @@ function adoptPendingScope(scope: string, force = false): void {
406433 return ;
407434 }
408435 pendingScope = scope ;
409- pendingPrefs = parseStoredPrefs ( readStorage ( PENDING_KEY , scope ) ) ;
436+ const stored = readStoredPrefs ( PENDING_KEY , scope ) ;
437+ pendingPrefs = stored . prefs ;
438+ pendingPersistedKeys = new Set (
439+ stored . available && stored . prefs ? ( Object . keys ( stored . prefs ) as SyncedPrefKey [ ] ) : [ ] ,
440+ ) ;
410441}
411442function writePendingStorage ( prefs : ServerUiPrefs | null ) : void {
412- writeStorage ( PENDING_KEY , pendingScope , prefs ? JSON . stringify ( prefs ) : null ) ;
443+ const persisted = writeStorage ( PENDING_KEY , pendingScope , prefs ? JSON . stringify ( prefs ) : null ) ;
444+ if ( persisted ) {
445+ pendingPersistedKeys = new Set (
446+ pendingPrefs ? ( Object . keys ( pendingPrefs ) as SyncedPrefKey [ ] ) : [ ] ,
447+ ) ;
448+ } else {
449+ pendingPersistedKeys . clear ( ) ;
450+ }
413451}
414452function cancelPendingKeys ( scope : string , keys : readonly SyncedPrefKey [ ] ) : void {
453+ if ( scope === pendingScope ) {
454+ reconcilePersistedPendingPrefs ( ) ;
455+ }
415456 const active = scope === pendingScope ? pendingPrefs : null ;
416457 const remaining = {
417458 ...parseStoredPrefs ( readStorage ( PENDING_KEY , scope ) ) ,
@@ -423,6 +464,8 @@ function cancelPendingKeys(scope: string, keys: readonly SyncedPrefKey[]): void
423464 const next = Object . keys ( remaining ) . length ? remaining : null ;
424465 if ( scope === pendingScope ) {
425466 pendingPrefs = next ;
467+ writePendingStorage ( next ) ;
468+ return ;
426469 }
427470 writeStorage ( PENDING_KEY , scope , next ? JSON . stringify ( next ) : null ) ;
428471}
@@ -444,11 +487,46 @@ function settlePendingStorage(ackedBatch: ServerUiPrefs): void {
444487 const merged = { ...stored , ...pendingPrefs } ;
445488 writePendingStorage ( Object . keys ( merged ) . length ? merged : null ) ;
446489}
490+ // Only persisted keys participate in cross-tab reconciliation. An in-memory-only key means
491+ // localStorage was unavailable, so absence from storage cannot be interpreted as cancellation.
492+ function reconcilePersistedPendingPrefs ( ) : void {
493+ if ( ! pendingPrefs || pendingPersistedKeys . size === 0 ) {
494+ return ;
495+ }
496+ const stored = readStoredPrefs ( PENDING_KEY , pendingScope ) ;
497+ if ( ! stored . available ) {
498+ return ;
499+ }
500+ const current = stored . prefs ?? { } ;
501+ for ( const key of [ ...pendingPersistedKeys ] ) {
502+ if ( ! Object . hasOwn ( current , key ) ) {
503+ delete pendingPrefs [ key ] ;
504+ pendingPersistedKeys . delete ( key ) ;
505+ continue ;
506+ }
507+ const storedValue = current [ key ] ;
508+ if ( ! prefValuesEqual ( pendingPrefs [ key ] , storedValue ) ) {
509+ ( pendingPrefs as Record < string , unknown > ) [ key ] = storedValue ;
510+ }
511+ }
512+ if ( ! Object . keys ( pendingPrefs ) . length ) {
513+ pendingPrefs = null ;
514+ }
515+ }
516+ function batchIsCurrent ( batch : ServerUiPrefs ) : boolean {
517+ return Boolean (
518+ pendingPrefs &&
519+ ( Object . keys ( batch ) as SyncedPrefKey [ ] ) . every (
520+ ( key ) => Object . hasOwn ( pendingPrefs , key ) && prefValuesEqual ( pendingPrefs [ key ] , batch [ key ] ) ,
521+ ) ,
522+ ) ;
523+ }
447524export function resetServerUiPrefsSync ( ) {
448525 clearConflictRedrain ( ) ;
449526 applyingServerPrefs = pushDraining = drainRequested = false ;
450527 pendingScope = "" ;
451528 pendingPrefs = pushWriter = null ;
529+ pendingPersistedKeys . clear ( ) ;
452530 pushScope = "" ;
453531 lastReconciledScope = "" ;
454532 lastReconciledConfigObject = null ;
@@ -562,6 +640,9 @@ function adoptPushWriter(writer: ServerUiPrefsWriter): void {
562640 if ( pushWriter === writer && pushScope === scope ) {
563641 return ;
564642 }
643+ // Reconcile the scope being left before moving pre-connection intent forward.
644+ // Otherwise another tab can cancel storage while this realm later resurrects its stale memory.
645+ reconcilePersistedPendingPrefs ( ) ;
565646 const unscopedPending =
566647 pendingScope === ""
567648 ? {
@@ -591,6 +672,7 @@ function removeBatch(batch: ServerUiPrefs): void {
591672 for ( const key of Object . keys ( batch ) as SyncedPrefKey [ ] ) {
592673 if ( prefValuesEqual ( pendingPrefs [ key ] , batch [ key ] ) ) {
593674 delete pendingPrefs [ key ] ;
675+ pendingPersistedKeys . delete ( key ) ;
594676 }
595677 }
596678 if ( ! Object . keys ( pendingPrefs ) . length ) {
@@ -616,6 +698,10 @@ async function drainPendingPrefs(writer: ServerUiPrefsWriter, epoch: number): Pr
616698 if ( pushWriter !== writer || pushEpoch !== epoch ) {
617699 return ;
618700 }
701+ reconcilePersistedPendingPrefs ( ) ;
702+ if ( ! pendingPrefs ) {
703+ return ;
704+ }
619705 const batch = { ...pendingPrefs } ;
620706 const afterCommit = pushAfterCommit ;
621707 for ( let attempt = 0 ; attempt < 2 ; attempt += 1 ) {
@@ -636,7 +722,17 @@ async function drainPendingPrefs(writer: ServerUiPrefsWriter, epoch: number): Pr
636722 } ) ,
637723 {
638724 waitForWritesResumed : true ,
639- canDispatch : ( ) => writer . canPatch !== false ,
725+ canDispatch : ( ) => {
726+ if ( writer . canPatch === false ) {
727+ return false ;
728+ }
729+ reconcilePersistedPendingPrefs ( ) ;
730+ if ( batchIsCurrent ( batch ) ) {
731+ return true ;
732+ }
733+ drainRequested = Boolean ( pendingPrefs ) ;
734+ return false ;
735+ } ,
640736 dispatchError : "Access changed before preferences could sync." ,
641737 } ,
642738 ) ;
@@ -733,6 +829,7 @@ export function pushServerUiPrefs(
733829 hooks . afterCommit ?.( { needsRefresh : false , retainedLocal : true } ) ;
734830 return ;
735831 }
832+ reconcilePersistedPendingPrefs ( ) ;
736833 pendingPrefs = { ...pendingPrefs , ...prefs } ;
737834 mergePendingIntoStorage ( ) ;
738835 startPendingDrain ( writer ) ;
0 commit comments