diff --git a/frontend/src/ts/commandline/commandline.ts b/frontend/src/ts/commandline/commandline.ts index 6e7bd402421b..00d2581505f3 100644 --- a/frontend/src/ts/commandline/commandline.ts +++ b/frontend/src/ts/commandline/commandline.ts @@ -60,6 +60,7 @@ export function show( inputValue = ""; activeIndex = 0; mode = "search"; + cachedSingleSubgroup = null; inputModeParams = { command: null, placeholder: null, @@ -97,7 +98,6 @@ export function show( activeCommand = null; Focus.set(false); CommandlineLists.setStackToDefault(); - await beforeList(); updateInput(); await filterSubgroup(); await showCommands(); @@ -247,13 +247,19 @@ function hideCommands(): void { element.innerHTML = ""; } +let cachedSingleSubgroup: MonkeyTypes.CommandsSubgroup | null = null; + async function getSubgroup(): Promise { if (subgroupOverride !== null) { return subgroupOverride; } if (usingSingleList) { - return CommandlineLists.getSingleSubgroup(); + if (cachedSingleSubgroup === null) { + cachedSingleSubgroup = await CommandlineLists.getSingleSubgroup(); + } else { + return cachedSingleSubgroup; + } } return CommandlineLists.getTopOfStack(); @@ -263,10 +269,6 @@ async function getList(): Promise { return (await getSubgroup()).list; } -async function beforeList(): Promise { - (await getSubgroup()).beforeList?.(); -} - async function showCommands(): Promise { const element = document.querySelector("#commandLine .suggestions"); if (element === null) { @@ -442,7 +444,6 @@ async function runActiveCommand(): Promise { CommandlineLists.pushToStack( command.subgroup as MonkeyTypes.CommandsSubgroup ); - await beforeList(); updateInput(""); await filterSubgroup(); await showCommands(); @@ -454,7 +455,6 @@ async function runActiveCommand(): Promise { void AnalyticsController.log("usedCommandLine", { command: command.id }); hide(); } else { - await beforeList(); await filterSubgroup(); await showCommands(); await updateActiveCommand(); diff --git a/frontend/src/ts/commandline/lists.ts b/frontend/src/ts/commandline/lists.ts index 6103ee699646..eb13928f3f0a 100644 --- a/frontend/src/ts/commandline/lists.ts +++ b/frontend/src/ts/commandline/lists.ts @@ -553,42 +553,28 @@ export async function getSingleSubgroup(): Promise challengesPromise, ]); - if (singleList) return singleList; - - // const - const singleCommands: MonkeyTypes.Command[] = []; - const beforeListFunctions: (() => void)[] = []; for (const command of commands.list) { const ret = buildSingleListCommands(command); - singleCommands.push(...ret.commands); - beforeListFunctions.push(...ret.beforeListFunctions); + singleCommands.push(...ret); } singleList = { title: "All commands", list: singleCommands, - beforeList: (): void => { - for (const func of beforeListFunctions) { - func(); - } - }, }; return singleList; } -type SingleList = { - commands: MonkeyTypes.Command[]; - beforeListFunctions: (() => void)[]; -}; - function buildSingleListCommands( command: MonkeyTypes.Command, parentCommand?: MonkeyTypes.Command -): SingleList { +): MonkeyTypes.Command[] { const commands: MonkeyTypes.Command[] = []; - const beforeListFunctions: (() => void)[] = []; if (command.subgroup) { + if (command.subgroup.beforeList) { + command.subgroup.beforeList(); + } const currentCommand = { ...command, subgroup: { @@ -596,11 +582,8 @@ function buildSingleListCommands( list: [], }, }; - if (command.subgroup.beforeList) { - beforeListFunctions.push(command.subgroup.beforeList); - } for (const cmd of command.subgroup.list) { - commands.push(...buildSingleListCommands(cmd, currentCommand).commands); + commands.push(...buildSingleListCommands(cmd, currentCommand)); } } else { if (parentCommand) { @@ -644,8 +627,5 @@ function buildSingleListCommands( commands.push(command); } } - return { - commands, - beforeListFunctions, - }; + return commands; }