refactor: use async everywhere

This commit is contained in:
Ankush Menat 2025-05-23 12:02:52 +05:30
parent d9a36686d4
commit 7387cc5fb5

View file

@ -46,6 +46,12 @@ export class DropdownConsole {
});
}
async sleep(duration) {
// because ace will be ace, this is needed to keep interaction between ace and rest of the
// code feel saner.
await new Promise((r) => setTimeout(r, duration));
}
show() {
this.dialog.show();
if (cur_frm && !cur_frm.is_new()) {
@ -59,69 +65,64 @@ export class DropdownConsole {
}
this.bind_executer();
this.load_completion();
this.load_completions();
}
bind_executer() {
async bind_executer() {
let me = this;
setTimeout(() => {
const field = this.dialog.get_field("console");
let editor = field.editor;
editor.setKeyboardHandler(null); // sorry emacs/vim users
editor.commands.addCommand({
name: "execute_code",
bindKey: {
// Shortcut keys
win: "Ctrl-Enter",
mac: "Command-Enter",
},
exec: function (editor) {
me.execute_code();
},
});
}, 200); // XXX: figure out a way to chain with readiness of ace?
await this.sleep(200);
const field = this.dialog.get_field("console");
let editor = field.editor;
editor.setKeyboardHandler(null); // sorry emacs/vim users
editor.commands.addCommand({
name: "execute_code",
bindKey: {
// Shortcut keys
win: "Ctrl-Enter",
mac: "Command-Enter",
},
exec: function (editor) {
me.execute_code();
},
});
}
async execute_code() {
this.dialog.set_value("output", "");
frappe
.xcall("frappe.desk.doctype.system_console.system_console.execute_code", {
let { output } = await frappe.xcall(
"frappe.desk.doctype.system_console.system_console.execute_code",
{
doc: {
console: this.dialog.get_value("console"),
doctype: "System Console",
type: "Python",
},
})
.then(({ output }) => {
this.dialog.set_value("output", output);
});
}
);
this.dialog.set_value("output", output);
}
async load_completion() {
async load_completions() {
let me = this;
setTimeout(() => {
frappe
.xcall(
"frappe.core.doctype.server_script.server_script.get_autocompletion_items",
null,
"GET",
{ cache: true }
)
.then((items) => {
const field = me.dialog.get_field("console");
const custom_completions = [];
if (cur_frm && !cur_frm.is_new()) {
frappe.meta
.get_fieldnames(cur_frm.doc.doctype, cur_frm.doc.parent, {
fieldtype: ["not in", frappe.model.no_value_type],
})
.forEach((fieldname) => {
custom_completions.push(`doc.${fieldname}`);
});
}
field.df.autocompletions = [...items, ...custom_completions];
await this.sleep(100);
let items = await frappe.xcall(
"frappe.core.doctype.server_script.server_script.get_autocompletion_items",
null,
"GET",
{ cache: true }
);
const field = me.dialog.get_field("console");
const custom_completions = [];
if (cur_frm && !cur_frm.is_new()) {
frappe.meta
.get_fieldnames(cur_frm.doc.doctype, cur_frm.doc.parent, {
fieldtype: ["not in", frappe.model.no_value_type],
})
.forEach((fieldname) => {
custom_completions.push(`doc.${fieldname}`);
});
}, 100);
}
field.df.autocompletions = [...items, ...custom_completions];
}
}