Skip to content

Commit

Permalink
feat: update messageSet pagination on merge
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinCupela committed Aug 20, 2024
1 parent 90082b4 commit 369f108
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/channel_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,14 @@ export class ChannelState<StreamChatGenerics extends ExtendableGenerics = Defaul
sources.forEach((messageSet) => {
target.isLatest = target.isLatest || messageSet.isLatest;
target.isCurrent = target.isCurrent || messageSet.isCurrent;
target.pagination.hasPrev =
messageSet.messages[0].created_at < target.messages[0].created_at
? messageSet.pagination.hasPrev
: target.pagination.hasPrev;
target.pagination.hasNext =
target.messages.slice(-1)[0].created_at < messageSet.messages.slice(-1)[0].created_at
? messageSet.pagination.hasNext
: target.pagination.hasNext;
messagesToAdd = [...messagesToAdd, ...messageSet.messages];
});
sources.forEach((s) => this.messageSets.splice(this.messageSets.indexOf(s), 1));
Expand Down
3 changes: 3 additions & 0 deletions test/unit/channel_state.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,9 @@ describe('ChannelState addMessagesSorted', function () {
const currentMessages = [generateMsg({ id: '10', date: '2020-01-01T00:00:03.001Z' }), ...overlap1];
state.addMessagesSorted(currentMessages, false, true, true, 'new');
state.messageSets[0].isCurrent = false;
state.messageSets[0].pagination = {hasPrev: true, hasNext: false};
state.messageSets[1].isCurrent = true;
state.messageSets[1].pagination = {hasPrev: false, hasNext: true};
const newMessages = [...overlap1, generateMsg({ id: '12', date: '2020-01-01T00:00:14.001Z' }), ...overlap2];
state.addMessagesSorted(newMessages, false, true, true, 'new');

Expand All @@ -545,6 +547,7 @@ describe('ChannelState addMessagesSorted', function () {
expect(state.messages[4].id).to.be.equal('14');
expect(state.messages).to.be.equal(state.latestMessages);
expect(state.messageSets.length).to.be.equal(1);
expect(state.messageSets[0].pagination).to.be.eql({hasPrev: false, hasNext: false});
});
});
});
Expand Down

0 comments on commit 369f108

Please sign in to comment.