brigadier-ts icon indicating copy to clipboard operation
brigadier-ts copied to clipboard

Allow asynchronous commands

Open haykam821 opened this issue 3 years ago • 4 comments

Currently, the return type of CommandDispatcher#execute is a number, as the Command type requires a number | void return type. As a result, commands cannot be executed asynchronously.

haykam821 avatar Feb 11 '22 16:02 haykam821

Does Mojang/brigadier allow commands to be executed asynchronously?

misode avatar Apr 25 '22 10:04 misode

Asynchronous commands aren't supported in the Java library, but Java does not have promise or async/await support like JavaScript does. Promises would have a less intrusive presence on Brigadier than Java's futures.

haykam821 avatar Apr 25 '22 23:04 haykam821

I'm unsure whether this would be possible, I still want to support synchronous command execution as there are cases where async execution is unpreferable.

When CommandDispatcher#execute runs it should know in advance whether or not it will return a promise or not. One idea I had was a generic parameter for the dispatcher, arguments and commands that extends number | Promise<number>. I may try this later but not sure it's going to work.

Kind of similar to https://github.com/Mojang/brigadier/pull/62 I realize, maybe I can take this as inspiration

misode avatar Apr 26 '22 00:04 misode

Although such an approach wouldn't be entirely backwards compatible, I think the Command type could include promises, and the CommandDispatcher#execute method could upgrade the return type to a promise if any individual command returned a promise. As long as the command tree does not contain any commands returning promises, the CommandDispatcher#execute method will never return a promise.

Alternatively, your approach could work, but would require a fair bit of refactoring to pass the type parameter down from the command source to nodes.

I hope asynchronous commands become possible in the future no matter what approach is taken, because promises unlock to ability to do particularly neat things that aren't easily possible synchronously.

haykam821 avatar Oct 04 '23 16:10 haykam821