Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Pomax opened this issue Jun 30, 2019 · 2 comments
Open

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

Pomax opened this issue Jun 30, 2019 · 2 comments

Comments

@Pomax
Copy link

Pomax commented Jun 30, 2019

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
Copy link

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

// record the differences between source and comparand
var changes = diff(source, comparand);
// apply the changes to the source
changes.forEach(function (change) {
diff.applyChange(source, true, change);
});

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
Copy link
Author

Pomax commented Jul 13, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants