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

Save references to instances of extensions #71

Closed pnb closed 2 years ago

pnb commented 2 years ago

I wrote an extension that has some of its own state, which I wanted to access. However, it seems like Chessboard.js does not store references to the instances of extensions it creates with new extensionData.class(this, extensionData.props), so it is not possible to access my extension's data. Or, at least I couldn't figure out a way without doing something a bit hacky, like passing a callback to the extension props that would allow it to send a reference to itself to the outside world.

I noticed that there was an unused this.extensions = [] variable in the Chessboard constructor, so I feel like maybe that is the intended place to store those references. This pull request does so.

It also seems like there is a very small chance that extensions could get garbage collected on low-memory machines, if there is no saved reference to them. This pull request should also prevent that potential issue.

shaack commented 2 years ago

Thanks. The list this.extensions was actually meant to store the references. I merged your pull request and published it on npm as v4.3.8.