views:

75

answers:

3

I'm writing a plug-in for a program where I need to track when native objects are added, removed, and edited in the active document. The API has events that are fired when the document is edited. However, the program does not track when the native objects actually change. Instead an object changing is treated as the object being deleted and then immediately replaced with another modified object with the same ID. This is done this way so the program can keep track of an undo record.

After some experimenting I've determined that the events are evoked as follows:

An Object Is Added: OnAddObject Event

An Object Is Removed: OnDeleteObject Event

An Object is Changed: OnReplaceObject Event->OnDeleteObject Event->OnAddObject Event

Right now my plug-in is only watching the OnAdd and OnDelete events where it is adding and removing instances of my custom object to and from a collection. But this also means every time an object changes my plugin is removing an reinitializing a near identical object. I'd rather just know that the document object has changed so my custom object can be refreshed rather then completely reinstantiated.

How can my methods that are subscribed to the OnDelete and OnAdd events tell that the object is not really being added or deleted but is being replaced because it has changed?

A: 

Keep track of the document ID.

modosansreves
A: 

That's a bit of a tough one because of the bad event structure of the original program. Perhaps you could put the id of the item that's being removed in a dictionary together with the current time.

That way you could create separate thread that checks the content of the dictionary (when it contains items). Give each item a few milliseconds (compare the DateTime from the dict.) for the Add event to kick in and so decide if it is a regular delete or a replace.

It's a bit inefficient because you'll have to slowdown your delete a few millisec. But I can't think of a better way.

Zyphrax
way to complicated
Johannes Rudolph
A: 

I suggest introducing "lock" variables (e.g. bools)

When the Replaced event fires, set them to lock

In Added and removed check the lock variables. If they are set to lock, set them to unlock and return.

Johannes Rudolph
I guess this isn't possible, because the Add/Remove events fire before the Replaced events.
Zyphrax
According to Eric's question, the Replace event fires followed by the Delete, followed by the Add, so Johannes solution should be OK, accounting for threading issues.
Andy Shellam
This answer is the closest to my solution. ALthought I did not use Bool variables but rather cached the ID of the object in the OnReplaceObject event
Eric Anastas