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

Recreation from diff #10

Open
FluentCoding opened this issue Nov 8, 2021 · 8 comments
Open

Recreation from diff #10

FluentCoding opened this issue Nov 8, 2021 · 8 comments
Assignees

Comments

@FluentCoding
Copy link

Hey!

I'm currently considering to use microdiff for my project, however I also have to be able to recreate an object from the diff and the source object (basically so that im able to send smaller-sized updates to a client state via a websocket connection), but it seems like microdiff is not offering any method for this. I will implement this in my project but I would like to know if this is in the ToDo.

@AsyncBanana
Copy link
Owner

Yeah, I plan to add patching soon. I can’t give you an exact ETA, but it will probably be a week or two. I will give you an update when I release it.

@AsyncBanana AsyncBanana self-assigned this Nov 8, 2021
@FluentCoding
Copy link
Author

Alright, thanks! I'm down to help you at this, you can hit me up on discord (FluentCoding#3314) if you want to discuss the API/implementation.

@gorkermann
Copy link

this might be the same as what you're thinking for patching, but is it possible to lump together CHANGE events with similar paths? So

{
  path: [ 'states', 0, 'jsonlist', 61, 'offsetP', 'x' ],
  type: 'CHANGE',
  value: 569.73
},
{
  path: [ 'states', 0, 'jsonlist', 61, 'offsetP', 'y' ],
  type: 'CHANGE',
  value: 266.862
},

would become

{
  path: [ 'states', 0, 'jsonlist', 61, 'offsetP' ],
  type: 'CHANGE',
  value: { x: 569.73, y: 266.862 }
},

@FluentCoding
Copy link
Author

I think you could do this but at the cost of a bit of performance as you would have to look up the equal path parents and then combine them.

@FluentCoding
Copy link
Author

Oh, also, related to this issue, here is the PR that I posted about 11 days ago: #13

@brainthinks
Copy link
Contributor

brainthinks commented Jan 12, 2022

If you have lodash in your project, here is how I'm currently experimenting with patch functionality:

function patch (target: any, diff: any[]): any {
  target = cloneDeep(target);

  for (let i = 0; i < diff.length; i++) {
    const {
      type,
      path,
      value,
    } = diff[i];

    switch (type) {
      case 'CREATE': {
        set(target, path, value);
        break;
      }
      case 'CHANGE': {
        set(target, path, value);
        break;
      }
      case 'REMOVE': {
        // Note this does not actually remove the entry
        set(target, path, undefined);
        break;
      }
      default: {
        throw new Error(`unknown diff entry type: ${type}`);
      }
    }
  }

  return target;
}

Hopefully this helps someone waiting for the patch functionality. It would be less trivial to implement without lodash, but doable. I'm not sure how REMOVE would work - right now I'm just setting it to undefined, but that isn't the same as removal since the ownEnumerableProperty still exists.

@AsyncBanana AsyncBanana pinned this issue Jan 16, 2022
@steveruizok
Copy link

Would lodash.unset work here for the REMOVE?

@AsyncBanana
Copy link
Owner

Micropatch is where FluentCoding's implementation ended up. Currently, you can use it directly from GitHub, and I am working on publishing it to NPM.

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

5 participants