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

Omit reIndexing of arrays when methods like unshift or splice are used #61

Closed
Baumgaer opened this issue Jul 26, 2020 · 1 comment
Closed

Comments

@Baumgaer
Copy link

Baumgaer commented Jul 26, 2020

When on-change observes an array which is modified with unshift (or other methods which are able to insert or delete items somewhare alse than the end of the array) the handler will be fired for each re-index. similar to #56

Example:

const myObservedArray = onChange([1, 2, 3, 4, 5], (path, newVal, oldVal) => {
    console.log(path, newVal, oldVal);
});
myObservedArray.unshift(6);

The output will be:

path newVal oldVal comment
5 5 undefined because unshift starts first to move every item one index further and overwrites existing values with the beginning at the end
4 4 5
3 3 4
2 2 3
1 1 2
0 6 1 the origin unshift

There should be an option to avoid this behavior and only yield the origin change to the listener function. Maybe you could add an aditional parameter to the function, named "action" or something like that, to be able to determine what happened really.

@DarrenPaulWright
Copy link
Collaborator

I can't replicate your issue. I only see one call to the listener function. I am working on #48 right now though, which will improve performance on specific methods.

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