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

New API with command context #22

Open
Webbrother opened this issue Apr 16, 2023 · 1 comment
Open

New API with command context #22

Webbrother opened this issue Apr 16, 2023 · 1 comment

Comments

@Webbrother
Copy link
Owner

At the moment there is no way to pass command context which could be necessary for such a command like attachment to pass Promise to the command.

Current implementation: commandController.executeCommand('myCommandName');

1st Idea

The idea: commandController.executeCommand('myCommandName', commandContext);

To implement such functionality it is necessary

  • update the command type ICommand make it generic something like ICommand<CommandContext>
  • update CommandController so the instance could understand ICommand generic type
  const commandWithoutContext: ICommand = {...};
  const commandWithContext: ICommand<number> = {...};

  const { ref, commandController } = useTextAreaMarkdownEditor({
    commandMap: {
      commandName1: commandWithoutContext,
      commandName2: commandWithContext,
    },
  });

  commandController.executeCommand('commandName1'); // No error
  commandController.executeCommand('commandName1', 42); // Error: only 1 argument expected
  
  commandController.executeCommand('commandName2'); // Error: only 2 arguments expected
  commandController.executeCommand('commandName2', 42); // No error

2nd Idea

Current API can be changed this way:

  class CommandWithoutContext extends BaseCommand {...};
  class CommandWithContext extends BaseCommand<number> {...};

  const { ref, commands {
     commandName1,
     commandName2,
  }} = useTextAreaMarkdownEditor({
    commandMap: {
      commandName1: CommandWithoutContext,
      commandName2: CommandWithContext,
    },
  });

  commandName1(); // No error
  commandName1(42); // Error: 0 arguments expected
  
  commandName2(); // Error: 1 argument expected
  commandName2(42); // No error

Such changes require complex types inference logic wich I didn't implement at the moment.
If anyone has code examples of such type inference logic please share them here. Any idea will be highly appreciated.

@Webbrother
Copy link
Owner Author

Webbrother commented Apr 17, 2023

Play with idea 1

Play with idea 2

Patterns that probably can be used

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

No branches or pull requests

1 participant