From 7ee8efc5af69bf2d4e0c908fe330d95196474e8d Mon Sep 17 00:00:00 2001 From: petoncle Date: Tue, 31 Oct 2023 09:35:02 +0100 Subject: [PATCH] Wait for commandline_frame message listener to be set up before trying to send messages from content process --- src/commandline_frame.ts | 3 +++ src/content/controller_content.ts | 20 ++++++++++++++------ src/excmds.ts | 1 + src/lib/messaging.ts | 1 + 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/commandline_frame.ts b/src/commandline_frame.ts index 3caaa430..6a717d71 100644 --- a/src/commandline_frame.ts +++ b/src/commandline_frame.ts @@ -405,6 +405,7 @@ export function editor_function(fn_name: keyof typeof tri_editor, ...args) { } Messaging.addListener("commandline_frame", Messaging.attributeCaller(SELF)) +logger.debug("Added commandline_frame message listener") commandline_state.fns = getCommandlineFns(commandline_state) Messaging.addListener( @@ -420,3 +421,5 @@ Messaging.addListener( ;(window as any).tri = Object.assign(window.tri || {}, { perfObserver: perf.listenForCounters(), }) + +Messaging.messageOwnTab("commandline_frame_ready_to_receive_messages") diff --git a/src/content/controller_content.ts b/src/content/controller_content.ts index 5b1950de..93f25284 100644 --- a/src/content/controller_content.ts +++ b/src/content/controller_content.ts @@ -102,6 +102,12 @@ class KeyCanceller { export const canceller = new KeyCanceller() +let commandlineFrameReadyToReceiveMessages = false +Messaging.addListener("commandline_frame_ready_to_receive_messages", () => { + logger.debug("Received commandline_frame_ready_to_receive_messages") + commandlineFrameReadyToReceiveMessages = true +}) + let mustBufferPageKeysForClInput = false let bufferedPageKeys: string[] = [] let bufferingPageKeysBeginTime: number @@ -244,12 +250,6 @@ export function acceptKey(keyevent: KeyboardEvent) { const bufferingDuration = performance.now() - bufferingPageKeysBeginTime; logger.debug("controller_content mustBufferPageKeysForClInput = " + mustBufferPageKeysForClInput + " bufferingDuration = " + bufferingDuration + "ms"); - // Stop buffering keys after 5s if something goes wrong and clInput never gets focused. - if (bufferingDuration > 5000) { - logger.debug("Aborting buffering of page keys since clInput still has not been focused") - mustBufferPageKeysForClInput = false - return false - } const isCharacterKey = keyevent.key.length == 1 && !keyevent.metaKey && !keyevent.ctrlKey && !keyevent.altKey && !keyevent.metaKey; if (isCharacterKey) { @@ -259,6 +259,14 @@ export function acceptKey(keyevent: KeyboardEvent) { canceller.push(keyevent) return true } + if (!commandlineFrameReadyToReceiveMessages) { + // If commandline frame cannot receive message, excmds.fillcmdline() will send a fillcmdline message to the + // commandline frame that it will never receive. As a result, clInput will not be focused (commandline_frame.focus() will not be called). + // If we (content/page process) start buffering keys for clInput, but clInput is not focused, then commandline_frame.focus() will not run, + // and it will not send the buffered_page_keys message back to us, and we will keep buffering (and eating events) forever. + logger.debug("controller_content Ignoring key event since commandline frame is not yet ready to receive messages", keyevent) + return + } if (!tryBufferingPageKeyForClInput(keyevent)) return generator.next(keyevent) } diff --git a/src/excmds.ts b/src/excmds.ts index af528ee9..ae530c32 100644 --- a/src/excmds.ts +++ b/src/excmds.ts @@ -3882,6 +3882,7 @@ export function hidecmdline() { export function fillcmdline(...strarr: string[]) { const str = strarr.join(" ") showcmdline(false) + logger.debug("excmds fillcmdline sending fillcmdline to commandline_frame") return Messaging.messageOwnTab("commandline_frame", "fillcmdline", [str, true/*trailspace*/, true/*focus*/]) } diff --git a/src/lib/messaging.ts b/src/lib/messaging.ts index dfc80137..7f9cf540 100644 --- a/src/lib/messaging.ts +++ b/src/lib/messaging.ts @@ -16,6 +16,7 @@ export type TabMessageType = | "alive" | "tab_changes" | "buffered_page_keys" + | "commandline_frame_ready_to_receive_messages" export type NonTabMessageType = | "owntab_background"