Wait for commandline_frame message listener to be set up before trying to send messages from content process

This commit is contained in:
petoncle 2023-10-31 09:35:02 +01:00
parent 8f6acd27ca
commit 7ee8efc5af
4 changed files with 19 additions and 6 deletions

View file

@ -405,6 +405,7 @@ export function editor_function(fn_name: keyof typeof tri_editor, ...args) {
} }
Messaging.addListener("commandline_frame", Messaging.attributeCaller(SELF)) Messaging.addListener("commandline_frame", Messaging.attributeCaller(SELF))
logger.debug("Added commandline_frame message listener")
commandline_state.fns = getCommandlineFns(commandline_state) commandline_state.fns = getCommandlineFns(commandline_state)
Messaging.addListener( Messaging.addListener(
@ -420,3 +421,5 @@ Messaging.addListener(
;(window as any).tri = Object.assign(window.tri || {}, { ;(window as any).tri = Object.assign(window.tri || {}, {
perfObserver: perf.listenForCounters(), perfObserver: perf.listenForCounters(),
}) })
Messaging.messageOwnTab("commandline_frame_ready_to_receive_messages")

View file

@ -102,6 +102,12 @@ class KeyCanceller {
export const canceller = new 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 mustBufferPageKeysForClInput = false
let bufferedPageKeys: string[] = [] let bufferedPageKeys: string[] = []
let bufferingPageKeysBeginTime: number let bufferingPageKeysBeginTime: number
@ -244,12 +250,6 @@ export function acceptKey(keyevent: KeyboardEvent) {
const bufferingDuration = performance.now() - bufferingPageKeysBeginTime; const bufferingDuration = performance.now() - bufferingPageKeysBeginTime;
logger.debug("controller_content mustBufferPageKeysForClInput = " + mustBufferPageKeysForClInput logger.debug("controller_content mustBufferPageKeysForClInput = " + mustBufferPageKeysForClInput
+ " bufferingDuration = " + bufferingDuration + "ms"); + " 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 const isCharacterKey = keyevent.key.length == 1
&& !keyevent.metaKey && !keyevent.ctrlKey && !keyevent.altKey && !keyevent.metaKey; && !keyevent.metaKey && !keyevent.ctrlKey && !keyevent.altKey && !keyevent.metaKey;
if (isCharacterKey) { if (isCharacterKey) {
@ -259,6 +259,14 @@ export function acceptKey(keyevent: KeyboardEvent) {
canceller.push(keyevent) canceller.push(keyevent)
return true 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)) if (!tryBufferingPageKeyForClInput(keyevent))
return generator.next(keyevent) return generator.next(keyevent)
} }

View file

@ -3882,6 +3882,7 @@ export function hidecmdline() {
export function fillcmdline(...strarr: string[]) { export function fillcmdline(...strarr: string[]) {
const str = strarr.join(" ") const str = strarr.join(" ")
showcmdline(false) showcmdline(false)
logger.debug("excmds fillcmdline sending fillcmdline to commandline_frame")
return Messaging.messageOwnTab("commandline_frame", "fillcmdline", [str, true/*trailspace*/, true/*focus*/]) return Messaging.messageOwnTab("commandline_frame", "fillcmdline", [str, true/*trailspace*/, true/*focus*/])
} }

View file

@ -16,6 +16,7 @@ export type TabMessageType =
| "alive" | "alive"
| "tab_changes" | "tab_changes"
| "buffered_page_keys" | "buffered_page_keys"
| "commandline_frame_ready_to_receive_messages"
export type NonTabMessageType = export type NonTabMessageType =
| "owntab_background" | "owntab_background"