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

@ -25,14 +25,14 @@ function getActiveChannel(app) {
return activeBuffer.name;
}
function setUserHostMode(app, args, mode) {
async function setUserHostMode(app, args, mode) {
let nick = args[0];
if (!nick) {
throw new Error("Missing nick");
}
let activeChannel = getActiveChannel(app);
let client = getActiveClient(app);
client.whois(nick).then((whois) => {
let whois = await client.whois(nick);
const info = whois[irc.RPL_WHOISUSER].params;
const user = info[2];
const host = info[3];
@ -40,7 +40,6 @@ function setUserHostMode(app, args, mode) {
command: "MODE",
params: [activeChannel, mode, `*!${user}@${host}`],
});
});
}
function markServerBufferUnread(app) {

View file

@ -1209,7 +1209,7 @@ export default class App extends Component {
});
}
fetchBacklog(serverID) {
async fetchBacklog(serverID) {
let client = this.clients.get(serverID);
if (!client.caps.enabled.has("draft/chathistory")) {
return;
@ -1224,8 +1224,8 @@ export default class App extends Component {
}
let now = irc.formatDate(new Date());
client.fetchHistoryTargets(now, lastReceipt.time).then((targets) => {
targets.forEach((target) => {
let targets = await client.fetchHistoryTargets(now, lastReceipt.time);
targets.forEach(async (target) => {
let from = lastReceipt;
let to = { time: now };
@ -1251,18 +1251,21 @@ export default class App extends Component {
client.fetchReadMarker(target.name);
}
client.fetchHistoryBetween(target.name, from, to, CHATHISTORY_MAX_SIZE).then((result) => {
let result;
try {
result = await client.fetchHistoryBetween(target.name, from, to, CHATHISTORY_MAX_SIZE);
} catch (err) {
console.error("Failed to fetch backlog for '" + target.name + "': ", err);
this.showError("Failed to fetch backlog for '" + target.name + "'");
return;
}
for (let msg of result.messages) {
let destBuffers = this.routeMessage(serverID, msg);
for (let bufName of destBuffers) {
this.handleChatMessage(serverID, bufName, msg);
}
}
}).catch((err) => {
console.error("Failed to fetch backlog for '" + target.name + "': ", err);
this.showError("Failed to fetch backlog for '" + target.name + "'");
});
});
});
}
@ -1367,14 +1370,13 @@ export default class App extends Component {
}
}
whoChannelBuffer(target, serverID) {
async whoChannelBuffer(target, serverID) {
let client = this.clients.get(serverID);
client.who(target, {
await client.who(target, {
fields: ["flags", "hostname", "nick", "realname", "username", "account"],
}).then(() => {
this.setBufferState({ name: target, server: serverID }, { hasInitialWho: true });
});
this.setBufferState({ name: target, server: serverID }, { hasInitialWho: true });
}
open(target, serverID, password) {
@ -1646,7 +1648,7 @@ export default class App extends Component {
this.openDialog("help");
}
handleBufferScrollTop() {
async handleBufferScrollTop() {
let buf = this.state.buffers.get(this.state.activeBuffer);
if (!buf || buf.type == BufferType.SERVER) {
return;
@ -1676,7 +1678,7 @@ export default class App extends Component {
limit = 200;
}
client.fetchHistoryBefore(buf.name, before, limit).then((result) => {
let result = await client.fetchHistoryBefore(buf.name, before, limit);
this.endOfHistory.set(buf.id, !result.more);
if (result.messages.length > 0) {
@ -1704,7 +1706,6 @@ export default class App extends Component {
for (let msg of result.messages) {
this.addChatMessage(buf.server, buf.name, msg);
}
});
}
openDialog(name, data) {
@ -1822,12 +1823,13 @@ export default class App extends Component {
});
}
handleNetworkSubmit(attrs, autojoin) {
async handleNetworkSubmit(attrs, autojoin) {
let client = this.clients.values().next().value;
this.dismissDialog();
if (this.state.dialogData && this.state.dialogData.id) {
if (Object.keys(attrs).length == 0) {
this.dismissDialog();
return;
}
@ -1837,7 +1839,7 @@ export default class App extends Component {
});
} else {
attrs = { ...attrs, tls: "1" };
client.createBouncerNetwork(attrs).then((id) => {
let id = await client.createBouncerNetwork(attrs);
if (!autojoin) {
return;
}
@ -1849,10 +1851,7 @@ export default class App extends Component {
client.params.autojoin = [autojoin];
this.switchToChannel = autojoin;
});
}
this.dismissDialog();
}
handleNetworkRemove() {

View file

@ -902,19 +902,18 @@ 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) => {
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) => {
let messages = await this.roundtripChatHistory(params);
limit -= messages.length;
if (limit <= 0) {
throw new Error("Cannot fetch all chat history: too many messages");
@ -922,33 +921,29 @@ export default class Client extends EventTarget {
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 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) => {
let batch = await this.fetchBatch(msg, "draft/chathistory-targets");
return batch.messages.map((msg) => {
if (msg.command != "CHATHISTORY" || msg.params[0] != "TARGETS") {
throw new Error("Cannot fetch chat history targets: unexpected message " + 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 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");
@ -957,7 +952,6 @@ export default class Client extends EventTarget {
networks.set(id, params);
}
return networks;
});
}
monitor(target) {