Closed alshakero closed 7 years ago
@warpech this is not the primary but a fast solution. It's much much faster than deep cloning. It really took some deep thinking and crazy focus to visualize the whole thing and come up with this.
@tomalec any comments on this? I fixed https://github.com/Palindrom/Palindrom/issues/73 and would like to release Palindrom 3.0.2. Would be cool to release this to be bundled in Palindrom 3.0.2 to save a version number and Level1 PR hassle.
@tomalec thank you so much. Looks much better now.
Is this good to go?
No comments from me at least, I am not sure if @tomalec has his points answered though?
I am not sure if @tomalec has his points answered though?
To the best of my knowledge, they all are.
I did move the comment above the function call, I don't think we should move it above the function itself. It's for the line, not the function. https://github.com/Palindrom/JSONPatcherProxy/blob/40350a6f7aa36dff4e361f4686fb48be1c2b1809/src/jsonpatcherproxy.js#L10-L14
I did move the comment above the function call, I donn't think we should move it above the function itself. It's for the line, not the function.
I'm talking about banner:
'use strict';
/**
* A helper function that calls Reflect.set.
* It is utilized to re-populate path history map after every `Reflect.set` call.
* @param {JSONPatcherProxy} instance JSONPatcherProxy instance
* @param {Object} target target object
* @param {string} key affected property name
* @param {any} newValue the new set value
*/
function ownReflectSet(instance, target, key, newValue) {
const result = Reflect.set(target, key, newValue);
/* we traverse the new (post-apply) object and update proxies paths accordingly */
instance._resetCachedProxiesPaths(instance.cachedProxy, '');
return result;
}
/*!
* https://github.com/PuppetJS/JSONPatcherProxy
* JSONPatcherProxy version: 0.0.5
* (c) 2017 Starcounter
* MIT license
*/
/** Class representing a JS Object observer */
const JSONPatcherProxy = (function() {
to
/*!
* https://github.com/PuppetJS/JSONPatcherProxy
* JSONPatcherProxy version: 0.0.5
* (c) 2017 Starcounter
* MIT license
*/
'use strict';
/**
* A helper function that calls Reflect.set.
* It is utilized to re-populate path history map after every `Reflect.set` call.
* @param {JSONPatcherProxy} instance JSONPatcherProxy instance
* @param {Object} target target object
* @param {string} key affected property name
* @param {any} newValue the new set value
*/
function ownReflectSet(instance, target, key, newValue) {
const result = Reflect.set(target, key, newValue);
/* we traverse the new (post-apply) object and update proxies paths accordingly */
instance._resetCachedProxiesPaths(instance.cachedProxy, '');
return result;
}
/** Class representing a JS Object observer */
const JSONPatcherProxy = (function() {
Also you made ownReflectSet
totally global and I think that is not necesarry.
Legit point.
Great, thanks for such a deep review that I simply could not make.
I just thought it's limitation we plan to remove in long term. Also, such change could be done by palindrom, as well.
That's nice to have, I agree, but right now we are fixing a high priority issue, so we should fix, add tests, make a note about the limitations and move forward.
Great, thanks for such a deep review
Totally agree, the code looks much better after Tomek's reviews.
I merged without updating docs, I'll bundle Palindrom, PR to Level1, and then PR for docs.
Explanation:
How do we proxify objects?
tree
object.originalObject
properties andproxify
them one by one. Copy each proxified prop totree
.proxify
function would passpath
to ES6 proxy traps (set
anddeleteProperty
). Because it knows where is it in the object tree throughout traversing.All done, now we have a proxified tree with a set of proxies that have
set
functions cachingpath
inside them.Looks fine.
Until
The project tree is mutated in a way that doesn't affect the deep proxy itself but changes its path. Like:
Now tree is
[{value: 2}]
, and2
thinks its path is/1/value
.This PR makes JP keep track of all proxies' paths and updates them if needed after every mutation.
Fixes: https://github.com/Palindrom/JSONPatcherProxy/issues/9 and more.