- Notifications
You must be signed in to change notification settings - Fork23
Get the list of patches#143
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Is it possible to get a list of changes applied by a draft? I'd like to check if certain properties were updated by the draft and if they were, fire some side effects. Something like: const[nextState,changes]=create(baseState,update)for(constchangeofchanges){// Let's pretend array equality works like this.if(change.path===['some','nested','property']&&change.nextValue=1){sideeffect()}} Can I achieve this with the existing API somehow? I've seen #72 does sound extremely close to what I need! |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 1 comment 4 replies
-
#72 is primarily about listening for changes to the draft in a manner before it's finalized. From your example, it seems you are listening for changesafter the finalization is complete and a new state has been produced. This means you could consider using a shallow comparison to detect changes. For example: functionisEqual(x:any,y:any){if(x===y){returnx!==0||1/x===1/y;}else{returnx!==x&&y!==y;}}constnextState=create(baseState,update);if(!isEqual(nextState.some.nested.property,baseState.some.nested.property)){sideEffect();} |
BetaWas this translation helpful?Give feedback.
All reactions
-
Thanks for a quick response! I can actually see the patches diff here: const[nextState,patches]=create(prevState,callback)
|
BetaWas this translation helpful?Give feedback.
All reactions
-
I'm not sure about the specific scenario for your check, but generally, using it to check for changing may not be rigorous. First, it should be noted that in the If you really need to use this solution, I suggest it should be set to This ensures that every detailed modification to the array will have a detailed patch generated, instead of being replaced by a length modification. Additionally, comparing possible values could also be an issue. For example, const[state,patches,inversePatches]=create({arr:[0,1,2,undefined],a:undefined,},(draft)=>{deletedraft.a;draft.arr.length=3;},{enablePatches:{arrayLengthAssignment:false,},});console.log('mutative patches:',patches);// mutative patches: [// { op: 'remove', path: [ 'arr', 3 ] },// { op: 'remove', path: [ 'a' ] }// ] In this example, although the patches indicate that the corresponding value was modified, the actual value may not have really been changed. |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
-
Good point. So |
BetaWas this translation helpful?Give feedback.
All reactions
-
To be precise, if the array exceeds 3 elements, the extra ones are trimmed. If it has fewer than 3, it is filled with
Yes. |
BetaWas this translation helpful?Give feedback.