Skip to content

Commit 744e283

Browse files
committed
Remove the unnecessary getHistory calls
Part of #70
1 parent af930e7 commit 744e283

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

src/cache/fastStorages/indices/messageHistory.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ interface IdsChunkReference {
99
readonly history: BehaviorSubject<IdsChunk>;
1010

1111
// Returns <0 if the message is older than chunk, =0 if inside chunk, >0 if newer than chunk, null when unknown.
12-
getMessagePosition(messageId: number): number | null;
12+
getMessageRelation(messageId: number): number | null;
13+
14+
// Returns the message index in the `history` property.
15+
// A fractional number means that the message isn't in the chunk, but if it was, it would stand in the given intermediate position.
16+
getMessageIndex(messageId: number): number;
1317

1418
// The chunk must intersect or directly touch the referenced chunk
1519
putChunk(chunk: IdsChunk): void;
@@ -94,7 +98,7 @@ function compareChunksAndIdForOrder(chunk: IdsChunk, id: number): number {
9498

9599
if (process.env.NODE_ENV !== 'production') {
96100
// eslint-disable-next-line no-console
97-
console.error('Unexpected empty chunk (no ids and not oldest or newest)');
101+
console.error('Unexpected empty chunk (no ids and neither oldest nor newest)');
98102
}
99103
return -1;
100104
}
@@ -228,7 +232,8 @@ class PeerIndex {
228232
const chunkReference = {
229233
isRevoked: false,
230234
history: new BehaviorSubject({ ids: [] }),
231-
getMessagePosition: (messageId: number) => compareChunksAndId(chunkReference.history.value, messageId),
235+
getMessageRelation: (messageId: number) => compareChunksAndId(chunkReference.history.value, messageId),
236+
getMessageIndex: (messageId: number) => findInIdsList(chunkReference.history.value.ids, messageId),
232237
putChunk: (chunk: IdsChunk) => {
233238
if (chunkReference.isRevoked) {
234239
if (process.env.NODE_ENV !== 'production') {
@@ -392,7 +397,8 @@ export default function messageHistory(collection: Collection<Message, any>) {
392397
const messagesChunkReference = {
393398
isRevoked: false,
394399
history: idsChunkReference.history,
395-
getMessagePosition: idsChunkReference.getMessagePosition,
400+
getMessageRelation: idsChunkReference.getMessageRelation,
401+
getMessageIndex: idsChunkReference.getMessageIndex,
396402
putChunk(chunk: MessagesChunk) {
397403
try {
398404
isUpdatingByThisIndex = true;

src/services/message/message.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export default class MessagesService {
8484
if (isPeerChanged) {
8585
isChunkChanged = true;
8686
} else if (this.currentChunk && messageId) {
87-
const messagePosition = this.currentChunk.getMessagePosition(messageId);
87+
const messagePosition = this.currentChunk.getMessageRelation(messageId);
8888
if (messagePosition !== 0) {
8989
isChunkChanged = true;
9090
if (messagePosition !== null) {
@@ -102,7 +102,7 @@ export default class MessagesService {
102102
if (!isPeerChanged && this.currentChunk) {
103103
// Keep the current chunk until the next is loaded
104104
// Don't recreate the next chunk if it contains the target message
105-
if (!this.nextChunk || this.nextChunk.getMessagePosition(messageId!) !== 0) {
105+
if (!this.nextChunk || this.nextChunk.getMessageRelation(messageId!) !== 0) {
106106
if (this.nextChunk) {
107107
this.nextChunk.destroy();
108108
}

src/services/message/message_chunk.ts

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface MessageChunkService {
1414
readonly history: BehaviorSubject<MessageHistoryChunk>;
1515

1616
// 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;
1818

1919
loadMore(direction: Direction.Newer | Direction.Older): void;
2020

@@ -69,22 +69,20 @@ export default function makeMessageChunk(peer: Peer, messageId: Exclude<number,
6969
return;
7070
}
7171

72-
if (isDestroyed) {
73-
return;
74-
}
75-
7672
try {
7773
isUpdatingCacheChunk = true;
7874
cacheChunkRef.putChunk(result);
7975
} finally {
8076
isUpdatingCacheChunk = false;
8177
}
8278
} 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+
}
8886
}
8987
}
9088

@@ -129,22 +127,39 @@ export default function makeMessageChunk(peer: Peer, messageId: Exclude<number,
129127
cacheChunkRef.revoke();
130128
}
131129

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;
136138
}
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);
139154
}
140-
} else {
141-
loadMessages(Direction.Around, messageId);
142155
}
143156

157+
makeSureChunkHasMessages();
158+
144159
return {
145160
history: historySubject,
146161
loadMore,
147-
getMessagePosition: cacheChunkRef.getMessagePosition,
162+
getMessageRelation: cacheChunkRef.getMessageRelation,
148163
destroy,
149164
};
150165
}

0 commit comments

Comments
 (0)