-
Notifications
You must be signed in to change notification settings - Fork 55
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
Report a single high-level mutation when adding/removing non-last array elements #16
Comments
There was another user who had a similar enhancement request a couple weeks back -- essentially to provide a more user-friendly and concise description of what mutation just occurred. Unfortunately there are a few things that make this difficult if not impossible without some major sacrifices to other parts the library functionality. As far as I know, there we two ways you could accomplish the desired behavior. You could either:
The first option isn't really viable because wrapping Array mutation methods is just asking for a conflict with other libraries and the ES5 Proxy polyfill already wraps the array mutation methods (i.e., things would get complicated and slow if we double wrapped the Array mutation methods). The second option seems possible, but it will have it's own challenges. I'm not certain if all browsers implementation of |
The issue you caught is a very interesting one. It's caused by an oddity with how the For example, if you perform a I'm going ahead with a little workaround enhancement/bug fix to resolve this issue so @christianliebel Thank you for reporting these issues! These are some excellent catches. |
@christianliebel I just pushed out the fix for the The enhancement you've requested will take some time to finish, but I'm hoping in the meantime that accurate reporting on Array |
@ElliotNB Awesome, thanks a lot. I’ll try detecting array additions/deletions from the stack of changes, but I’d agree that an opt-in, library-provided method would be preferable.
You’re welcome! 🤘 Thanks for your fast help and support. |
@christianliebel I am also trying to get additions/deletions from changes in proxyArray. Any luck in detecting additiuons/deletions? |
@joharzmn Yes, I’ve enabled the |
@christianliebel Have you noticed any differences between how Chrome, Firefox, Edge, etc report I've been meaning to get this enhancement built out... |
@ElliotNB No, I haven’t specifically tested the Proxy behavior on different browsers. But in the context of the application, the following workaround seems to work across browsers. @joharzmn This is my workaround: /**
* This is a workaround that tries to squash a chain of change events to a single high-level mutation event.
*
* Example: Deleting an array entry
* const arr = [0, 1, 2]
* arr.splice(0, 1); // arr = [0, 1]
*
* Leads to this chain of events:
* { type: 'update', property: '1', newValue: 2, oldValue: 1 }
* { type: 'delete', property: '2', newValue: null, oldValue: 2 }
* { type: 'update', property: 'length', newValue: 2, oldValue: 3 }
*
* However, the application is only interested in high-level events:
* { type: 'delete', property: '1', newValue: null, oldValue: 1 }
*
* @param changes
*/
export function squashDeleteMutationChanges(changes) {
let activeArray = null;
const processedChanges = [];
function squashChanges(end) {
processedChanges.push({...changes[end], type: 'delete', newValue: null});
}
changes.reverse();
changes.forEach((change, index) => {
const {type, property, target, newValue, previousValue} = change;
if (type === 'update' && property === 'length' && Array.isArray(target) && newValue < previousValue) {
if (activeArray) {
squashChanges(index - 1);
}
activeArray = {reference: target, start: index};
} else if (activeArray && !isNaN(property) && target === activeArray.reference) {
activeArray.end = index;
} else {
activeArray = null;
processedChanges.push(change);
}
});
if (activeArray) {
squashChanges(activeArray.end);
}
processedChanges.reverse();
return processedChanges;
} |
Adding/removing non-last array elements leads to a chain of update changes, before the actual delete/add change is reported on the last array element. I can understand this behaviour from the proxy’s perspective (and I know that you try to be close to the Proxy behaviour), however as a developer I’d prefer to get the high-level perspective of which array element was actually affected.
Library version tested against: 0.0.8
Repro:
Result:
Expected:
The library should either report the actual affected index for delete/add changes on arrays or introduce a new (additional) change type
addElement
/deleteElement
that reports the actual index. Interestingly, there seems to be no change fired on thelength
property when you add items to an array (I tried it with bothsplice
andpush
).The text was updated successfully, but these errors were encountered: