@@ -34,6 +34,7 @@ import {
3434 DefaultStreamChatGenerics ,
3535 MessageInput ,
3636 MessageReactionType ,
37+ NextPageConfiguration ,
3738 StreamMessage ,
3839} from './types' ;
3940
@@ -293,6 +294,10 @@ export class ChannelService<
293294 beforeUpdateMessage ?: (
294295 message : StreamMessage < T >
295296 ) => StreamMessage < T > | Promise < StreamMessage < T > > ;
297+ /**
298+ * By default the SDK uses an offset based pagination, you can change/extend this by providing your own custom paginator method. It will be called with the result of the latest channel query.
299+ */
300+ customPaginator ?: ( channelQueryResult : Channel < T > [ ] ) => NextPageConfiguration ;
296301 private channelsSubject = new BehaviorSubject < Channel < T > [ ] | undefined > (
297302 undefined
298303 ) ;
@@ -374,6 +379,7 @@ export class ChannelService<
374379 this . activeParentMessageIdSubject . next ( message ?. id ) ;
375380 } ;
376381 private dismissErrorNotification ?: Function ;
382+ private nextPageConfiguration ?: NextPageConfiguration ;
377383
378384 constructor (
379385 private chatClientService : ChatClientService < T > ,
@@ -650,15 +656,14 @@ export class ChannelService<
650656 ) {
651657 this . filters = filters ;
652658 this . options = {
653- offset : 0 ,
654659 limit : 25 ,
655660 state : true ,
656661 presence : true ,
657662 watch : true ,
658663 message_limit : this . messagePageSize ,
659664 ...options ,
660665 } ;
661- this . sort = sort || { last_message_at : - 1 , updated_at : - 1 } ;
666+ this . sort = sort || { last_message_at : - 1 } ;
662667 this . shouldSetActiveChannel = shouldSetActiveChannel ;
663668 this . clientEventsSubscription = this . chatClientService . events$ . subscribe (
664669 ( notification ) => void this . handleNotification ( notification )
@@ -1107,9 +1112,7 @@ export class ChannelService<
11071112 }
11081113 this . isStateRecoveryInProgress = true ;
11091114 try {
1110- if ( this . options ) {
1111- this . options . offset = 0 ;
1112- }
1115+ this . nextPageConfiguration = undefined ;
11131116 // If channel list is not inited, we set the active channel
11141117 const shoulSetActiveChannel =
11151118 this . shouldSetActiveChannel &&
@@ -1377,10 +1380,16 @@ export class ChannelService<
13771380 await activeChannel ?. stopTyping ( parentId ) ;
13781381 }
13791382
1383+ /**
1384+ * The current list of channels
1385+ */
13801386 get channels ( ) {
13811387 return this . channelsSubject . getValue ( ) || [ ] ;
13821388 }
13831389
1390+ /**
1391+ * The current active channel
1392+ */
13841393 get activeChannel ( ) {
13851394 return this . activeChannelSubject . getValue ( ) || undefined ;
13861395 }
@@ -1480,12 +1489,32 @@ export class ChannelService<
14801489 ) {
14811490 try {
14821491 this . channelQueryStateSubject . next ( { state : 'in-progress' } ) ;
1492+ let filters : ChannelFilters < T > ;
1493+ let options : ChannelOptions ;
1494+ if ( this . nextPageConfiguration ) {
1495+ if ( this . nextPageConfiguration . type === 'filter' ) {
1496+ filters = {
1497+ ...this . filters ! ,
1498+ ...this . nextPageConfiguration . paginationFilter ,
1499+ } ;
1500+ options = this . options as ChannelOptions ;
1501+ } else {
1502+ options = {
1503+ ...this . options ,
1504+ offset : this . nextPageConfiguration . offset ,
1505+ } ;
1506+ filters = this . filters ! ;
1507+ }
1508+ } else {
1509+ filters = this . filters ! ;
1510+ options = this . options as ChannelOptions ;
1511+ }
14831512 const channels = await this . chatClientService . chatClient . queryChannels (
1484- this . filters ! ,
1513+ filters ,
14851514 this . sort || { } ,
1486- this . options
1515+ options
14871516 ) ;
1488- this . options ! . offset = channels . length ! ;
1517+ this . setNextPageConfiguration ( channels ) ;
14891518 channels . forEach ( ( c ) => this . watchForChannelEvents ( c ) ) ;
14901519 const prevChannels = recoverState
14911520 ? [ ]
@@ -1898,4 +1927,18 @@ export class ChannelService<
18981927 void channel . markRead ( ) ;
18991928 }
19001929 }
1930+
1931+ private setNextPageConfiguration ( channelQueryResult : Channel < T > [ ] ) {
1932+ if ( this . customPaginator ) {
1933+ this . nextPageConfiguration = this . customPaginator ( channelQueryResult ) ;
1934+ } else {
1935+ this . nextPageConfiguration = {
1936+ type : 'offset' ,
1937+ offset :
1938+ ( this . nextPageConfiguration ?. type === 'offset'
1939+ ? this . nextPageConfiguration . offset
1940+ : 0 ) + channelQueryResult . length ,
1941+ } ;
1942+ }
1943+ }
19011944}
0 commit comments