flitbit / diff

Javascript utility for calculating deep difference, capturing changes, and applying changes across objects; for nodejs and the browser.
MIT License
2.99k stars 213 forks source link

How do I actually apply a diff "later"? #162

Open Pomax opened 5 years ago

Pomax commented 5 years ago

I can't seem to figure out how to apply a diff generated by the diff function if all I have is a target and a change object. For instance, in a client/server setup:

atServer() {
    let patch = diff(prevState, newState);
    client.sendPatch(patch);
}

and

client.onPatch(patch) {
    let patched = applyDiff(this.state, patch); // <- what is this call?
    this.processUpdatedState(patched);
}

What is the call to actually apply a diff to an object? Not "while generating the diff" using applyChange, and not "while having a direct reference to the original object" because then what's the point of diffing at all (you already have the result).

There's an applyDiff function but its signature is applyDiff(target, source, filter) which makes no sense: is source supposed to be the diff object?

emmanuelgomez commented 5 years ago

@Pomax I had the same problem and after a lot of work I found a way to do it with this package. The examples of this repo explain how to implement it. Greetings.

https://github.com/flitbit/diff/blob/e2b5b86add402ae4373ab025ee7e9a98e3424023/examples/capture_change_apply_elsewhere.js#L24-L30

I can't seem to figure out how to apply a diff generated by the diff function if all I have is a target and a change object. For instance, in a client/server setup:

atServer() {
    let patch = diff(prevState, newState);
    client.sendPatch(patch);
}

and

client.onPatch(patch) {
    let patched = applyDiff(this.state, patch); // <- what is this call?
    this.processUpdatedState(patched);
}

What is the call to actually apply a diff to an object? Not "while generating the diff" using applyChange, and not "while having a direct reference to the original object" because then what's the point of diffing at all (you already have the result).

There's an applyDiff function but its signature is applyDiff(target, source, filter) which makes no sense: is source supposed to be the diff object?

Pomax commented 5 years ago

I went the other way and am now using rfc6902 for the creating of diffs (server side), and jsonpatch to apply them (client side).