From 7387cc5fb5294dca6a1d7c55af5e2bfe1650ff34 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Fri, 23 May 2025 12:02:52 +0530 Subject: [PATCH] refactor: use async everywhere --- .../public/js/frappe/ui/dropdown_console.js | 97 ++++++++++--------- 1 file changed, 49 insertions(+), 48 deletions(-) diff --git a/frappe/public/js/frappe/ui/dropdown_console.js b/frappe/public/js/frappe/ui/dropdown_console.js index 6bee719424..8d138e8dd6 100644 --- a/frappe/public/js/frappe/ui/dropdown_console.js +++ b/frappe/public/js/frappe/ui/dropdown_console.js @@ -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]; } }