-
Notifications
You must be signed in to change notification settings - Fork 58
Comparison API #138
Comments
@pottedmeat has been working on this in the compare branch. |
I've come up with what I think is a fairly robust API for object/array patch creation that has some areas of ambiguity that I need to nail down. The first is with the object patch which has change records at each key indicated whether it was added, updated, or deleted. It also provides the old and new value at that key.
Next is the array patch, which stores the indexes at which changes will be made as well as what's been added and removed and whether these changes were relocated within that array. This lets us learn a lot about the changes, find previous/next references, and do intelligent object removal/movement. Values are stored as indexes. This is acceptable for the original array, but the references to the comparison array mean that the comparison array cannot be mutated and it must be passed when performing a patch. This seems problematic. Previous/next references can be computed (either the position before the insertion point or the insertion point plus the number of removed items) but it's not immediately evident.
|
@matt-gadd, @agubler as we discussed, if you could look at the |
I just had an epiphany on this. This is a perfect situation for ES Observables. If |
as @pottedmeat notes the This may be more related to the dojo/stores#4 but there are quite a few JSONPatch libs out there (i've used https://github.com/cujojs/jiff in the past), did we take a look at any? or did we deem it not flexible enough for a generic diff/patch util? On the example failing test: 'can remove item from an array, and change an item': () => {
const myOpts = { identityKey: 'id', compareObjects: true };
const myArray = [
{ id: 1, label: 'foo' },
{ id: 2, label: 'bar' }
];
const myArray2 = [
{ id: 2, label: 'qux' }
];
const myPatch = diff(myArray, myArray2, myOpts);
const myResult = patch(myArray, myPatch, myArray2);
// above throws, as the object to change no longer exists at that array index
assert.equal(myResult, myArray2);
} |
I am open to revisiting this. Also, before Google abandoned it, the specification for Harmony Observe and the related polyfills covered off mutations of objects and arrays in a fairly efficient fashion. I made an attempt quite a while ago to fit a lot of that into a Dojo-like core: https://github.com/kitsonk/core-js/tree/master/observe The test may also be useful: https://github.com/kitsonk/core-js/tree/master/tests/unit/observe |
In regards to JSONPatch I am not opposed to it. The one challenge though that I see with it, for array operations it isn't very efficient. This was a challenge with the Harmony Object.observe as well and the way they solved it was via a If we adopted JSONPatch, but enhanced it to allow splices and potentially registrable change types, is it JSONPatch anymore? |
@maier49 is this relevant to stores anymore? |
Several functional problems have highlighted that
core
could benefit from a centralised API for comparing objects and mutating objects based upon those comparisons. We should have an API that:The text was updated successfully, but these errors were encountered: