Skip to content

Commit 8ebc418

Browse files
committed
feat: Add option to not set active channel after ChannelService init #248
1 parent cde2324 commit 8ebc418

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ describe('ChannelService', () => {
3535
c?: Channel[],
3636
sort?: ChannelSort,
3737
options?: ChannelOptions,
38-
mockChannelQuery?: Function
38+
mockChannelQuery?: Function,
39+
shouldSetActiveChannel?: boolean
3940
) => Promise<Channel[]>;
4041
let user: UserResponse;
4142
const filters = { type: 'messaging' };
@@ -70,7 +71,8 @@ describe('ChannelService', () => {
7071
channels?: Channel[],
7172
sort?: ChannelSort,
7273
options?: ChannelOptions,
73-
mockChannelQuery?: Function
74+
mockChannelQuery?: Function,
75+
shouldSetActiveChannel?: boolean
7476
) => {
7577
if (mockChannelQuery) {
7678
mockChannelQuery();
@@ -80,7 +82,7 @@ describe('ChannelService', () => {
8082
);
8183
}
8284

83-
return service.init(filters, sort, options);
85+
return service.init(filters, sort, options, shouldSetActiveChannel);
8486
};
8587
});
8688

@@ -145,6 +147,15 @@ describe('ChannelService', () => {
145147
).toBeRejectedWith(error);
146148
});
147149

150+
it('should not set active channel if #shouldSetActiveChannel is false', async () => {
151+
const activeChannelSpy = jasmine.createSpy();
152+
service.activeChannel$.subscribe(activeChannelSpy);
153+
activeChannelSpy.calls.reset();
154+
await init(undefined, undefined, undefined, undefined, false);
155+
156+
expect(activeChannelSpy).not.toHaveBeenCalled();
157+
});
158+
148159
it('should reset', async () => {
149160
await init();
150161
const messagesSpy = jasmine.createSpy();

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,14 @@ export class ChannelService {
392392
* @param filters
393393
* @param sort
394394
* @param options
395+
* @param shouldSetActiveChannel Decides if the firs channel in the result should be made as active channel, or no channel should be marked as active
395396
* @returns the list of channels found by the query
396397
*/
397398
async init(
398399
filters: ChannelFilters,
399400
sort?: ChannelSort,
400-
options?: ChannelOptions
401+
options?: ChannelOptions,
402+
shouldSetActiveChannel: boolean = true
401403
) {
402404
this.filters = filters;
403405
this.options = options || {
@@ -409,7 +411,7 @@ export class ChannelService {
409411
message_limit: this.messagePageSize,
410412
};
411413
this.sort = sort || { last_message_at: -1, updated_at: -1 };
412-
const result = await this.queryChannels();
414+
const result = await this.queryChannels(shouldSetActiveChannel);
413415
this.chatClientService.notification$.subscribe(
414416
(notification) => void this.handleNotification(notification)
415417
);
@@ -434,7 +436,7 @@ export class ChannelService {
434436
*/
435437
async loadMoreChannels() {
436438
this.options!.offset = this.channels.length!;
437-
await this.queryChannels();
439+
await this.queryChannels(false);
438440
}
439441

440442
/**
@@ -950,7 +952,7 @@ export class ChannelService {
950952
this.activeChannelSubscriptions = [];
951953
}
952954

953-
private async queryChannels() {
955+
private async queryChannels(shouldSetActiveChannel: boolean) {
954956
try {
955957
const channels = await this.chatClientService.chatClient.queryChannels(
956958
this.filters!,
@@ -960,7 +962,11 @@ export class ChannelService {
960962
channels.forEach((c) => this.watchForChannelEvents(c));
961963
const prevChannels = this.channelsSubject.getValue() || [];
962964
this.channelsSubject.next([...prevChannels, ...channels]);
963-
if (channels.length > 0 && !this.activeChannelSubject.getValue()) {
965+
if (
966+
channels.length > 0 &&
967+
!this.activeChannelSubject.getValue() &&
968+
shouldSetActiveChannel
969+
) {
964970
this.setAsActiveChannel(channels[0]);
965971
}
966972
this.hasMoreChannelsSubject.next(channels.length >= this.options!.limit!);

0 commit comments

Comments
 (0)