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

Circular Dependency issue in the Command Pattern Example #151

Open
keshav2010 opened this issue Jan 14, 2024 · 0 comments
Open

Circular Dependency issue in the Command Pattern Example #151

keshav2010 opened this issue Jan 14, 2024 · 0 comments

Comments

@keshav2010
Copy link

keshav2010 commented Jan 14, 2024

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 ?

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