rules-machine
rules-machine copied to clipboard
Input mutation
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.
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?
I think that's a fine solution as long as it's clear in the docs.
There's a few possible options around this that I want to consider:
- Ticket #27 is an indirect way to mitigate.
- The diff ticket #22 will likely need to snapshot/freeze the object anyway to track changes (using the proposed method.)
- 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.
- 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.