Skip to content

Circular Dependency issue in the Command Pattern ExampleΒ #151

Open
@keshav2010

Description

@keshav2010

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 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions