Migrate to async/await

This commit is contained in:
Simon Ser 2023-04-19 12:51:13 +02:00
parent 57f64e9cc2
commit 535bdb2f52
3 changed files with 124 additions and 132 deletions

View file

@ -902,62 +902,56 @@ export default class Client extends EventTarget {
}
/* Fetch one page of history before the given date. */
fetchHistoryBefore(target, before, limit) {
async fetchHistoryBefore(target, before, limit) {
let max = Math.min(limit, this.isupport.chatHistory());
let params = ["BEFORE", target, "timestamp=" + before, max];
return this.roundtripChatHistory(params).then((messages) => {
return { messages, more: messages.length >= max };
});
let messages = await this.roundtripChatHistory(params);
return { messages, more: messages.length >= max };
}
/* Fetch history in ascending order. */
fetchHistoryBetween(target, after, before, limit) {
async fetchHistoryBetween(target, after, before, limit) {
let max = Math.min(limit, this.isupport.chatHistory());
let params = ["AFTER", target, "timestamp=" + after.time, max];
return this.roundtripChatHistory(params).then((messages) => {
limit -= messages.length;
if (limit <= 0) {
throw new Error("Cannot fetch all chat history: too many messages");
}
if (messages.length >= max) {
// There are still more messages to fetch
after.time = messages[messages.length - 1].tags.time;
return this.fetchHistoryBetween(target, after, before, limit);
}
return { messages };
});
let messages = await this.roundtripChatHistory(params);
limit -= messages.length;
if (limit <= 0) {
throw new Error("Cannot fetch all chat history: too many messages");
}
if (messages.length >= max) {
// There are still more messages to fetch
after.time = messages[messages.length - 1].tags.time;
return await this.fetchHistoryBetween(target, after, before, limit);
}
return { messages };
}
fetchHistoryTargets(t1, t2) {
async fetchHistoryTargets(t1, t2) {
let msg = {
command: "CHATHISTORY",
params: ["TARGETS", "timestamp=" + t1, "timestamp=" + t2, 1000],
};
return this.fetchBatch(msg, "draft/chathistory-targets").then((batch) => {
return batch.messages.map((msg) => {
if (msg.command != "CHATHISTORY" || msg.params[0] != "TARGETS") {
throw new Error("Cannot fetch chat history targets: unexpected message " + msg);
}
return {
name: msg.params[1],
latestMessage: msg.params[2],
};
});
let batch = await this.fetchBatch(msg, "draft/chathistory-targets");
return batch.messages.map((msg) => {
console.assert(msg.command === "CHATHISTORY" && msg.params[0] === "TARGETS");
return {
name: msg.params[1],
latestMessage: msg.params[2],
};
});
}
listBouncerNetworks() {
async listBouncerNetworks() {
let req = { command: "BOUNCER", params: ["LISTNETWORKS"] };
return this.fetchBatch(req, "soju.im/bouncer-networks").then((batch) => {
let networks = new Map();
for (let msg of batch.messages) {
console.assert(msg.command === "BOUNCER" && msg.params[0] === "NETWORK");
let id = msg.params[1];
let params = irc.parseTags(msg.params[2]);
networks.set(id, params);
}
return networks;
});
let batch = await this.fetchBatch(req, "soju.im/bouncer-networks");
let networks = new Map();
for (let msg of batch.messages) {
console.assert(msg.command === "BOUNCER" && msg.params[0] === "NETWORK");
let id = msg.params[1];
let params = irc.parseTags(msg.params[2]);
networks.set(id, params);
}
return networks;
}
monitor(target) {