【问题标题】:How to do pagination on channels in twilio-programmable-chat?如何在 twilio-programmable-chat 中对频道进行分页?
【发布时间】:2019-07-08 03:59:42
【问题描述】:

我正在使用 twilio javascript sdk 进行 twilio-programmable-chat。

我想对我的频道结果应用分页,但我无法弄清楚。

这是我当前的代码。

this.chatClient.getUserChannelDescriptors().then(paginator => {

  // All channels are fetched

})

我试图传递一个类似于getMessages(10) 工作方式的pageSize,但没有成功。

this.chatClient.getUserChannelDescriptors(10).then(paginator => {
 // The result was same, it fetched all the channels instead of just 10
})

我正在寻找一个如何在频道上进行分页的示例。 谢谢。

【问题讨论】:

标签: javascript reactjs twilio twilio-programmable-chat


【解决方案1】:

我终于找到了方法。

它应该递归完成,因为我们通过调用getUserChannelDescriptors()获取初始列表,但随后可以通过调用nextPage()获取其余记录;

async function processChannels(paginator) {

    // Now, if hasNextPage is true
    // call nextPage() to get the records instead of getUserChannelDescriptors()
    if (paginator.hasNextPage) {
        const nextPaginator = paginator.nextPage();
        processChannels(nextPaginator);
    } else {
        console.log("END OF RECORDS");
    }
}

async function getChannels() {
    const paginator = await chatClient.getUserChannelDescriptors();

    // Initiate the recursive function
    if (paginator.items) {
        await processChannels(paginator);
    }
}

这就是你每次通话都会得到的。

【讨论】:

  • 嗨.. 我正在使用 twilio js 开发一个聊天室,我正在使用 getSubscribedChannels() 列出用户的所有订阅频道。如果 getUserChannelDescriptors() 更好,需要一些建议吗?只是好奇你为什么使用 getUserChannelDescriptors 而不是 getSubscribedChannels()。
  • 另外,如果您可以建议分页结果是否按频道创建/更新日期或最后发送的消息时间戳排序(默认)。提前致谢。
  • 1. getUserChannelDescriptors() 提供了通道的简化版本,而不是 getSubscribedChannels(),后者获得了包含所有细节的通道的完整版本。 @tsikka
  • 2.默认频道按其创建/更新日期排序。 https://stackoverflow.com/a/54784463/6696353,这就是我按照最后一条消息对它们进行排序的方式@tsikka
  • 感谢您的回复。是的,我提到了你关于排序的答案。如果我有 100 个频道并且我在第一页获得最新的 50 个频道,我很担心,但是我在比如说第 60 个频道中有一条消息。我不会在第一页看到它,对吧?虽然这是一条较新的消息
【解决方案2】:

根据documentation,getUserChannelDescriptors 方法不带任何参数。

但您不必手动进行分页,因为该方法返回 Promise.<Paginator.<ChannelDescriptor>> 类型..这意味着您应该能够访问 twilio 提供的分页功能。

您的paginator.items 应该只有单个页面中的项目。

编辑: 基本上重点是您的第一个 sn-p 是正确的。不幸的是,twilio 不是开源的,所以我无法检查他们在哪里定义了page_size。但我鼓励你创建一百个模拟通道,然后检查 paginator.items 数组的大小。

试试这个:

this.chatClient.getUserChannelDescriptors().then(paginator => {
  console.log(paginator.items, paginator.hasNextPage(), paginator.hasPrevPage());
})

Paginator 类的文档是here

【讨论】:

  • 感谢您的回答我有点明白您的回答但不完全是,您能给我一个小例子吗?如何完成分页操作?
猜你喜欢
  • 1970-01-01
  • 2022-01-16
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-25
  • 2019-10-08
  • 2017-06-04
相关资源
最近更新 更多