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

Changes not detected on internal variables #78

Closed
hpx7 opened this issue Mar 19, 2021 · 1 comment
Closed

Changes not detected on internal variables #78

hpx7 opened this issue Mar 19, 2021 · 1 comment

Comments

@hpx7
Copy link
Contributor

hpx7 commented Mar 19, 2021

I'm using the popular https://github.com/jhlywa/chess.js but can't seem detect changes on mutations of the Chess object

Repro:

import onChange from "on-change";
import { Chess } from "chess.js";

const chess = new Chess();
const watchedChess = onChange(chess, () => {
  console.log("change detected");
});

watchedChess.foo = "bar"; // change detected
watchedChess.move({ from: "e2", to: "e4" }); // no change detected, even though this function mutates internal state

I suspect the issue is related to the internal chess state representation, which is not exposed to library consumers. Do you see any way/workaround to make this work?

@DarrenPaulWright
Copy link
Collaborator

You are correct, they are storing moves in a private var, so on-change can't see those changes. I recommend two possibilities:

  1. Extend chess.js and wrap all mutable methods to trigger a change event.
  2. Add an onChange method to the library that triggers an event whenever a change is detected.

I don't think you would need this library for either of those options.

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

2 participants