Skip to content

Commit

Permalink
use chunk when ready lots of contacts (#1450)
Browse files Browse the repository at this point in the history
  • Loading branch information
huan committed Jul 11, 2018
1 parent a0c5d11 commit 9131dbb
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/user/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,23 @@ export class Contact extends Accessory implements Sayable {
const contactIdList: string[] = await this.puppet.contactSearch(query)
const contactList = contactIdList.map(id => this.load(id))

await Promise.all(contactList.map(c => c.ready()))
const CHUNK_SIZE = 16
let batchIndex = 0

while (batchIndex * CHUNK_SIZE < contactList.length) {
const contactListChunk = contactList.slice(
batchIndex * CHUNK_SIZE,
batchIndex * (CHUNK_SIZE + 1),
)
await Promise.all(
contactListChunk.map(
c => c.ready(),
),
)

batchIndex++
}

return contactList

} catch (e) {
Expand Down

0 comments on commit 9131dbb

Please sign in to comment.