Skip to content

Commit

Permalink
handle null case
Browse files Browse the repository at this point in the history
  • Loading branch information
lunaroyster committed Nov 6, 2023
1 parent 51b1bd7 commit f5a10a7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
15 changes: 14 additions & 1 deletion modules/extensions/src/api/experimental/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export function add({ id, contributions, command }: AddCommandArgs) {
let createCommand = async (cmdFnArgs: CommandFnArgs) => {
const cmd = await command(cmdFnArgs);

if (!cmd) {
return null;
}

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

Expand Down Expand Up @@ -71,8 +75,17 @@ export function registerCreate(
data: { commandId: string; contributions: Array<string> },
createCommand: CreateCommand
): void {

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

if (!cmd) {
return null
}

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

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

export type Run = () => any;

Expand Down Expand Up @@ -214,25 +214,21 @@ export function Command(cmdArgs: CommandArgs): CommandProxy {
export type CommandProxy =
| ({
data: {
type: string;
type: "action";
label: string;
description?: string;
icon?: string;
};
run?: Run;
commands?: (
args: CommandFnArgs
) => Promise<Array<CommandProxy | CommandArgs>>;
} & ProxyMarked & { [CommandSymbol]: true })
| ({
data: {
type: string;
type: "context";
label: string;
description?: string;
icon?: string;
};
run?: Run;
commands?: (
args: CommandFnArgs
) => Promise<Array<CommandProxy | CommandArgs>>;
} & ProxyMarked & { [CommandSymbol]: true })[];
) => Promise<Array<CommandProxy>>;
} & ProxyMarked & { [CommandSymbol]: true });
2 changes: 1 addition & 1 deletion modules/extensions/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export type ExperimentalAPI = {
commandId: string;
contributions: Array<string>;
},
create: CreateCommand
create: (createArgs: CommandFnArgs) => Promise<CommandProxy | null>
) => void;
};
};
Expand Down

0 comments on commit f5a10a7

Please sign in to comment.