Skip to content

Commit

Permalink
fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
lunaroyster committed Nov 6, 2023
1 parent f5a10a7 commit 6db6ac4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
16 changes: 8 additions & 8 deletions modules/extensions/src/api/experimental/commands.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { extensionPort } from "../../util/comlink";
import { extensionPort, proxy } from "../../util/comlink";
import {
CreateCommand,
CommandProxy,
Expand Down Expand Up @@ -36,24 +36,25 @@ export interface AddCommandArgs {
*/
export function add({ id, contributions, command }: AddCommandArgs) {
if (typeof command === "function") {
let createCommand = async (cmdFnArgs: CommandFnArgs) => {
let createCommand = proxy(async (cmdFnArgs: CommandFnArgs) => {
const cmd = await command(cmdFnArgs);

if (!cmd) {
return null;
}

return isCommandProxy(cmd) ? cmd : Command(cmd);
};
});

extensionPort.experimental.commands.registerCreateCommand(
{ commandId: id, contributions },
createCommand
);
} else {
let createCommand = async () => {
let createCommand = proxy(async () => {
return isCommandProxy(command) ? command : Command(command);
};
});

extensionPort.experimental.commands.registerCreateCommand(
{ commandId: id, contributions },
createCommand
Expand All @@ -75,14 +76,13 @@ export function registerCreate(
data: { commandId: string; contributions: Array<string> },
createCommand: CreateCommand
): void {

extensionPort.experimental.commands.registerCreateCommand(
data,
async (args: CommandFnArgs) => {
const cmd = await createCommand(args)
const cmd = await createCommand(args);

if (!cmd) {
return null
return null;
}

return isCommandProxy(cmd) ? cmd : Command(cmd);
Expand Down
16 changes: 12 additions & 4 deletions modules/extensions/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ export type CommandsFn = (

export type CreateCommand = (
args: CommandFnArgs
) => CommandProxy | Promise<CommandProxy> | CommandArgs | Promise<CommandArgs> | null;
) =>
| CommandProxy
| Promise<CommandProxy>
| CommandArgs
| Promise<CommandArgs>
| null;

export type Run = () => any;

Expand Down Expand Up @@ -174,6 +179,11 @@ export function Command(cmdArgs: CommandArgs): CommandProxy {
// Compute subcommands
let subCmds = await commands(args);

// While we expect commands() to return an array, we don't want to throw an error if it doesn't.
if (!subCmds || !Array.isArray(subCmds)) {
return proxy([]);
}

const commandProxyArray: Array<CommandProxy> = subCmds.map((subCmd) => {
// Subcommands can be either a CommandArgs or a CommandProxy.
// If it's already a wrapped command, just return it.
Expand Down Expand Up @@ -228,7 +238,5 @@ export type CommandProxy =
description?: string;
icon?: string;
};
commands?: (
args: CommandFnArgs
) => Promise<Array<CommandProxy>>;
commands?: (args: CommandFnArgs) => Promise<Array<CommandProxy>>;
} & ProxyMarked & { [CommandSymbol]: true });

0 comments on commit 6db6ac4

Please sign in to comment.