diff icon indicating copy to clipboard operation
diff copied to clipboard

How do I actually apply a diff "later"?

Open Pomax opened this issue 5 years ago • 2 comments

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 avatar Jun 30 '19 18:06 Pomax

@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?

emmanuelgomez avatar Jul 11 '19 06:07 emmanuelgomez

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

Pomax avatar Jul 13 '19 19:07 Pomax