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

Input mutation #16

Open
chhatch opened this issue May 5, 2022 · 3 comments
Open

Input mutation #16

chhatch opened this issue May 5, 2022 · 3 comments

Comments

@chhatch
Copy link
Collaborator

chhatch commented May 5, 2022

When a rule set is executed on an object that object gets mutated.

const fishRhyme = ruleFactory([
  { if: 'fish == "oneFish"', then: 'fish = "twoFish"' },
  { if: 'fish == "redFish"', then: 'fish = "blueFish"' },
]);
const fish = { fish: "oneFish" };
fishRhyme(fish); 
console.log(fish);
// { fish: 'twoFish' }

I found // input = cloneDeep(input); at index.ts:49, so it looks like Lodash was being used to deep clone the input object.

@justsml
Copy link
Contributor

justsml commented May 24, 2022

My initial benchmarks suffered greatly if cloneDeep was the default.
I figured I could leave it up to the caller/user? Should there be a warning in the function's JSDoc?

@chhatch
Copy link
Collaborator Author

chhatch commented May 24, 2022

I think that's a fine solution as long as it's clear in the docs.

@justsml
Copy link
Contributor

justsml commented Jul 15, 2022

There's a few possible options around this that I want to consider:

  1. Ticket Implement input & output map functionality #27 is an indirect way to mitigate.
  2. The diff ticket Upgrade tracing & diffing output support #22 will likely need to snapshot/freeze the object anyway to track changes (using the proposed method.)
  3. More complex/efficient approach: use ES6 Proxy class/objects to observe changes, however that might require re-writing several built-in methods to align with that.
  4. Pre-scan the rule to know what change to expect (based on variable assignment); and only compare changes on a limited subset of the object/value.

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