Coder Social home page Coder Social logo

Comments (10)

alshakero avatar alshakero commented on June 3, 2024 1

Why deleteProperty trap is not able to catch it correctly?

Because that's what the JS engine actually does. If you watch the traps in slow-motion (so to say) you see the engine replacing (shifting left) array elements one by one starting from the spliced item until it reaches the end then it removes the last element, which only then triggers the deleteProperty trap.

And this makes sense. You want arr[5] value to be different before and after you do splice(2,1). Right? You want the elements shifted.

from json-patch.

 avatar commented on June 3, 2024

👍 Same issue here.

from json-patch.

tomalec avatar tomalec commented on June 3, 2024

@Pavek Thanks for reporting, and nice test case :) It seems it comes from limitations of Object.observe that we use. I will try to figure something out, maybe try how Array.observe affects performance, and how hard is it to shim.
@warpech have we ever consider Array.observe?

from json-patch.

tomalec avatar tomalec commented on June 3, 2024

@Pavek, @winduptoy I have created branch for this fiddling with Array.observe.

@Pavek test case passes with native (Object && Array).observe, I have also added test case with full featured .splice removes and adds many items. Please, check if it works fine for you.
If so, I'll move on with performance and shimming work.

from json-patch.

tomalec avatar tomalec commented on June 3, 2024

It seems working without native Array.observe is much more complicated. I created algorithm to generate nicer patches, also form manual generate calls (.compare, .generate, shimmed observer): https://github.com/tomalec/JSON-Patch/tree/issues/65_ArrayObserve_shim
and it works nice with native observers.

The problem comes with shim, as we keep mirror as deep clone so there is no way to check that
for

obj = { items: [{name:"obj1"}, {name:"obj2"}, {name:"obj3"}]};

new obj.items[1] is equal to any of old obj.items[?].

@warpech any ideas?
Maybe we could serve better and optimized patches with native support, and leave it as it was for shimmed? - then, naturally, we would not serve different patches for same scenario for shimmed and native observers; equivalent but not same.

from json-patch.

tomalec avatar tomalec commented on June 3, 2024

Related SO issue http://stackoverflow.com/questions/29414710/is-there-any-way-to-preserve-the-order-while-generating-patch-for-json-files

from json-patch.

warpech avatar warpech commented on June 3, 2024

We could keep a shallow clone of arrays to check that. This way you could compare arr.indexOf(obj) with arrShallowClone.indexOf(obj)

from json-patch.

tomalec avatar tomalec commented on June 3, 2024

The question is where to keep those clones? as currently we keep only one deepClone of everything as Mirror

from json-patch.

alshakero avatar alshakero commented on June 3, 2024

Please see #204 (comment)

I don't think we'll implement a fix for this. Because I think our observation efforts will be mainly focused on JSONPatcherProxy.

Closing but feel free to reopen.

from json-patch.

tomalec avatar tomalec commented on June 3, 2024

Why deleteProperty trap is not able to catch it correctly?

const JSONPatcherProxy = require('jsonpatcherproxy');

var myObj = { items: ["a", "b", "c"]};
var jsonPatcherProxy = new JSONPatcherProxy( myObj );
var observedObject = jsonPatcherProxy.observe(true);
myObj.items.splice(1, 1); // <<-- removing second item in-place
var patches = jsonPatcherProxy.generate();

https://runkit.com/tomalec/5b151a476d7288001244bd25

from json-patch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.