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

Map.set callback not triggered with correct arguments #82

Closed
saxelsen opened this issue Jul 14, 2021 · 1 comment
Closed

Map.set callback not triggered with correct arguments #82

saxelsen opened this issue Jul 14, 2021 · 1 comment

Comments

@saxelsen
Copy link

saxelsen commented Jul 14, 2021

Version 3.0.2

When the object is a Map, calls to the map.set() method triggers the callback with 3/4 incorrect arguments (path, value, previousValue):

const _state = new Map();
const state = onChange(_state, (path, value, previousValue, applyData) => {
  console.log(`Changed ${path} from ${JSON.stringify(previousValue)} to ${JSON.stringify(value)}`);
});

state.set("a", "hello, world");

Produces the following output: "Changed from {} to {}".


I can achieve part of the intended behaviour with applyData.args[0] for the path and applyData.args[1] for the value, but this seems like it is pretty ripe for unforeseen errors.

Any thoughts?

@DarrenPaulWright
Copy link
Collaborator

First, JSON.stringify on a map will always produce "{}", no matter the contents. I have tests verifying that value and previous value are correct. In your console.log, if you instead used value.get('a') you would see the 'hello, world' on value.

Second, the path. Since you can't access the contents of a map with dot syntax, in your example _state.a is undefined, it was determined to just show the path to the map, not within it. Then you can extrapolate from the value, previousValue, and/or applyData what changed. In this case the applyData tells you exactly what changed.

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