@@ -47,6 +47,49 @@ function slotIdKey(pubkey: string): string {
4747 return `${ SLOT_ID_KEY_PREFIX } :${ pubkey } ` ;
4848}
4949
50+ export type ApplyRemoteContextResult = "unchanged" | "advanced" ;
51+
52+ function resolveRemoteContextTimestamp ( args : {
53+ current : number ;
54+ timestamp : number ;
55+ } ) : { next : number ; result : ApplyRemoteContextResult } {
56+ const next = Math . max ( args . current , args . timestamp ) ;
57+ return {
58+ next,
59+ result : next === args . current ? "unchanged" : "advanced" ,
60+ } ;
61+ }
62+
63+ export function applyRemoteContextTimestamp ( args : {
64+ effectiveState : Map < string , number > ;
65+ contextSourceCreatedAt : Map < string , number > ;
66+ contextId : string ;
67+ timestamp : number ;
68+ eventCreatedAt : number ;
69+ } ) : ApplyRemoteContextResult {
70+ const {
71+ effectiveState,
72+ contextSourceCreatedAt,
73+ contextId,
74+ timestamp,
75+ eventCreatedAt,
76+ } = args ;
77+ const sourceCreatedAt = contextSourceCreatedAt . get ( contextId ) ?? 0 ;
78+ const current = effectiveState . get ( contextId ) ?? 0 ;
79+ const { next, result } = resolveRemoteContextTimestamp ( {
80+ current,
81+ timestamp,
82+ } ) ;
83+
84+ if ( result === "advanced" ) {
85+ effectiveState . set ( contextId , next ) ;
86+ }
87+ if ( eventCreatedAt > sourceCreatedAt ) {
88+ contextSourceCreatedAt . set ( contextId , eventCreatedAt ) ;
89+ }
90+ return result ;
91+ }
92+
5093export class ReadStateManager {
5194 private pubkey : string ;
5295 private relayClient : RelayClient ;
@@ -60,9 +103,7 @@ export class ReadStateManager {
60103 private unsubscribeLive : ( ( ) => void ) | null = null ;
61104 private initialized = false ;
62105 private maxFetchedCreatedAt = 0 ;
63- private forcedContexts = new Set < string > ( ) ;
64106 private contextSourceCreatedAt = new Map < string , number > ( ) ;
65- private pendingSyncedRollbacks = new Set < string > ( ) ;
66107 private pendingSyncedAdvances = new Set < string > ( ) ;
67108 private destroyed = false ;
68109
@@ -101,7 +142,6 @@ export class ReadStateManager {
101142 }
102143
103144 markContextRead ( contextId : string , unixTimestamp : number ) : void {
104- this . forcedContexts . delete ( contextId ) ;
105145 this . advanceContext ( contextId , unixTimestamp , { publishable : true } ) ;
106146 this . contextSourceCreatedAt . set (
107147 contextId ,
@@ -113,16 +153,6 @@ export class ReadStateManager {
113153 this . advanceContext ( contextId , unixTimestamp , { publishable : false } ) ;
114154 }
115155
116- markContextUnread ( contextId : string , lastMessageUnix : number ) : void {
117- const rollbackTo = lastMessageUnix - 1 ;
118- this . effectiveState . set ( contextId , rollbackTo ) ;
119- this . publishableContextIds . add ( contextId ) ;
120- this . forcedContexts . add ( contextId ) ;
121- this . persistLocalState ( ) ;
122- this . notifyListeners ( ) ;
123- this . schedulePublish ( ) ;
124- }
125-
126156 private advanceContext (
127157 contextId : string ,
128158 unixTimestamp : number ,
@@ -241,16 +271,17 @@ export class ReadStateManager {
241271 }
242272
243273 for ( const [ ctx , ts ] of Object . entries ( blob . contexts ) ) {
244- if ( this . forcedContexts . has ( ctx ) ) continue ;
245- const sourceCreatedAt = this . contextSourceCreatedAt . get ( ctx ) ?? 0 ;
246- const current = this . effectiveState . get ( ctx ) ?? 0 ;
247- if ( event . created_at > sourceCreatedAt ) {
248- this . effectiveState . set ( ctx , ts ) ;
249- this . contextSourceCreatedAt . set ( ctx , event . created_at ) ;
250- } else if ( event . created_at === sourceCreatedAt && ts !== current ) {
251- this . effectiveState . set ( ctx , ts ) ;
274+ const result = applyRemoteContextTimestamp ( {
275+ effectiveState : this . effectiveState ,
276+ contextSourceCreatedAt : this . contextSourceCreatedAt ,
277+ contextId : ctx ,
278+ timestamp : ts ,
279+ eventCreatedAt : event . created_at ,
280+ } ) ;
281+ if ( result !== "unchanged" ) {
282+ this . pendingSyncedAdvances . add ( ctx ) ;
283+ this . publishableContextIds . add ( ctx ) ;
252284 }
253- this . publishableContextIds . add ( ctx ) ;
254285 }
255286
256287 if ( blob . client_id === this . clientId ) {
@@ -361,27 +392,15 @@ export class ReadStateManager {
361392
362393 let anyAdvanced = false ;
363394 for ( const [ ctx , ts ] of Object . entries ( blob . contexts ) ) {
364- if ( this . forcedContexts . has ( ctx ) ) continue ;
365- const sourceCreatedAt = this . contextSourceCreatedAt . get ( ctx ) ?? 0 ;
366- const current = this . effectiveState . get ( ctx ) ?? 0 ;
367- if ( event . created_at > sourceCreatedAt ) {
368- if ( this . effectiveState . get ( ctx ) !== ts ) {
369- if ( ts < current && current > 0 ) {
370- this . pendingSyncedRollbacks . add ( ctx ) ;
371- this . pendingSyncedAdvances . delete ( ctx ) ;
372- console . debug (
373- `[ReadStateManager] synced rollback ctx=${ ctx . substring ( 0 , 12 ) } … from=${ current } to=${ ts } ` ,
374- ) ;
375- } else if ( ts > current ) {
376- this . pendingSyncedAdvances . add ( ctx ) ;
377- this . pendingSyncedRollbacks . delete ( ctx ) ;
378- }
379- this . effectiveState . set ( ctx , ts ) ;
380- anyAdvanced = true ;
381- }
382- this . contextSourceCreatedAt . set ( ctx , event . created_at ) ;
383- } else if ( event . created_at === sourceCreatedAt && ts !== current ) {
384- this . effectiveState . set ( ctx , ts ) ;
395+ const result = applyRemoteContextTimestamp ( {
396+ effectiveState : this . effectiveState ,
397+ contextSourceCreatedAt : this . contextSourceCreatedAt ,
398+ contextId : ctx ,
399+ timestamp : ts ,
400+ eventCreatedAt : event . created_at ,
401+ } ) ;
402+ if ( result === "advanced" ) {
403+ this . pendingSyncedAdvances . add ( ctx ) ;
385404 anyAdvanced = true ;
386405 }
387406 if ( ! this . publishableContextIds . has ( ctx ) ) {
@@ -467,7 +486,6 @@ export class ReadStateManager {
467486 }
468487 }
469488 this . lastPublishedContexts = contexts ;
470- this . forcedContexts . clear ( ) ;
471489 this . maxFetchedCreatedAt = Math . max (
472490 this . maxFetchedCreatedAt ,
473491 event . created_at ,
@@ -544,12 +562,6 @@ export class ReadStateManager {
544562 ) ;
545563 }
546564
547- drainSyncedRollbacks ( ) : ReadonlySet < string > {
548- const drained = this . pendingSyncedRollbacks ;
549- this . pendingSyncedRollbacks = new Set < string > ( ) ;
550- return drained ;
551- }
552-
553565 drainSyncedAdvances ( ) : ReadonlySet < string > {
554566 const drained = this . pendingSyncedAdvances ;
555567 this . pendingSyncedAdvances = new Set < string > ( ) ;
0 commit comments