@@ -14,7 +14,7 @@ export interface MessageChunkService {
14
14
readonly history : BehaviorSubject < MessageHistoryChunk > ;
15
15
16
16
// Returns <0 if the message is older than chunk, =0 if inside chunk, >0 if newer than chunk, null when unknown.
17
- getMessagePosition ( messageId : number ) : number | null ;
17
+ getMessageRelation ( messageId : number ) : number | null ;
18
18
19
19
loadMore ( direction : Direction . Newer | Direction . Older ) : void ;
20
20
@@ -69,22 +69,20 @@ export default function makeMessageChunk(peer: Peer, messageId: Exclude<number,
69
69
return ;
70
70
}
71
71
72
- if ( isDestroyed ) {
73
- return ;
74
- }
75
-
76
72
try {
77
73
isUpdatingCacheChunk = true ;
78
74
cacheChunkRef . putChunk ( result ) ;
79
75
} finally {
80
76
isUpdatingCacheChunk = false ;
81
77
}
82
78
} finally {
83
- historySubject . next ( {
84
- ...cacheChunkRef . history . value ,
85
- loadingOlder : direction === Direction . Around || direction === Direction . Older ? false : historySubject . value . loadingOlder ,
86
- loadingNewer : direction === Direction . Around || direction === Direction . Newer ? false : historySubject . value . loadingNewer ,
87
- } ) ;
79
+ if ( ! isDestroyed ) {
80
+ historySubject . next ( {
81
+ ...cacheChunkRef . history . value ,
82
+ loadingOlder : direction === Direction . Around || direction === Direction . Older ? false : historySubject . value . loadingOlder ,
83
+ loadingNewer : direction === Direction . Around || direction === Direction . Newer ? false : historySubject . value . loadingNewer ,
84
+ } ) ;
85
+ }
88
86
}
89
87
}
90
88
@@ -129,22 +127,39 @@ export default function makeMessageChunk(peer: Peer, messageId: Exclude<number,
129
127
cacheChunkRef . revoke ( ) ;
130
128
}
131
129
132
- if ( messageId === Infinity ) {
133
- const { ids } = historySubject . value ;
134
- if ( ids . length < LOAD_CHUNK_LENGTH ) {
135
- loadMessages ( Direction . Older , ids [ 0 ] /* undefined is welcome here */ ) ;
130
+ function makeSureChunkHasMessages ( ) {
131
+ const { ids, newestReached, oldestReached } = cacheChunkRef . history . value ;
132
+
133
+ if ( messageId === Infinity ) {
134
+ if ( ids . length < LOAD_CHUNK_LENGTH && ! oldestReached ) {
135
+ loadMessages ( Direction . Older , ids . length ? ids [ ids . length - 1 ] : undefined ) ;
136
+ }
137
+ return ;
136
138
}
137
- if ( ids . length > 0 ) {
138
- loadMessages ( Direction . Newer , ids [ 0 ] , 0 ) ;
139
+
140
+ if ( messageId === - Infinity ) {
141
+ if ( ids . length < LOAD_CHUNK_LENGTH && ! newestReached ) {
142
+ loadMessages ( Direction . Older , ids . length ? ids [ 0 ] : undefined ) ;
143
+ }
144
+ return ;
145
+ }
146
+
147
+ const messageIndex = cacheChunkRef . getMessageIndex ( messageId ) ;
148
+ const minSideCount = Math . floor ( LOAD_CHUNK_LENGTH / 2 ) - 1 ;
149
+ if (
150
+ ( messageIndex < minSideCount && ! newestReached )
151
+ || ( ids . length - messageIndex - 1 < minSideCount && ! oldestReached )
152
+ ) {
153
+ loadMessages ( Direction . Around , messageId ) ;
139
154
}
140
- } else {
141
- loadMessages ( Direction . Around , messageId ) ;
142
155
}
143
156
157
+ makeSureChunkHasMessages ( ) ;
158
+
144
159
return {
145
160
history : historySubject ,
146
161
loadMore,
147
- getMessagePosition : cacheChunkRef . getMessagePosition ,
162
+ getMessageRelation : cacheChunkRef . getMessageRelation ,
148
163
destroy,
149
164
} ;
150
165
}
0 commit comments