mobxjs / mobx-state-tree

Full-featured reactive state management without the boilerplate
https://mobx-state-tree.js.org/
MIT License
6.94k stars 640 forks source link

Need to pass current patched node to onPatch callback function #2002

Closed geohuz closed 1 year ago

geohuz commented 1 year ago

Feature request

Is your feature request related to a problem? Please describe.

Describe the solution you'd like

I have a program need to automatically subscribe the change to the data, so for example using onPatch to capture an "add" operation, in the call back subscribing the further change from other clients(with websocket communication) then apply the change to the node, but since there is no such argument I have to use the store instance and lookup the node then apply change.

so my patch fuction is a generic process, something like:

onPatch(userStore, (patch, node)=>triggerUpdate(patch, node))

function triggerUpdatePatch(patch, node) {
  case "add":
     subscribeChange(data=> {
       applySnapshot(node, data)  // this makes the function operate dynamically
     })
}

and then, I can use onPatch(orderStore, (patch, node)=>triggerUpdate(patch, node)) for the generic usage.

Describe alternatives you've considered

Additional context

Are you willing to (attempt) a PR?

geohuz commented 1 year ago

This is a stupid question, I should figure out the solution easily.