Skip to content

Commit 484d33f

Browse files
committed
feat: Make it possible to pause read events #267
1 parent 56830cb commit 484d33f

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

projects/stream-chat-angular/src/lib/channel.service.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,23 @@ describe('ChannelService', () => {
352352
expect(activeChannel.markRead).toHaveBeenCalledWith();
353353
});
354354

355+
it('should pause read events', async () => {
356+
await init();
357+
service.shouldMarkActiveChannelAsRead = false;
358+
let activeChannel!: Channel<DefaultStreamChatGenerics>;
359+
service.activeChannel$.subscribe((c) => (activeChannel = c!));
360+
const newMessage = mockMessage();
361+
activeChannel.state.messages.push(newMessage);
362+
spyOn(activeChannel, 'markRead');
363+
(activeChannel as MockChannel).handleEvent('message.new', newMessage);
364+
365+
expect(activeChannel.markRead).not.toHaveBeenCalledWith();
366+
367+
service.setAsActiveChannel(activeChannel);
368+
369+
expect(activeChannel.markRead).not.toHaveBeenCalledWith();
370+
});
371+
355372
it(`shouldn't make "markRead" call, if user dosen't have 'read-events' capability`, async () => {
356373
await init();
357374
let activeChannel!: Channel<DefaultStreamChatGenerics>;

projects/stream-chat-angular/src/lib/channel.service.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ export class ChannelService<
224224
url: string,
225225
channel: Channel<T>
226226
) => Promise<void>;
227+
/**
228+
* If set to false, read events won't be sent as new messages are received
229+
*/
230+
shouldMarkActiveChannelAsRead = true;
227231
private channelsSubject = new BehaviorSubject<Channel<T>[] | undefined>(
228232
undefined
229233
);
@@ -357,7 +361,7 @@ export class ChannelService<
357361
channel.state.messages.forEach((m) => {
358362
m.readBy = getReadBy(m, channel);
359363
});
360-
if (this.canSendReadEvents) {
364+
if (this.canSendReadEvents && this.shouldMarkActiveChannelAsRead) {
361365
void channel.markRead();
362366
}
363367
this.activeChannelMessagesSubject.next([...channel.state.messages]);
@@ -873,7 +877,7 @@ export class ChannelService<
873877
...channel.state.messages,
874878
]);
875879
this.activeChannel$.pipe(first()).subscribe((c) => {
876-
if (this.canSendReadEvents) {
880+
if (this.canSendReadEvents && this.shouldMarkActiveChannelAsRead) {
877881
void c?.markRead();
878882
}
879883
});

0 commit comments

Comments
 (0)