Description
Hi There,
I've been reading up the documentation recently (https://docs.colyseus.io/best-practices/)
and came across the best-practices part where it is recommended to adopt the Command-Pattern.
In the example given in the docs for "OnJoinCommand.ts" file,
// OnJoinCommand.ts
import { Command } from "@colyseus/command";
import { MyRoom } from './MyRoom'; // this is missing in the example.
export class OnJoinCommand extends Command<MyRoom, {
sessionId: string
}> {
execute({ sessionId }) {
this.state.players[sessionId] = new Player();
}
}
and below is the code given
For MyRoom.ts file:
import { Room } from "colyseus";
import { Dispatcher } from "@colyseus/command";
import { OnJoinCommand } from "./OnJoinCommand";
class MyRoom extends Room<YourState> {
dispatcher = new Dispatcher(this);
onCreate() {
this.setState(new YourState());
}
onJoin(client, options) {
this.dispatcher.dispatch(new OnJoinCommand(), {
sessionId: client.sessionId
});
}
onDispose() {
this.dispatcher.stop();
}
}
MyRoom imports OnJoinCommand , and vice-versa.
It seems the circular-dependency issue is introduced due to the decision of having generic type Room for class Command (Command)
I saw in previous versions of colyseus docs, instead of taking reference of a room, you could pass reference to the room state instead (see: https://0-13-x.docs.colyseus.io/best-practices/command-pattern/)
Which seems like a better way as it would avoid this sort of circular dependency issue when splitting commands into different module.
But I'm not sure why that approach has been changed (?) now. If you could help suggest some way to avoid this issue ?