Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Get the list of patches#143

Unanswered
kettanaito asked this question inQ&A
Sep 23, 2025· 1 comments· 4 replies
Discussion options

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 seenpatches but that's afunction meant to apply/revert the changes. Not quite what I'm looking for.

#72 does sound extremely close to what I need!

You must be logged in to vote

Replies: 1 comment 4 replies

Comment options

#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();}
You must be logged in to vote
4 replies
@kettanaito
Comment options

Thanks for a quick response! I can actually see the patches diff here:

const[nextState,patches]=create(prevState,callback)

patches contains a serialized array of changes performed in a draft! Would that be safe to use? It does seem to work for my use case from what I can see (still exploring).

@unadlib
Comment options

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 theenablePatches option, whenenablePatches: true is set,arrayLengthAssignment defaults totrue. For details, you can refer tohttps://mutative.js.org/docs/api-reference/create.

If you really need to use this solution, I suggest it should be set tofalse, like this:

{  enablePatches: {    arrayLengthAssignment: false,  },}

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.

@kettanaito
Comment options

Good point. Sodraft.array.length = 3 gets interpreted as "the element by third index has been removed", right? And if there are ten elements after that index, will that produce 10 patch objects?

@unadlib
Comment options

So draft.array.length = 3 gets interpreted as "the element by third index has been removed", right?

To be precise, if the array exceeds 3 elements, the extra ones are trimmed. If it has fewer than 3, it is filled withundefined to meet the length of 3.

And if there are ten elements after that index, will that produce 10 patch objects?

Yes.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@kettanaito@unadlib

[8]ページ先頭

©2009-2025 Movatter.jp