Common approach to get a node’s old value when modifying or deleting node in a graph?

My application contains a graph of nodes. When user modifies a node, the change may propagate to other nodes. For each nodes that are changed directly or indrectly, an event handler is triggered. The code in the event handler needs to access the node’s old property value (the value before its changed). I wonder what’s the typical and robust way to implement it?

A straightforward approach may be determining the nodes to be changed (both directly and indirectly) and sending an event, say, “willChange” to them to notiffy them to save current value. But that requires the app to determine ahead which nodes will be changed, which is specific to application logic. I didn’t try this approach.

My current approach is saving the entire graph before making change, then accessing the counterpart node in the backed graph to get old value, as below:

-> backup the entire graph
-> make change to the active graph
   -> event handler is triggered on a node
      -> retrieve the node's old value from the backed up graph

This works, but has one limietaion. For example, the following doesn’t work because when we make the second change, the node value saved in the backed up graph is not up-to-date.

-> backup the entire graph
-> make a first change to the active graph
   -> event handler is triggered on a node
      -> retrieve the node's old value from the backed up graph
-> make a second change to the active graph
   -> event handler is triggered on a node
      -> retrieve the node's old value from the backed up graph

At first sight, the issue can be easily resolved by simply backing up the graph again before making the second change:

-> backup the entire graph
-> make a first change to the active graph
   -> event handler is triggered on a node
      -> retrieve the node's old value from the backed up graph
-> backup the entire graph
-> make a second change to the active graph
   -> event handler is triggered on a node
      -> retrieve the node's old value from the backed up graph

But it’s actually not that simple, because changes may be nested, like the following:

-> backup the entire graph
-> make a first change to the active graph
   -> event handler is triggered on a node
      -> retrieve the node's old value from the backed up graph
      -> make a second/nested change to the active graph
         -> event handler is triggered on a node
            -> retrieve the node's old value from the backed up graph

I resolve the issue in my current application with two approaches:

  1. Backup the entire graph again before the seond/nested change. This works. But backing up entire graph takes cpu time. So I suppose I should avoid doing this very often. Note that my application is event driven, so the event handler trigged in the second change may make a third change and this goes on.

  2. Design application code carefully to avoid the above nested call. This works, but it’s not a general solution and imposes restriction on how to design the code.

This is the first time I work on an application based on a graph of nodes. I didn’t have experience in this area before. I have made a long progress (the current application works fine) but I can’t help thinking if there is any better way to deal with the issue? I wonder what’s the common way to get a node’s old value when modifying or deleting node in a graph? Thanks.