Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[commands] provide command args directly #107

Merged
merged 4 commits into from
Nov 6, 2023

Conversation

lunaroyster
Copy link
Contributor

@lunaroyster lunaroyster commented Nov 6, 2023

Why?

Currently, we make you wrap every single command, even nested ones. The invocation looks like

await replit.experimental.commands.add(
  { id: "hey", contributions: ["commandbar"] },
  Command({
    label: "hey",
    description: "hey commands",
    commands: () => {
      return [
        Command({
          label: "hello",
          description: "says hello",
          run: () => console.log("hello"),
        }),
        Command({
          label: "hola",
          description: "says hola",
          run: () => console.log("hola"),
        }),
      ];
    },
  }),
);

It would be nice to not have to wrap things in Command(). The client library can simply do it for you.

await replit.experimental.commands.add(
  { id: "hey", contributions: ["commandbar"] },
  {
    label: "hey",
    description: "hey commands",
    commands: () => {
      return [
        {
          label: "hello",
          description: "says hello",
          run: () => console.log("hello"),
        },
        {
          label: "hola",
          description: "says hola",
          run: () => console.log("hola"),
        },
      ];
    },
  },
);

I didn't remove Command() because I think it's actually a good pattern for composing larger commands. Also, backwards compatibility!

let hola = Command({
  label: "hola",
  description: "says hola",
  run: () => console.log("hola"),
});

let hello = Command({
  label: "hello",
  description: "says hello",
  run: () => console.log("hello"),
});

await replit.experimental.commands.add(
  { id: "hey", contributions: ["commandbar"] },
  {
    label: "hey",
    description: "hey commands",
    commands: () => {
      return [hola, hello];
    },
  },
);

What changed

  • Now the add, and the old register / registerCreate methods all accept raw objects in addition to the wrapped Command(...). When you don't wrap your command objects, we do it for you
  • To accomplish this, we attach a {[CommandSymbol]: true} once we wrap, just to make sure we're not doing it twice
  • Beyond that, not a lot has changed. We had to update a bunch of types to reflect this new world

Test plan

  • clone this repo as a repl
  • hit run
  • load locally via extensions devtools
  • now open the chrome dev tools console, and paste in the second code snippet. you should now see a command called hey in the commandbar, which returns hello and hola

@lunaroyster
Copy link
Contributor Author

lunaroyster commented Nov 6, 2023

Current dependencies on/for this PR:

This comment was autogenerated by Freephite.

@lunaroyster lunaroyster force-pushed the 11-07-_commands_provide_command_args_directly branch from 3a99e65 to c217089 Compare November 6, 2023 21:07
Base automatically changed from 11-06-_commands_add_contribution_types to main November 6, 2023 21:09
@lunaroyster lunaroyster force-pushed the 11-07-_commands_provide_command_args_directly branch from c217089 to 51b1bd7 Compare November 6, 2023 21:10
@lafkpages
Copy link
Contributor

Does this mean we no longer have to wrap commands in replit.proxy()?

@lunaroyster
Copy link
Contributor Author

lunaroyster commented Nov 6, 2023

Does this mean we no longer have to wrap commands in replit.proxy()?

you wouldn't have had to wrap them in proxy even before this version. the first example here is the old invocation, which contains a Command() call, that does it for you

But after this merges, you don't even have to wrap them in Command(), we do that for you too

The docs will make it clearer

@lunaroyster
Copy link
Contributor Author

@masad-frost this handles the remaining piece of feedback you had about the commands API

i'm merging this for now so i can go publish some updated docs, but would still appreciate a review, both on the code and the final syntax :)

@lunaroyster lunaroyster merged commit ed58e63 into main Nov 6, 2023
@lunaroyster lunaroyster deleted the 11-07-_commands_provide_command_args_directly branch November 6, 2023 23:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants