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

Run multiple commands at the same time #3

Open
oskarer opened this issue Nov 25, 2020 · 3 comments
Open

Run multiple commands at the same time #3

oskarer opened this issue Nov 25, 2020 · 3 comments
Labels
enhancement New feature or request

Comments

@oskarer
Copy link

oskarer commented Nov 25, 2020

First of all, thank you for this library! It has been a blast so far 🥇

I have a use case where I want to dispatch two (or more) commands after a command finished. They're all promises.

Example;

export class OnJoinCommand extends Command<YourState, { sessionId: string }> {
  execute({ sessionId }) {
    this.state.players[sessionId] = new Player();
    return [
      new FirstCommand().setPayload({ sessionId }),
      new SecondCommand().setPayload({ sessionId })
    ];
  }
}

In that case SecondCommand would execute after FirstCommand finished, how would I execute both at the same time?

@oskarer
Copy link
Author

oskarer commented Nov 25, 2020

#4 fits my use case but I guess supporting both async and sync execution would be good.

@endel
Copy link
Member

endel commented Nov 25, 2020

Hi @oskarer 👋 glad you're using this and thanks for providing the workaround!

I agree it would be nice to provide a way to either run them at once or sequentially. There are so many ways to achieve that, I'm not sure which one would be elegant/intuitive and minimal enough though.

Maybe we could have an AsyncCommand that you extend from? Every Command could be async, but maybe only AsyncCommand's do not await for the response, and just moves on...

import { AsyncCommand } from "@colyseus/command";

export class FirstCommand extends AsyncCommand<YourState, { sessionId: string }> {
  async execute() {
  }
}

export class SecondCommand extends AsyncCommand<YourState, { sessionId: string }> {
  async execute() {
  }
}

What do you think?

@oskarer
Copy link
Author

oskarer commented Nov 26, 2020

@endel That sounds like an elegant solution to me! 👍

@endel endel added the enhancement New feature or request label Nov 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants