question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I’d like to add to #1412 and #1511 to propose a type-safe approach.

Problem:

Commands are currently heavily error-prone:

  1. You can misspell the type and it will silently never trigger in your app
  2. You can misinterpret the types

Part of this problem is caused by the ability to listen to multiple commands at once.

Proposal

Encourage type safety within Lexical core and user-created commands. Make it so that you can only listen to a specific command at once and this command is strictly type safe and provided by Lexical core.

execCommand<T>(type: Command<T>, payload: T): void;
registerCommand<T>(type: Command<T>, payload: (T) => void): void;

class Command<T> {
  type: string;
  constructor(type: string) {
    this.type = type;
  }
}
const formatTextCommand = new Command<string>('formatText');
new LexicalEditor().execCommand(formatTextCommand, 'bold');
new LexicalEditor().registerCommand(formatTextCommand, (format) => {
  // bold
});

The generic type above, while seemingly unused, enables us to offer type safety for the payload.

[Optionally] We can do like transforms where we check for the command reference to ensure that we’re strictly referring to the same command, in which case we don’t even need the type.

class Command<T> {}
const formatTextCommand = new Command<string>();

Trade-offs

(+) It’s type safe (-) It’s more costly, we have to provide an instance for every single command (-) It more verbose, especially for some Lexical core/React. For most internal/real-life usages it should be equivalent though.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
trueadmcommented, Mar 23, 2022

We can’t use enums unfortunately, open source Lexical should work without them (we strip out types) and also I couldn’t get Flow enums working at all!

1reaction
amyworrallcommented, Mar 23, 2022

On iOS we do this:

public struct CommandType: RawRepresentable {
  public var rawValue: String
  public init(rawValue: String) {
    self.rawValue = rawValue
  }
  static let selectionChange = CommandType(rawValue: "selectionChange")
  static let click = CommandType(rawValue: "click")
  static let deleteCharacter = CommandType(rawValue: "deleteCharacter")
  static let insertLineBreak = CommandType(rawValue: "insertLineBreak")
  static let insertParagraph = CommandType(rawValue: "insertParagraph")
  ...
}

Then anyone can add their own commands by doing:

extension struct CommandType {
  static let myCommand = CommandType(rawValue: "myCommand")
}

and to use them, the type is CommandType, and the IDE will happily autocomplete .deleteCharacter or whatever.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typing Commands
Typing Multiple Commands and Long Commands. You can type more than one command on a single line. Simply place a semicolon (;) between...
Read more >
Typing the command - IBM
Type in the command. As you type each character, the corresponding character appears in the entry area, and the cursor advances to the...
Read more >
type | Microsoft Learn
In the Windows Command shell, type is a built in command which displays the contents of a text file. Use the type command...
Read more >
Typing Unix Commands
To run a Unix command (or any program, for that matter), you normally must type the name of the command/program file followed by...
Read more >
TYPE (DOS command) - Wikipedia
In computing, type is a command in various command-line interpreters (shells) such as COMMAND.COM , cmd.exe , 4DOS/4NT and Windows PowerShell used to ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found