如何在twilio可编程聊天中的频道上进行分页?

时间:2019-02-14 07:22:33

标签: javascript reactjs twilio twilio-programmable-chat

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

我想对频道结果进行分页,但是我无法弄清楚。

这是我当前的代码。

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

  // All channels are fetched

})

我试图通过pageSize类似于getMessages(10)的工作方式,但是没有成功。

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

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

2 个答案:

答案 0 :(得分:2)

我终于找到了一种方法。

由于我们是通过调用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);
    }
}

这就是您在每次通话中都会得到的。

答案 1 :(得分:1)

根据documentationgetUserChannelDescriptors方法不带任何参数。

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

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

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

尝试一下:

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

Paginator类的文档为here

相关问题