@@ -229,10 +229,9 @@ interface ComposerDraftSync {
229229 /** Per entry: this tab's partialized value and storage's value at its last sync. */
230230 readonly local : ComposerDraftEntries ;
231231 readonly stored : ComposerDraftEntries ;
232- /** The storage text last written or read, its entries, and whether memory adopted them . */
232+ /** The storage text last written or read and its entries . */
233233 readonly text ?: string | null ;
234234 readonly textEntries ?: ComposerDraftEntries | null ;
235- readonly adopted ?: boolean ;
236235}
237236const UNSYNCED : ComposerDraftSync = { local : new Map ( ) , stored : new Map ( ) } ;
238237let sync = UNSYNCED ;
@@ -243,9 +242,18 @@ function writeComposerDrafts(name: string, value: StorageValue<ComposerPersistSt
243242 composerDraftLocalStorage . setItem ( name , JSON . stringify ( value ) ) ;
244243 return ;
245244 }
246- const partialized = partializeComposerDraftStoreState ( value . state . capturedState ) ;
247- const local = entriesOf ( partialized ) ;
248- const changed = changedEntryIds ( local , sync . local ) ;
245+ let partialized = partializeComposerDraftStoreState ( value . state . capturedState ) ;
246+ let local = entriesOf ( partialized ) ;
247+ let changed = changedEntryIds ( local , sync . local ) ;
248+ const deferred = changedEntryIds ( sync . textEntries ?? sync . stored , sync . local ) . filter ( ( id ) =>
249+ jsonEquals ( local . get ( id ) , sync . local . get ( id ) ) ,
250+ ) ;
251+ if ( deferred . length > 0 ) {
252+ adoptStoredComposerDrafts ( ) ;
253+ partialized = partializeComposerDraftStoreState ( composerDraftStore . getState ( ) ) ;
254+ local = entriesOf ( partialized ) ;
255+ changed = changedEntryIds ( local , sync . local ) ;
256+ }
249257 if ( changed . length === 0 ) return ;
250258 const text = readComposerDraftText ( ) ;
251259 const parsed = sync . text === text ? undefined : readStoredComposerDrafts ( text ) ;
@@ -262,7 +270,6 @@ function writeComposerDrafts(name: string, value: StorageValue<ComposerPersistSt
262270 stored : stored ? withEntries ( sync . stored , changed , local ) : local ,
263271 text : nextText ,
264272 textEntries : merged ,
265- adopted : false ,
266273 } ;
267274}
268275
@@ -279,7 +286,7 @@ const composerPersistStorage: PersistStorage<ComposerPersistState> = {
279286 const stored = storedStateOf ( value ) ;
280287 const seen = stored && entriesOf ( stored ) ;
281288 // onRehydrateStorage sets `local` once the state is hydrated.
282- sync = { ...UNSYNCED , stored : seen ?? new Map ( ) , text, textEntries : seen , adopted : true } ;
289+ sync = { ...UNSYNCED , stored : seen ?? new Map ( ) , text, textEntries : seen } ;
283290 return value ;
284291 } ,
285292 setItem : ( name , value ) => composerDebouncedStorage . setItem ( name , value ) ,
@@ -351,18 +358,21 @@ function adoptStoredDraft(
351358/** Takes in every entry another tab changed in storage that this tab has not changed itself. */
352359function adoptStoredComposerDrafts ( ) : void {
353360 const text = readComposerDraftText ( ) ;
354- if ( sync . adopted && sync . text === text ) return ;
355361 const stored = readStoredComposerDrafts ( text ) ;
356362 const entries = stored && entriesOf ( stored ) ;
357363 const fromStorage = entries ? changedEntryIds ( entries , sync . stored ) : [ ] ;
358364 // Shares unchanged entries with the baseline instead of keeping a second parsed copy.
359365 const seen = entries && withEntries ( sync . stored , fromStorage , entries ) ;
360- sync = { ...sync , text, textEntries : seen , adopted : true } ;
361- if ( ! stored || ! seen || fromStorage . length === 0 ) return ;
366+ sync = { ...sync , text, textEntries : seen } ;
362367 const state = composerDraftStore . getState ( ) ;
363368 const local = entriesOf ( partializeComposerDraftStoreState ( state ) ) ;
364- const ids = new Set ( fromStorage . filter ( ( id ) => jsonEquals ( local . get ( id ) , sync . local . get ( id ) ) ) ) ;
365- if ( ids . size === 0 ) return ;
369+ const deferred = changedEntryIds ( seen ?? sync . stored , sync . local ) ;
370+ const ids = new Set (
371+ [ ...new Set ( [ ...fromStorage , ...deferred ] ) ] . filter ( ( id ) =>
372+ jsonEquals ( local . get ( id ) , sync . local . get ( id ) ) ,
373+ ) ,
374+ ) ;
375+ if ( ! stored || ! seen || ids . size === 0 ) return ;
366376 const keep = < T > ( stored : T | undefined ) => stored ;
367377 composerDraftStore . setState ( {
368378 draftThreadsByThreadKey : adoptStoredRecord (
0 commit comments