lib/client: extract fetchBatch function from roundtripChatHistory

This commit is contained in:
Simon Ser 2021-03-03 14:13:05 +01:00
parent 3ece5f1ca5
commit aa79067179

View File

@ -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;
}