shaack / cm-chessboard

A JavaScript chessboard without dependencies. Rendered in SVG, coded in ES6. Views FEN, handles move input, animated, responsive, expandable
MIT License
216 stars 68 forks source link

Remove Asynchronous Logic #50

Closed LandonSchropp closed 3 years ago

LandonSchropp commented 3 years ago

This PR turned out to be a bit bigger than we discussed, but I think the additional work was helpful.

Once I removed the debounce functions, it turns out that the initialization function was no longer needed because none of the code that was being initialized was asynchronous. I was also running into an issue with setOrientation once I made ChessboardView#redraw synchronous, and removing the initialization promise fixed it. This in turn meant that many of the functions that relied on initialization could also be made synchronous.

The end result of this is the only asynchronous function in Chessboard.js is setPosition. I had to refactor the promise logic a little in this function once initialization was removed, but I think the changes simplify things.

I'm especially pleased with how the redraw function turned out after the refactor:

redraw() {
    this.drawBoard()
    this.drawCoordinates()
    this.drawMarkers()
    this.setCursor()
    this.drawPieces(this.chessboard.state.squares)
}

Finally, I split the changes up into several commits. This should make reviewing the changes much easier, as you can see what happened at each one.

Please let me know if there's anything else you'd like to see here. Thanks!

shaack commented 3 years ago

I testet it and it seems to work better than ever. The code is now cleaner and better understandable and better useable. Thanks for your great work, I will merge it now. I also think about removing the debounce when resizing, because it makes a better UI feeling if the board directly responds to resizing. Thanks again.

LandonSchropp commented 3 years ago

Thank you!