diff --git a/lib/client.js b/lib/client.js index f567c8f..fca4776 100644 --- a/lib/client.js +++ b/lib/client.js @@ -370,6 +370,31 @@ export default class Client extends EventTarget { }); } + fetchBatch(msg, batchType) { + return this.roundtrip(msg, (event) => { + var msg = event.detail.message; + + switch (msg.command) { + case "BATCH": + var enter = msg.params[0].startsWith("+"); + var name = msg.params[0].slice(1); + if (enter) { + break; + } + var batch = this.batches.get(name); + if (batch.type === batchType) { + return batch; + } + break; + case "FAIL": + if (msg.params[0] === msg.command) { + throw msg; + } + break; + } + }); + } + roundtripChatHistory(params) { // Don't send multiple CHATHISTORY commands in parallel, we can't // properly handle batches and errors. @@ -378,28 +403,7 @@ export default class Client extends EventTarget { command: "CHATHISTORY", params, }; - return this.roundtrip(msg, (event) => { - var msg = event.detail.message; - - switch (msg.command) { - case "BATCH": - var enter = msg.params[0].startsWith("+"); - var name = msg.params[0].slice(1); - if (enter) { - break; - } - var batch = this.batches.get(name); - if (batch.type == "chathistory") { - return batch; - } - break; - case "FAIL": - if (msg.params[0] == "CHATHISTORY") { - throw msg; - } - break; - } - }); + return this.fetchBatch(msg, "chathistory"); }); return this.pendingHistory; }