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

The ClientEngine.start() promise is not resolved #124

Open
T-vK opened this issue Aug 16, 2019 · 2 comments
Open

The ClientEngine.start() promise is not resolved #124

T-vK opened this issue Aug 16, 2019 · 2 comments

Comments

@T-vK
Copy link

T-vK commented Aug 16, 2019

Can we please change the code of ClientEngine.start so that it resolves once it's ready?

I was extremely confused that this just wouldn't work:

export default class MyGameClientEngine extends ClientEngine {
    // ...
    async start() {
        await super.start(); // Execution stops right here
        this.gameEngine.once('renderer.ready', () => {
            // ...
            document.querySelector('#joinGame').addEventListener('click', (clickEvent) => {
                console.log("CLICK #joinGame");
                if (Utils.isTouchDevice()){
                    this.renderer.enableFullScreen();
                }
                clickEvent.currentTarget.disabled = true;
                this.socket.emit('requestRestart');
            });
            // ...
        });
        // ...
    }
    // ...
}

Or this:

const clientEngine = new MyGameClientEngine(gameEngine, options);
clientEngine.start().then(()=>{
    // This doesn't get executed because `ClientEngine.start` doesn't resolve
    clientEngine.renderer.setupBackground();
    clientEngine.renderer.setRendererSize();
}).catch(console.error); 

It seems like a very bad idea to not resolve the promise immediately. (Not to mention that rejecting with undefined makes debugging a nightmare.)

I'd be willing to make a PR, but I wanted to discuss this first.

@namel
Copy link
Member

namel commented Aug 19, 2019

that would be an excellent PR.

@xaroth8088
Copy link

xaroth8088 commented Oct 6, 2019

For anyone looking for a workaround, you can hack around it by doing the following:

  • In your renderer, maintain a reference to both your GameEngine and ClientEngine subclasses
  • In your renderer's draw method (or similar), add the following code:
    if (this.clientEngineRef.resolveGame) {
        this.gameEngineRef.initialized = true;
    }
  • In your GameEngine subclass, add the following function:
    getPlayerGameOverResult() {
        return this.initialized;
    }

This works because ClientEngine stores the resolve function from ClientEngine::start()'s promise in a class variable that only ever gets called during ClientEngine::step(), and even then only if GameEngine::getPlayerGameOverResult() returns a truthy value.

Hacky? Yes. But, it does cause ClientEngine::start()'s behavior to match the documentation, so... 🤷‍♀

this comment was edited to make the workaround more reliable

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

3 participants