Skip to content

Commit

Permalink
Remove the unnecessary getHistory calls
Browse files Browse the repository at this point in the history
Part of #70
  • Loading branch information
Finesse committed May 6, 2020
1 parent af930e7 commit 744e283
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
14 changes: 10 additions & 4 deletions src/cache/fastStorages/indices/messageHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ interface IdsChunkReference {
readonly history: BehaviorSubject<IdsChunk>;

// Returns <0 if the message is older than chunk, =0 if inside chunk, >0 if newer than chunk, null when unknown.
getMessagePosition(messageId: number): number | null;
getMessageRelation(messageId: number): number | null;

// Returns the message index in the `history` property.
// A fractional number means that the message isn't in the chunk, but if it was, it would stand in the given intermediate position.
getMessageIndex(messageId: number): number;

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

if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line no-console
console.error('Unexpected empty chunk (no ids and not oldest or newest)');
console.error('Unexpected empty chunk (no ids and neither oldest nor newest)');
}
return -1;
}
Expand Down Expand Up @@ -228,7 +232,8 @@ class PeerIndex {
const chunkReference = {
isRevoked: false,
history: new BehaviorSubject({ ids: [] }),
getMessagePosition: (messageId: number) => compareChunksAndId(chunkReference.history.value, messageId),
getMessageRelation: (messageId: number) => compareChunksAndId(chunkReference.history.value, messageId),
getMessageIndex: (messageId: number) => findInIdsList(chunkReference.history.value.ids, messageId),
putChunk: (chunk: IdsChunk) => {
if (chunkReference.isRevoked) {
if (process.env.NODE_ENV !== 'production') {
Expand Down Expand Up @@ -392,7 +397,8 @@ export default function messageHistory(collection: Collection<Message, any>) {
const messagesChunkReference = {
isRevoked: false,
history: idsChunkReference.history,
getMessagePosition: idsChunkReference.getMessagePosition,
getMessageRelation: idsChunkReference.getMessageRelation,
getMessageIndex: idsChunkReference.getMessageIndex,
putChunk(chunk: MessagesChunk) {
try {
isUpdatingByThisIndex = true;
Expand Down
4 changes: 2 additions & 2 deletions src/services/message/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default class MessagesService {
if (isPeerChanged) {
isChunkChanged = true;
} else if (this.currentChunk && messageId) {
const messagePosition = this.currentChunk.getMessagePosition(messageId);
const messagePosition = this.currentChunk.getMessageRelation(messageId);
if (messagePosition !== 0) {
isChunkChanged = true;
if (messagePosition !== null) {
Expand All @@ -102,7 +102,7 @@ export default class MessagesService {
if (!isPeerChanged && this.currentChunk) {
// Keep the current chunk until the next is loaded
// Don't recreate the next chunk if it contains the target message
if (!this.nextChunk || this.nextChunk.getMessagePosition(messageId!) !== 0) {
if (!this.nextChunk || this.nextChunk.getMessageRelation(messageId!) !== 0) {
if (this.nextChunk) {
this.nextChunk.destroy();
}
Expand Down
53 changes: 34 additions & 19 deletions src/services/message/message_chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface MessageChunkService {
readonly history: BehaviorSubject<MessageHistoryChunk>;

// Returns <0 if the message is older than chunk, =0 if inside chunk, >0 if newer than chunk, null when unknown.
getMessagePosition(messageId: number): number | null;
getMessageRelation(messageId: number): number | null;

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

Expand Down Expand Up @@ -69,22 +69,20 @@ export default function makeMessageChunk(peer: Peer, messageId: Exclude<number,
return;
}

if (isDestroyed) {
return;
}

try {
isUpdatingCacheChunk = true;
cacheChunkRef.putChunk(result);
} finally {
isUpdatingCacheChunk = false;
}
} finally {
historySubject.next({
...cacheChunkRef.history.value,
loadingOlder: direction === Direction.Around || direction === Direction.Older ? false : historySubject.value.loadingOlder,
loadingNewer: direction === Direction.Around || direction === Direction.Newer ? false : historySubject.value.loadingNewer,
});
if (!isDestroyed) {
historySubject.next({
...cacheChunkRef.history.value,
loadingOlder: direction === Direction.Around || direction === Direction.Older ? false : historySubject.value.loadingOlder,
loadingNewer: direction === Direction.Around || direction === Direction.Newer ? false : historySubject.value.loadingNewer,
});
}
}
}

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

if (messageId === Infinity) {
const { ids } = historySubject.value;
if (ids.length < LOAD_CHUNK_LENGTH) {
loadMessages(Direction.Older, ids[0] /* undefined is welcome here */);
function makeSureChunkHasMessages() {
const { ids, newestReached, oldestReached } = cacheChunkRef.history.value;

if (messageId === Infinity) {
if (ids.length < LOAD_CHUNK_LENGTH && !oldestReached) {
loadMessages(Direction.Older, ids.length ? ids[ids.length - 1] : undefined);
}
return;
}
if (ids.length > 0) {
loadMessages(Direction.Newer, ids[0], 0);

if (messageId === -Infinity) {
if (ids.length < LOAD_CHUNK_LENGTH && !newestReached) {
loadMessages(Direction.Older, ids.length ? ids[0] : undefined);
}
return;
}

const messageIndex = cacheChunkRef.getMessageIndex(messageId);
const minSideCount = Math.floor(LOAD_CHUNK_LENGTH / 2) - 1;
if (
(messageIndex < minSideCount && !newestReached)
|| (ids.length - messageIndex - 1 < minSideCount && !oldestReached)
) {
loadMessages(Direction.Around, messageId);
}
} else {
loadMessages(Direction.Around, messageId);
}

makeSureChunkHasMessages();

return {
history: historySubject,
loadMore,
getMessagePosition: cacheChunkRef.getMessagePosition,
getMessageRelation: cacheChunkRef.getMessageRelation,
destroy,
};
}

0 comments on commit 744e283

Please sign in to comment.